control_device
Turn Home Assistant devices on or off by specifying entity IDs and desired states to manage smart home automation through natural language commands.
Instructions
Control a Home Assistant entity by turning it on or off.
Args:
entity_id: The Home Assistant entity ID to control (format: domain.entity)
state: The desired state ('on' or 'off')
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| entity_id | Yes | ||
| state | Yes |
Implementation Reference
- app/tools/device_controls.py:21-50 (handler)The handler function for the 'control_device' tool. It validates the entity_id and state, checks the Home Assistant token, determines the service (turn_on/turn_off), calls make_ha_request, and returns success or error message.async def control_device(entity_id: str, state: str) -> str: """Control a Home Assistant entity by turning it on or off. Args: entity_id: The Home Assistant entity ID to control (format: domain.entity) state: The desired state ('on' or 'off') """ # Basic validation if not entity_id or "." not in entity_id: return f"Invalid entity ID format: {entity_id}. Must be in format: domain.entity" state = state.lower() if state not in ["on", "off"]: return f"Invalid state: {state}. Must be 'on' or 'off'" # Check token if not HOME_ASSISTANT_TOKEN: return "Home Assistant token not configured. Set HOME_ASSISTANT_TOKEN environment variable." # Get domain from entity_id domain = entity_id.split(".")[0] service = "turn_on" if state == "on" else "turn_off" # Call the HA API result = await make_ha_request(domain, service, entity_id) if result["success"]: return f"Successfully turned {state} {entity_id}" else: return f"Failed to control {entity_id}: {result.get('error', 'Unknown error')}"
- app/tools/device_controls.py:11-19 (registration)The init_tools function registers the 'control_device' tool (along with others) using the FastMCP decorator fastmcp_instance.tool().def init_tools(fastmcp_instance: FastMCP): """Initialize the tools with a FastMCP instance.""" global mcp mcp = fastmcp_instance # Register tools with the FastMCP instance fastmcp_instance.tool()(control_device) fastmcp_instance.tool()(search_entities) fastmcp_instance.tool()(set_device_color) # Register set_device_color like other tools