Merge pull request #1892 from xinnan-tech/py_fix_mcp

update:添加Home Assistant插件超时处理和错误日志
This commit is contained in:
hrz
2025-07-22 17:43:33 +08:00
committed by GitHub
2 changed files with 16 additions and 4 deletions
@@ -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):
@@ -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):