diff --git a/custom_components/mcp_bridge/__init__.py b/custom_components/mcp_bridge/__init__.py index a50c8e0..19750a1 100644 --- a/custom_components/mcp_bridge/__init__.py +++ b/custom_components/mcp_bridge/__init__.py @@ -16,13 +16,12 @@ from homeassistant.core import ( SupportsResponse, ) from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import area_registry as ar from homeassistant.helpers.typing import ConfigType _LOGGER = logging.getLogger(__name__) DOMAIN = "mcp_bridge" - -# Label to mark scripts as MCP-accessible MCP_ACCESSIBLE_LABEL = "mcp_accessible" @@ -31,16 +30,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: _LOGGER.info("Setting up MCP Bridge integration") async def get_exposed_entities(call: ServiceCall) -> ServiceResponse: - """Get all entities exposed to conversation / voice assistants.""" + """Return entities exposed to conversation.""" entity_reg = er.async_get(hass) - area_reg = hass.helpers.area_registry.async_get(hass) + area_reg = ar.async_get(hass) exposed_entities: list[dict[str, Any]] = [] for state in hass.states.async_all(): - entity_id = state.entity_id - entity_entry = entity_reg.async_get(entity_id) - + entity_entry = entity_reg.async_get(state.entity_id) if not entity_entry: continue @@ -57,13 +54,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: exposed_entities.append( { - "entity_id": entity_id, + "entity_id": state.entity_id, "state": state.state, "friendly_name": state.attributes.get( - "friendly_name", entity_id + "friendly_name", state.entity_id ), "area": area_name, - "domain": entity_id.split(".")[0], + "domain": state.entity_id.split(".", 1)[0], "device_class": state.attributes.get("device_class"), "supported_features": state.attributes.get( "supported_features", 0 @@ -74,8 +71,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: } ) - _LOGGER.debug("Found %s exposed entities", len(exposed_entities)) - return ServiceResponse( { "entities": exposed_entities, @@ -84,14 +79,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) async def get_exposed_scripts(call: ServiceCall) -> ServiceResponse: - """Get all scripts marked as MCP-accessible.""" + """Return scripts marked as MCP-accessible.""" entity_reg = er.async_get(hass) - exposed_scripts: list[dict[str, Any]] = [] for entity_id in hass.states.async_entity_ids("script"): entity_entry = entity_reg.async_get(entity_id) - if not entity_entry: continue @@ -113,8 +106,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: } ) - _LOGGER.debug("Found %s exposed scripts", len(exposed_scripts)) - return ServiceResponse( { "scripts": exposed_scripts, @@ -123,7 +114,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) async def get_entity_metadata(call: ServiceCall) -> ServiceResponse: - """Get detailed metadata for a specific entity.""" + """Return detailed metadata for a single entity.""" entity_id = call.data.get(CONF_ENTITY_ID) if not entity_id: @@ -133,17 +124,17 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) entity_reg = er.async_get(hass) - area_reg = hass.helpers.area_registry.async_get(hass) + area_reg = ar.async_get(hass) - entity_entry = entity_reg.async_get(entity_id) state = hass.states.get(entity_id) - if not state: return ServiceResponse( {"error": f"Entity {entity_id} not found"}, success=False, ) + entity_entry = entity_reg.async_get(entity_id) + area_name = None if entity_entry and entity_entry.area_id: area = area_reg.async_get_area(entity_entry.area_id) @@ -157,7 +148,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: "friendly_name", entity_id ), "area": area_name, - "domain": entity_id.split(".")[0], + "domain": entity_id.split(".", 1)[0], "device_class": state.attributes.get("device_class"), "supported_features": state.attributes.get( "supported_features", 0 @@ -197,5 +188,4 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) _LOGGER.info("MCP Bridge services registered successfully") - return True