xiaozhi-esp32-server/main/xiaozhi-server/plugins_func/functions/hass_set_state.py
lapy c5ff101e66 feat(xiaozhi-server): western provider set, English defaults, runtime, and plugins
- Defaults and config aligned with English-first product surface

- Adds/removes ASR/TTS paths; drops China-only provider implementations and bundled SenseVoice assets

- Westernizes prompts, plugin/tool copy, connection and handler behavior on upstream fixes

- Powermem max_tokens and OpenAI no-thinking guard preserved on downstream path
2026-04-25 17:05:08 +01:00

179 lines
6.9 KiB
Python

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
if TYPE_CHECKING:
from core.connection import ConnectionHandler
TAG = __name__
logger = setup_logging()
hass_set_state_function_desc = {
"type": "function",
"function": {
"name": "hass_set_state",
"description": "Set device status in homeassistant, including on/off, adjust light brightness/color/color temperature, adjust player volume, device pause/resume/mute operations",
"parameters": {
"type": "object",
"properties": {
"state": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "Action to perform, turn on device: turn_on, turn off device: turn_off, increase brightness: brightness_up, decrease brightness: brightness_down, set brightness: brightness_value, increase volume: volume_up, decrease volume: volume_down, set volume: volume_set, set color temperature: set_kelvin, set color: set_color, device pause: pause, device continue: continue, mute/unmute: volume_mute",
},
"input": {
"type": "integer",
"description": "Only needed when setting volume or brightness, valid values 1-100, corresponding to 1%-100% of volume and brightness",
},
"is_muted": {
"type": "string",
"description": "Only needed when setting mute operation, value is true when setting mute, false when unmuting",
},
"rgb_color": {
"type": "array",
"items": {"type": "integer"},
"description": "Only needed when setting color, fill in the RGB value of the target color here",
},
},
"required": ["type"],
},
"entity_id": {
"type": "string",
"description": "Device id to operate, entity_id in homeassistant",
},
},
"required": ["state", "entity_id"],
},
},
}
@register_function("hass_set_state", hass_set_state_function_desc, ToolType.SYSTEM_CTL)
def hass_set_state(conn: "ConnectionHandler", entity_id="", state=None):
if state is None:
state = {}
try:
ha_response = handle_hass_set_state(conn, entity_id, state)
return ActionResponse(Action.REQLLM, ha_response, None)
except asyncio.TimeoutError:
logger.bind(tag=TAG).error("Set Home Assistant status timeout")
return ActionResponse(Action.ERROR, "Request timeout", None)
except Exception as e:
error_msg = f"Failed to execute Home Assistant operation"
logger.bind(tag=TAG).error(error_msg)
return ActionResponse(Action.ERROR, error_msg, None)
def handle_hass_set_state(conn: "ConnectionHandler", entity_id, state):
ha_config = initialize_hass_handler(conn)
api_key = ha_config.get("api_key")
base_url = ha_config.get("base_url")
"""
state = { "type":"brightness_up","input":"80","is_muted":"true"}
"""
domains = entity_id.split(".")
if len(domains) > 1:
domain = domains[0]
else:
return "Execution failed, incorrect device id"
action = ""
arg = ""
value = ""
if state["type"] == "turn_on":
description = "Device turned on"
if domain == "cover":
action = "open_cover"
elif domain == "vacuum":
action = "start"
else:
action = "turn_on"
elif state["type"] == "turn_off":
description = "Device turned off"
if domain == "cover":
action = "close_cover"
elif domain == "vacuum":
action = "stop"
else:
action = "turn_off"
elif state["type"] == "brightness_up":
description = "Light brightened"
action = "turn_on"
arg = "brightness_step_pct"
value = 10
elif state["type"] == "brightness_down":
description = "Light dimmed"
action = "turn_on"
arg = "brightness_step_pct"
value = -10
elif state["type"] == "brightness_value":
description = f"Brightness adjusted to {state['input']}"
action = "turn_on"
arg = "brightness_pct"
value = state["input"]
elif state["type"] == "set_color":
description = f"Color adjusted to {state['rgb_color']}"
action = "turn_on"
arg = "rgb_color"
value = state["rgb_color"]
elif state["type"] == "set_kelvin":
description = f"Color temperature adjusted to {state['input']}K"
action = "turn_on"
arg = "kelvin"
value = state["input"]
elif state["type"] == "volume_up":
description = "Volume increased"
action = state["type"]
elif state["type"] == "volume_down":
description = "Volume decreased"
action = state["type"]
elif state["type"] == "volume_set":
description = f"Volume adjusted to {state['input']}"
action = state["type"]
arg = "volume_level"
value = state["input"]
if state["input"] >= 1:
value = state["input"] / 100
elif state["type"] == "volume_mute":
description = f"Device muted"
action = state["type"]
arg = "is_volume_muted"
value = state["is_muted"]
elif state["type"] == "pause":
description = f"Device paused"
action = state["type"]
if domain == "media_player":
action = "media_pause"
if domain == "cover":
action = "stop_cover"
if domain == "vacuum":
action = "pause"
elif state["type"] == "continue":
description = f"Device resumed"
if domain == "media_player":
action = "media_play"
if domain == "vacuum":
action = "start"
else:
return f"{domain} {state['type']} function not yet supported"
if arg == "":
data = {
"entity_id": entity_id,
}
else:
data = {"entity_id": entity_id, arg: value}
url = f"{base_url}/api/services/{domain}/{action}"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=data, timeout=5) # Set 5 second timeout
logger.bind(tag=TAG).info(
f"Set status: {description}, url: {url}, return_code: {response.status_code}"
)
if response.status_code == 200:
return description
else:
return f"Set failed, error code: {response.status_code}"