From d8c97a9f81eea98e4c5b7320ae3f6d0290bbb601 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Mon, 6 Jul 2026 16:44:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=90=BA=E7=A8=8B?= =?UTF-8?q?=E6=AD=BB=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins_func/functions/hass_play_music.py | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/main/xiaozhi-server/plugins_func/functions/hass_play_music.py b/main/xiaozhi-server/plugins_func/functions/hass_play_music.py index 7bd9f2a1..a386493f 100644 --- a/main/xiaozhi-server/plugins_func/functions/hass_play_music.py +++ b/main/xiaozhi-server/plugins_func/functions/hass_play_music.py @@ -1,7 +1,6 @@ from plugins_func.register import register_function, ToolType, ActionResponse, Action from plugins_func.functions.hass_init import initialize_hass_handler from config.logger import setup_logging -import asyncio import requests from typing import TYPE_CHECKING @@ -39,16 +38,27 @@ hass_play_music_function_desc = { ) def hass_play_music(conn: "ConnectionHandler", entity_id="", media_content_id="random"): try: - # 执行音乐播放命令 - future = asyncio.run_coroutine_threadsafe( - handle_hass_play_music(conn, entity_id, media_content_id), conn.loop + task = conn.loop.create_task( + handle_hass_play_music(conn, entity_id, media_content_id) ) - ha_response = future.result() + + def handle_done(f): + try: + f.result() + logger.bind(tag=TAG).info("音乐播放完成") + except Exception as e: + logger.bind(tag=TAG).error(f"音乐播放失败: {e}") + + task.add_done_callback(handle_done) + return ActionResponse( - action=Action.RESPONSE, result="退出意图已处理", response=ha_response + action=Action.RECORD, result="指令已接收", response="正在为您播放音乐" ) except Exception as e: logger.bind(tag=TAG).error(f"处理音乐意图错误: {e}") + return ActionResponse( + action=Action.RESPONSE, result=str(e), response="播放音乐时出错了" + ) async def handle_hass_play_music(