2025-03-22 20:39:52 +08:00
|
|
|
from plugins_func.register import register_function, ToolType, ActionResponse, Action
|
|
|
|
|
from plugins_func.functions.hass_init import initialize_hass_handler
|
2025-03-18 20:42:09 +08:00
|
|
|
from config.logger import setup_logging
|
|
|
|
|
import asyncio
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
TAG = __name__
|
|
|
|
|
logger = setup_logging()
|
|
|
|
|
|
2025-03-22 20:39:52 +08:00
|
|
|
hass_get_state_function_desc = {
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "hass_get_state",
|
2025-04-09 10:39:42 +08:00
|
|
|
"description": "获取homeassistant里设备的状态,包括查询灯光亮度、颜色、色温,媒体播放器的音量,设备的暂停、继续操作",
|
2025-03-22 20:39:52 +08:00
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"entity_id": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "需要操作的设备id,homeassistant里的entity_id"
|
2025-03-18 20:42:09 +08:00
|
|
|
}
|
2025-03-22 20:39:52 +08:00
|
|
|
},
|
|
|
|
|
"required": ["entity_id"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-18 20:42:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@register_function("hass_get_state", hass_get_state_function_desc, ToolType.SYSTEM_CTL)
|
|
|
|
|
def hass_get_state(conn, entity_id=''):
|
|
|
|
|
try:
|
|
|
|
|
|
2025-03-22 20:39:52 +08:00
|
|
|
future = asyncio.run_coroutine_threadsafe(
|
|
|
|
|
handle_hass_get_state(conn, entity_id),
|
|
|
|
|
conn.loop
|
|
|
|
|
)
|
|
|
|
|
ha_response = future.result()
|
2025-04-09 10:39:42 +08:00
|
|
|
return ActionResponse( Action.REQLLM, ha_response , None )
|
2025-03-18 20:42:09 +08:00
|
|
|
except Exception as e:
|
2025-03-22 20:39:52 +08:00
|
|
|
logger.bind(tag=TAG).error(f"处理设置属性意图错误: {e}")
|
|
|
|
|
|
|
|
|
|
|
2025-03-18 20:42:09 +08:00
|
|
|
async def handle_hass_get_state(conn, entity_id):
|
2025-03-22 20:39:52 +08:00
|
|
|
HASS_CACHE = initialize_hass_handler(conn)
|
2025-03-18 20:42:09 +08:00
|
|
|
api_key = HASS_CACHE['api_key']
|
|
|
|
|
base_url = HASS_CACHE['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)
|
|
|
|
|
if response.status_code == 200:
|
2025-04-09 10:39:42 +08:00
|
|
|
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
|
|
|
|
|
#return response.json()['attributes']
|
|
|
|
|
#response.attributes
|
|
|
|
|
|
2025-03-18 20:42:09 +08:00
|
|
|
else:
|
|
|
|
|
return f"切换失败,错误码: {response.status_code}"
|