diff --git a/homeassistant/components/cloud/alexa_config.py b/homeassistant/components/cloud/alexa_config.py index 212cbb26e0a..6645c4f9f60 100644 --- a/homeassistant/components/cloud/alexa_config.py +++ b/homeassistant/components/cloud/alexa_config.py @@ -29,6 +29,7 @@ from homeassistant.components.homeassistant.exposed_entities import ( from homeassistant.components.sensor import SensorDeviceClass from homeassistant.const import CLOUD_NEVER_EXPOSED_ENTITIES from homeassistant.core import HomeAssistant, callback, split_entity_id +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er, start from homeassistant.helpers.entity import get_device_class from homeassistant.helpers.event import async_call_later @@ -104,7 +105,11 @@ def entity_supported(hass: HomeAssistant, entity_id: str) -> bool: if domain in SUPPORTED_DOMAINS: return True - device_class = get_device_class(hass, entity_id) + try: + device_class = get_device_class(hass, entity_id) + except HomeAssistantError: + # The entity no longer exists + return False if ( domain == "binary_sensor" and device_class in SUPPORTED_BINARY_SENSOR_DEVICE_CLASSES diff --git a/homeassistant/components/cloud/http_api.py b/homeassistant/components/cloud/http_api.py index b0878fd24aa..f5d5c98fe1a 100644 --- a/homeassistant/components/cloud/http_api.py +++ b/homeassistant/components/cloud/http_api.py @@ -28,7 +28,6 @@ from homeassistant.components.http.data_validator import RequestDataValidator from homeassistant.const import CLOUD_NEVER_EXPOSED_ENTITIES from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.util.location import async_detect_location_info @@ -569,15 +568,14 @@ async def google_assistant_get( """Get data for a single google assistant entity.""" cloud = hass.data[DOMAIN] gconf = await cloud.client.get_google_config() - entity_registry = er.async_get(hass) entity_id: str = msg["entity_id"] state = hass.states.get(entity_id) - if not entity_registry.async_is_registered(entity_id) or not state: + if not state: connection.send_error( msg["id"], websocket_api.const.ERR_NOT_FOUND, - f"{entity_id} unknown or not in the entity registry", + f"{entity_id} unknown", ) return @@ -684,17 +682,8 @@ async def alexa_get( msg: dict[str, Any], ) -> None: """Get data for a single alexa entity.""" - entity_registry = er.async_get(hass) entity_id: str = msg["entity_id"] - if not entity_registry.async_is_registered(entity_id): - connection.send_error( - msg["id"], - websocket_api.const.ERR_NOT_FOUND, - f"{entity_id} not in the entity registry", - ) - return - if entity_id in CLOUD_NEVER_EXPOSED_ENTITIES or not entity_supported_by_alexa( hass, entity_id ): diff --git a/tests/components/cloud/test_http_api.py b/tests/components/cloud/test_http_api.py index 8911b34055a..ff79fd1ea77 100644 --- a/tests/components/cloud/test_http_api.py +++ b/tests/components/cloud/test_http_api.py @@ -820,7 +820,7 @@ async def test_get_google_entity( assert not response["success"] assert response["error"] == { "code": "not_found", - "message": "light.kitchen unknown or not in the entity registry", + "message": "light.kitchen unknown", } # Test getting a blocked entity @@ -841,9 +841,6 @@ async def test_get_google_entity( entity_registry.async_get_or_create( "light", "test", "unique", suggested_object_id="kitchen" ) - entity_registry.async_get_or_create( - "cover", "test", "unique", suggested_object_id="garage" - ) hass.states.async_set("light.kitchen", "on") hass.states.async_set("cover.garage", "open", {"device_class": "garage"}) @@ -991,10 +988,18 @@ async def test_get_alexa_entity( {"type": "cloud/alexa/entities/get", "entity_id": "light.kitchen"} ) response = await client.receive_json() + assert response["success"] + assert response["result"] is None + + # Test getting an unknown sensor + await client.send_json_auto_id( + {"type": "cloud/alexa/entities/get", "entity_id": "sensor.temperature"} + ) + response = await client.receive_json() assert not response["success"] assert response["error"] == { - "code": "not_found", - "message": "light.kitchen not in the entity registry", + "code": "not_supported", + "message": "sensor.temperature not supported by Alexa", } # Test getting a blocked entity