From a5e748f7754fa7bf13de51ea6aab965d3695818d Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Thu, 28 Aug 2025 00:39:43 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96ha=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins_func/functions/hass_get_state.py | 11 +++-------- .../plugins_func/functions/hass_set_state.py | 10 +++------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/main/xiaozhi-server/plugins_func/functions/hass_get_state.py b/main/xiaozhi-server/plugins_func/functions/hass_get_state.py index 94478e35..4efb12b8 100644 --- a/main/xiaozhi-server/plugins_func/functions/hass_get_state.py +++ b/main/xiaozhi-server/plugins_func/functions/hass_get_state.py @@ -29,12 +29,7 @@ hass_get_state_function_desc = { @register_function("hass_get_state", hass_get_state_function_desc, ToolType.SYSTEM_CTL) def hass_get_state(conn, entity_id=""): try: - - future = asyncio.run_coroutine_threadsafe( - handle_hass_get_state(conn, entity_id), conn.loop - ) - # 添加10秒超时 - ha_response = future.result(timeout=10) + ha_response = handle_hass_get_state(conn, entity_id) return ActionResponse(Action.REQLLM, ha_response, None) except asyncio.TimeoutError: logger.bind(tag=TAG).error("获取Home Assistant状态超时") @@ -45,13 +40,13 @@ def hass_get_state(conn, entity_id=""): return ActionResponse(Action.ERROR, error_msg, None) -async def handle_hass_get_state(conn, entity_id): +def handle_hass_get_state(conn, entity_id): ha_config = initialize_hass_handler(conn) api_key = ha_config.get("api_key") base_url = ha_config.get("base_url") url = f"{base_url}/api/states/{entity_id}" headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} - response = requests.get(url, headers=headers) + response = requests.get(url, headers=headers, timeout=5) if response.status_code == 200: responsetext = "设备状态:" + response.json()["state"] + " " logger.bind(tag=TAG).info(f"api返回内容: {response.json()}") diff --git a/main/xiaozhi-server/plugins_func/functions/hass_set_state.py b/main/xiaozhi-server/plugins_func/functions/hass_set_state.py index c1fcdaa6..3addc823 100644 --- a/main/xiaozhi-server/plugins_func/functions/hass_set_state.py +++ b/main/xiaozhi-server/plugins_func/functions/hass_set_state.py @@ -54,11 +54,7 @@ def hass_set_state(conn, entity_id="", state=None): if state is None: state = {} try: - future = asyncio.run_coroutine_threadsafe( - handle_hass_set_state(conn, entity_id, state), conn.loop - ) - # 添加10秒超时 - ha_response = future.result(timeout=10) + ha_response = handle_hass_set_state(conn, entity_id, state) return ActionResponse(Action.REQLLM, ha_response, None) except asyncio.TimeoutError: logger.bind(tag=TAG).error("设置Home Assistant状态超时") @@ -69,7 +65,7 @@ def hass_set_state(conn, entity_id="", state=None): return ActionResponse(Action.ERROR, error_msg, None) -async def handle_hass_set_state(conn, entity_id, state): +def handle_hass_set_state(conn, entity_id, state): ha_config = initialize_hass_handler(conn) api_key = ha_config.get("api_key") base_url = ha_config.get("base_url") @@ -169,7 +165,7 @@ async def handle_hass_set_state(conn, entity_id, state): data = {"entity_id": entity_id, arg: value} url = f"{base_url}/api/services/{domain}/{action}" headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} - response = requests.post(url, headers=headers, json=data) + response = requests.post(url, headers=headers, json=data, timeout=5) # 设置5秒超时 logger.bind(tag=TAG).info( f"设置状态:{description},url:{url},return_code:{response.status_code}" )