From 1b8963f311d8b76ef2f31ab2f601d150a8813157 Mon Sep 17 00:00:00 2001 From: 3030332422 <3030332422@qq.com> Date: Tue, 22 Jul 2025 16:28:12 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=B7=BB=E5=8A=A0Home=20Assistant?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E8=B6=85=E6=97=B6=E5=A4=84=E7=90=86=E5=92=8C?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins_func/functions/hass_get_state.py | 10 ++++++++-- .../plugins_func/functions/hass_set_state.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 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 e8167db4..94478e35 100644 --- a/main/xiaozhi-server/plugins_func/functions/hass_get_state.py +++ b/main/xiaozhi-server/plugins_func/functions/hass_get_state.py @@ -33,10 +33,16 @@ def hass_get_state(conn, entity_id=""): future = asyncio.run_coroutine_threadsafe( handle_hass_get_state(conn, entity_id), conn.loop ) - ha_response = future.result() + # 添加10秒超时 + ha_response = future.result(timeout=10) return ActionResponse(Action.REQLLM, ha_response, None) + except asyncio.TimeoutError: + logger.bind(tag=TAG).error("获取Home Assistant状态超时") + return ActionResponse(Action.ERROR, "请求超时", None) except Exception as e: - logger.bind(tag=TAG).error(f"处理设置属性意图错误: {e}") + error_msg = f"执行Home Assistant操作失败" + logger.bind(tag=TAG).error(error_msg) + return ActionResponse(Action.ERROR, error_msg, None) async def handle_hass_get_state(conn, entity_id): 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 704148ee..430679bb 100644 --- a/main/xiaozhi-server/plugins_func/functions/hass_set_state.py +++ b/main/xiaozhi-server/plugins_func/functions/hass_set_state.py @@ -55,10 +55,16 @@ def hass_set_state(conn, entity_id="", state={}): future = asyncio.run_coroutine_threadsafe( handle_hass_set_state(conn, entity_id, state), conn.loop ) - ha_response = future.result() + # 添加10秒超时 + ha_response = future.result(timeout=10) return ActionResponse(Action.REQLLM, ha_response, None) + except asyncio.TimeoutError: + logger.bind(tag=TAG).error("设置Home Assistant状态超时") + return ActionResponse(Action.ERROR, "请求超时", None) except Exception as e: - logger.bind(tag=TAG).error(f"处理设置属性意图错误: {e}") + error_msg = f"执行Home Assistant操作失败" + logger.bind(tag=TAG).error(error_msg) + return ActionResponse(Action.ERROR, error_msg, None) async def handle_hass_set_state(conn, entity_id, state):