mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-30 05:13:59 +08:00
refactor: 合并 HA 设备控制插件为 hass_state 模块
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
-- liquibase formatted sql
|
||||||
|
|
||||||
|
-- 插入新的 hass_state 插件记录(合并 get_state 的配置字段)
|
||||||
|
INSERT INTO ai_model_provider (id, model_type, provider_code, name, fields,
|
||||||
|
sort, creator, create_date, updater, update_date)
|
||||||
|
VALUES ('SYSTEM_PLUGIN_HA_STATE',
|
||||||
|
'Plugin',
|
||||||
|
'hass_state',
|
||||||
|
'HomeAssistant设备控制',
|
||||||
|
JSON_ARRAY(
|
||||||
|
JSON_OBJECT(
|
||||||
|
'key', 'base_url',
|
||||||
|
'type', 'string',
|
||||||
|
'label', 'HA 服务器地址',
|
||||||
|
'default',
|
||||||
|
(SELECT param_value FROM sys_params WHERE param_code = 'plugins.home_assistant.base_url')
|
||||||
|
),
|
||||||
|
JSON_OBJECT(
|
||||||
|
'key', 'api_key',
|
||||||
|
'type', 'string',
|
||||||
|
'label', 'HA API 访问令牌',
|
||||||
|
'default',
|
||||||
|
(SELECT param_value FROM sys_params WHERE param_code = 'plugins.home_assistant.api_key')
|
||||||
|
),
|
||||||
|
JSON_OBJECT(
|
||||||
|
'key', 'devices',
|
||||||
|
'type', 'array',
|
||||||
|
'label', '设备列表(名称,实体ID;…)',
|
||||||
|
'default',
|
||||||
|
(SELECT param_value FROM sys_params WHERE param_code = 'plugins.home_assistant.devices')
|
||||||
|
)
|
||||||
|
),
|
||||||
|
50, 0, NOW(), 0, NOW());
|
||||||
|
|
||||||
|
-- 迁移已配置的 agent:把指向 HA_GET_STATE 的改成 HA_STATE(保留参数)
|
||||||
|
UPDATE ai_agent_plugin_mapping
|
||||||
|
SET plugin_id = 'SYSTEM_PLUGIN_HA_STATE'
|
||||||
|
WHERE plugin_id = 'SYSTEM_PLUGIN_HA_GET_STATE';
|
||||||
|
|
||||||
|
-- HA_SET_STATE 本来就没有配置字段(fields=[]),直接删除其 mapping
|
||||||
|
DELETE FROM ai_agent_plugin_mapping
|
||||||
|
WHERE plugin_id = 'SYSTEM_PLUGIN_HA_SET_STATE';
|
||||||
|
|
||||||
|
-- 删除旧的插件定义
|
||||||
|
DELETE FROM ai_model_provider
|
||||||
|
WHERE id IN ('SYSTEM_PLUGIN_HA_GET_STATE', 'SYSTEM_PLUGIN_HA_SET_STATE');
|
||||||
@@ -711,3 +711,10 @@ databaseChangeLog:
|
|||||||
- sqlFile:
|
- sqlFile:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202607101200.sql
|
path: classpath:db/changelog/202607101200.sql
|
||||||
|
- changeSet:
|
||||||
|
id: 202607290930
|
||||||
|
author: cgd
|
||||||
|
changes:
|
||||||
|
- sqlFile:
|
||||||
|
encoding: utf8
|
||||||
|
path: classpath:db/changelog/202607290930.sql
|
||||||
|
|||||||
@@ -497,8 +497,7 @@ const FALLBACK_PLUGIN_NAME_KEYS = {
|
|||||||
SYSTEM_PLUGIN_MUSIC: "agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC",
|
SYSTEM_PLUGIN_MUSIC: "agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC",
|
||||||
SYSTEM_PLUGIN_NEWS_CHINANEWS: "agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS",
|
SYSTEM_PLUGIN_NEWS_CHINANEWS: "agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS",
|
||||||
SYSTEM_PLUGIN_NEWS_NEWSNOW: "agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW",
|
SYSTEM_PLUGIN_NEWS_NEWSNOW: "agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW",
|
||||||
SYSTEM_PLUGIN_HA_GET_STATE: "agentSnapshot.plugin.SYSTEM_PLUGIN_HA_GET_STATE",
|
SYSTEM_PLUGIN_HA_STATE: "agentSnapshot.plugin.SYSTEM_PLUGIN_HA_STATE",
|
||||||
SYSTEM_PLUGIN_HA_SET_STATE: "agentSnapshot.plugin.SYSTEM_PLUGIN_HA_SET_STATE",
|
|
||||||
SYSTEM_PLUGIN_HA_PLAY_MUSIC: "agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC",
|
SYSTEM_PLUGIN_HA_PLAY_MUSIC: "agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC",
|
||||||
SYSTEM_PLUGIN_WEB_SEARCH: "agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH",
|
SYSTEM_PLUGIN_WEB_SEARCH: "agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH",
|
||||||
SYSTEM_PLUGIN_CALL_DEVICE: "agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE"
|
SYSTEM_PLUGIN_CALL_DEVICE: "agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE"
|
||||||
|
|||||||
@@ -903,8 +903,7 @@ export default {
|
|||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Server-Musikplayer',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Server-Musikplayer',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'China News',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'China News',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'NewsNow-Aggregation',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'NewsNow-Aggregation',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_GET_STATE': 'HomeAssistant-Statusabfrage',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_STATE': 'HomeAssistant-Gerätesteuerung',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_SET_STATE': 'HomeAssistant-Statusaktualisierung',
|
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant-Musikplayer',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant-Musikplayer',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Websuche',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Websuche',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Geräteanruf',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Geräteanruf',
|
||||||
|
|||||||
@@ -953,8 +953,7 @@ export default {
|
|||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Server music player',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Server music player',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'China News',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'China News',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'NewsNow aggregation',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'NewsNow aggregation',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_GET_STATE': 'HomeAssistant state query',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_STATE': 'HomeAssistant device control',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_SET_STATE': 'HomeAssistant state update',
|
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant music player',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant music player',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Web search',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Web search',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Device-to-device call',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Device-to-device call',
|
||||||
|
|||||||
@@ -903,8 +903,7 @@ export default {
|
|||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Reprodutor de música do servidor',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Reprodutor de música do servidor',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'China News',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'China News',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'Agregação NewsNow',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'Agregação NewsNow',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_GET_STATE': 'Consulta de estado do HomeAssistant',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_STATE': 'Controle de dispositivo do HomeAssistant',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_SET_STATE': 'Atualização de estado do HomeAssistant',
|
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'Reprodutor de música do HomeAssistant',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'Reprodutor de música do HomeAssistant',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Pesquisa na web',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Pesquisa na web',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Chamada entre dispositivos',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Chamada entre dispositivos',
|
||||||
|
|||||||
@@ -903,8 +903,7 @@ export default {
|
|||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Trình phát nhạc máy chủ',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': 'Trình phát nhạc máy chủ',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'Tin tức China News',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': 'Tin tức China News',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'Tổng hợp NewsNow',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'Tổng hợp NewsNow',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_GET_STATE': 'Truy vấn trạng thái HomeAssistant',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_STATE': 'Điều khiển thiết bị HomeAssistant',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_SET_STATE': 'Cập nhật trạng thái HomeAssistant',
|
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'Trình phát nhạc HomeAssistant',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'Trình phát nhạc HomeAssistant',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Tìm kiếm web',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': 'Tìm kiếm web',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Gọi thiết bị',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': 'Gọi thiết bị',
|
||||||
|
|||||||
@@ -953,8 +953,7 @@ export default {
|
|||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': '服务器音乐播放',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': '服务器音乐播放',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': '中新网新闻',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': '中新网新闻',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'newsnow新闻聚合',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'newsnow新闻聚合',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_GET_STATE': 'HomeAssistant设备状态查询',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_STATE': 'HomeAssistant设备控制',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_SET_STATE': 'HomeAssistant设备状态修改',
|
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant音乐播放',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant音乐播放',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': '联网搜索',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': '联网搜索',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': '设备呼叫设备',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': '设备呼叫设备',
|
||||||
|
|||||||
@@ -903,8 +903,7 @@ export default {
|
|||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': '伺服器音樂播放',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_MUSIC': '伺服器音樂播放',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': '中新網新聞',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_CHINANEWS': '中新網新聞',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'newsnow新聞聚合',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_NEWS_NEWSNOW': 'newsnow新聞聚合',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_GET_STATE': 'HomeAssistant設備狀態查詢',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_STATE': 'HomeAssistant設備控制',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_SET_STATE': 'HomeAssistant設備狀態修改',
|
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant音樂播放',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_HA_PLAY_MUSIC': 'HomeAssistant音樂播放',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': '聯網搜尋',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_WEB_SEARCH': '聯網搜尋',
|
||||||
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': '設備呼叫設備',
|
'agentSnapshot.plugin.SYSTEM_PLUGIN_CALL_DEVICE': '設備呼叫設備',
|
||||||
|
|||||||
@@ -286,8 +286,7 @@ Intent:
|
|||||||
# play_music是服务器自带的音乐播放,hass_play_music是通过home assistant控制的独立外部程序音乐播放
|
# play_music是服务器自带的音乐播放,hass_play_music是通过home assistant控制的独立外部程序音乐播放
|
||||||
# 如果用了hass_play_music,就不要开启play_music,两者只留一个
|
# 如果用了hass_play_music,就不要开启play_music,两者只留一个
|
||||||
- play_music
|
- play_music
|
||||||
#- hass_get_state
|
#- hass_state
|
||||||
#- hass_set_state
|
|
||||||
#- hass_play_music
|
#- hass_play_music
|
||||||
|
|
||||||
Memory:
|
Memory:
|
||||||
|
|||||||
@@ -1,98 +0,0 @@
|
|||||||
import httpx
|
|
||||||
from config.logger import setup_logging
|
|
||||||
from plugins_func.functions.hass_init import initialize_hass_handler
|
|
||||||
from plugins_func.register import register_function, ToolType, ActionResponse, Action
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from core.connection import ConnectionHandler
|
|
||||||
|
|
||||||
TAG = __name__
|
|
||||||
logger = setup_logging()
|
|
||||||
|
|
||||||
hass_get_state_function_desc = {
|
|
||||||
"type": "function",
|
|
||||||
"function": {
|
|
||||||
"name": "hass_get_state",
|
|
||||||
"description": "获取homeassistant里设备的状态,包括查询灯光亮度、颜色、色温,媒体播放器的音量,设备的暂停、继续操作",
|
|
||||||
"parameters": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"entity_id": {
|
|
||||||
"type": "string",
|
|
||||||
"description": "需要操作的设备id,homeassistant里的entity_id",
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["entity_id"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@register_function("hass_get_state", hass_get_state_function_desc, ToolType.SYSTEM_CTL)
|
|
||||||
async def hass_get_state(conn: "ConnectionHandler", entity_id=""):
|
|
||||||
try:
|
|
||||||
ha_response = await handle_hass_get_state(conn, entity_id)
|
|
||||||
return ActionResponse(Action.REQLLM, ha_response, None)
|
|
||||||
except httpx.TimeoutException:
|
|
||||||
logger.bind(tag=TAG).error("获取Home Assistant状态超时")
|
|
||||||
return ActionResponse(Action.ERROR, "请求超时", None)
|
|
||||||
except Exception as 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: "ConnectionHandler", 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"}
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=httpx.Timeout(5.0, connect=3.0)) as client:
|
|
||||||
response = await client.get(url, headers=headers)
|
|
||||||
|
|
||||||
if response.status_code == 200:
|
|
||||||
responsetext = "设备状态:" + response.json()["state"] + " "
|
|
||||||
logger.bind(tag=TAG).info(f"api返回内容: {response.json()}")
|
|
||||||
|
|
||||||
if "media_title" in response.json()["attributes"]:
|
|
||||||
responsetext = (
|
|
||||||
responsetext
|
|
||||||
+ "正在播放的是:"
|
|
||||||
+ str(response.json()["attributes"]["media_title"])
|
|
||||||
+ " "
|
|
||||||
)
|
|
||||||
if "volume_level" in response.json()["attributes"]:
|
|
||||||
responsetext = (
|
|
||||||
responsetext
|
|
||||||
+ "音量是:"
|
|
||||||
+ str(response.json()["attributes"]["volume_level"])
|
|
||||||
+ " "
|
|
||||||
)
|
|
||||||
if "color_temp_kelvin" in response.json()["attributes"]:
|
|
||||||
responsetext = (
|
|
||||||
responsetext
|
|
||||||
+ "色温是:"
|
|
||||||
+ str(response.json()["attributes"]["color_temp_kelvin"])
|
|
||||||
+ " "
|
|
||||||
)
|
|
||||||
if "rgb_color" in response.json()["attributes"]:
|
|
||||||
responsetext = (
|
|
||||||
responsetext
|
|
||||||
+ "rgb颜色是:"
|
|
||||||
+ str(response.json()["attributes"]["rgb_color"])
|
|
||||||
+ " "
|
|
||||||
)
|
|
||||||
if "brightness" in response.json()["attributes"]:
|
|
||||||
responsetext = (
|
|
||||||
responsetext
|
|
||||||
+ "亮度是:"
|
|
||||||
+ str(response.json()["attributes"]["brightness"])
|
|
||||||
+ " "
|
|
||||||
)
|
|
||||||
logger.bind(tag=TAG).info(f"查询返回内容: {responsetext}")
|
|
||||||
return responsetext
|
|
||||||
else:
|
|
||||||
return f"切换失败,错误码: {response.status_code}"
|
|
||||||
@@ -16,7 +16,7 @@ def append_devices_to_prompt(conn):
|
|||||||
config_source = (
|
config_source = (
|
||||||
"home_assistant"
|
"home_assistant"
|
||||||
if plugins_config.get("home_assistant")
|
if plugins_config.get("home_assistant")
|
||||||
else "hass_get_state"
|
else "hass_state"
|
||||||
)
|
)
|
||||||
|
|
||||||
if "hass_get_state" in funcs or "hass_set_state" in funcs:
|
if "hass_get_state" in funcs or "hass_set_state" in funcs:
|
||||||
@@ -36,7 +36,7 @@ def initialize_hass_handler(conn):
|
|||||||
plugins_config = conn.config.get("plugins", {})
|
plugins_config = conn.config.get("plugins", {})
|
||||||
# 确定配置来源
|
# 确定配置来源
|
||||||
config_source = (
|
config_source = (
|
||||||
"home_assistant" if plugins_config.get("home_assistant") else "hass_get_state"
|
"home_assistant" if plugins_config.get("home_assistant") else "hass_state"
|
||||||
)
|
)
|
||||||
if not plugins_config.get(config_source):
|
if not plugins_config.get(config_source):
|
||||||
return ha_config
|
return ha_config
|
||||||
|
|||||||
+87
@@ -10,6 +10,24 @@ if TYPE_CHECKING:
|
|||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
|
hass_get_state_function_desc = {
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "hass_get_state",
|
||||||
|
"description": "获取homeassistant里设备的状态,包括查询灯光亮度、颜色、色温,媒体播放器的音量,设备的暂停、继续操作",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"entity_id": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "需要操作的设备id,homeassistant里的entity_id",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["entity_id"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
hass_set_state_function_desc = {
|
hass_set_state_function_desc = {
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
@@ -52,6 +70,20 @@ hass_set_state_function_desc = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@register_function("hass_get_state", hass_get_state_function_desc, ToolType.SYSTEM_CTL)
|
||||||
|
async def hass_get_state(conn: "ConnectionHandler", entity_id=""):
|
||||||
|
try:
|
||||||
|
ha_response = await handle_hass_get_state(conn, entity_id)
|
||||||
|
return ActionResponse(Action.REQLLM, ha_response, None)
|
||||||
|
except httpx.TimeoutException:
|
||||||
|
logger.bind(tag=TAG).error("获取Home Assistant状态超时")
|
||||||
|
return ActionResponse(Action.ERROR, "请求超时", None)
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"执行Home Assistant操作失败"
|
||||||
|
logger.bind(tag=TAG).error(error_msg)
|
||||||
|
return ActionResponse(Action.ERROR, error_msg, None)
|
||||||
|
|
||||||
|
|
||||||
@register_function("hass_set_state", hass_set_state_function_desc, ToolType.SYSTEM_CTL)
|
@register_function("hass_set_state", hass_set_state_function_desc, ToolType.SYSTEM_CTL)
|
||||||
async def hass_set_state(conn: "ConnectionHandler", entity_id="", state=None):
|
async def hass_set_state(conn: "ConnectionHandler", entity_id="", state=None):
|
||||||
if state is None:
|
if state is None:
|
||||||
@@ -68,6 +100,61 @@ async def hass_set_state(conn: "ConnectionHandler", entity_id="", state=None):
|
|||||||
return ActionResponse(Action.ERROR, error_msg, None)
|
return ActionResponse(Action.ERROR, error_msg, None)
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_hass_get_state(conn: "ConnectionHandler", 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"}
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=httpx.Timeout(5.0, connect=3.0)) as client:
|
||||||
|
response = await client.get(url, headers=headers)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
responsetext = "设备状态:" + response.json()["state"] + " "
|
||||||
|
logger.bind(tag=TAG).info(f"api返回内容: {response.json()}")
|
||||||
|
|
||||||
|
if "media_title" in response.json()["attributes"]:
|
||||||
|
responsetext = (
|
||||||
|
responsetext
|
||||||
|
+ "正在播放的是:"
|
||||||
|
+ str(response.json()["attributes"]["media_title"])
|
||||||
|
+ " "
|
||||||
|
)
|
||||||
|
if "volume_level" in response.json()["attributes"]:
|
||||||
|
responsetext = (
|
||||||
|
responsetext
|
||||||
|
+ "音量是:"
|
||||||
|
+ str(response.json()["attributes"]["volume_level"])
|
||||||
|
+ " "
|
||||||
|
)
|
||||||
|
if "color_temp_kelvin" in response.json()["attributes"]:
|
||||||
|
responsetext = (
|
||||||
|
responsetext
|
||||||
|
+ "色温是:"
|
||||||
|
+ str(response.json()["attributes"]["color_temp_kelvin"])
|
||||||
|
+ " "
|
||||||
|
)
|
||||||
|
if "rgb_color" in response.json()["attributes"]:
|
||||||
|
responsetext = (
|
||||||
|
responsetext
|
||||||
|
+ "rgb颜色是:"
|
||||||
|
+ str(response.json()["attributes"]["rgb_color"])
|
||||||
|
+ " "
|
||||||
|
)
|
||||||
|
if "brightness" in response.json()["attributes"]:
|
||||||
|
responsetext = (
|
||||||
|
responsetext
|
||||||
|
+ "亮度是:"
|
||||||
|
+ str(response.json()["attributes"]["brightness"])
|
||||||
|
+ " "
|
||||||
|
)
|
||||||
|
logger.bind(tag=TAG).info(f"查询返回内容: {responsetext}")
|
||||||
|
return responsetext
|
||||||
|
else:
|
||||||
|
return f"切换失败,错误码: {response.status_code}"
|
||||||
|
|
||||||
|
|
||||||
async def handle_hass_set_state(conn: "ConnectionHandler", entity_id, state):
|
async def handle_hass_set_state(conn: "ConnectionHandler", entity_id, state):
|
||||||
ha_config = initialize_hass_handler(conn)
|
ha_config = initialize_hass_handler(conn)
|
||||||
api_key = ha_config.get("api_key")
|
api_key = ha_config.get("api_key")
|
||||||
Reference in New Issue
Block a user