From 470c3a0f5f4c8d21799f04d75f19921e39a4ed42 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 3 May 2023 16:50:43 +0200 Subject: [PATCH] Use exposed_entities API in cloud tests (#92408) --- tests/components/cloud/test_alexa_config.py | 75 ++++++++++---------- tests/components/cloud/test_google_config.py | 69 ++++++++---------- tests/components/cloud/test_http_api.py | 4 +- 3 files changed, 69 insertions(+), 79 deletions(-) diff --git a/tests/components/cloud/test_alexa_config.py b/tests/components/cloud/test_alexa_config.py index 134838ff1ce..4d1f4d457df 100644 --- a/tests/components/cloud/test_alexa_config.py +++ b/tests/components/cloud/test_alexa_config.py @@ -16,9 +16,11 @@ from homeassistant.components.homeassistant.exposed_entities import ( DATA_EXPOSED_ENTITIES, ExposedEntities, async_expose_entity, + async_get_entity_settings, ) from homeassistant.const import EntityCategory 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.setup import async_setup_component @@ -602,20 +604,23 @@ async def test_alexa_config_migrate_expose_entity_prefs( ) await conf.async_initialize() - entity_exposed = entity_registry.async_get(entity_exposed.entity_id) - assert entity_exposed.options == {"cloud.alexa": {"should_expose": True}} - - entity_migrated = entity_registry.async_get(entity_migrated.entity_id) - assert entity_migrated.options == {"cloud.alexa": {"should_expose": False}} - - entity_config = entity_registry.async_get(entity_config.entity_id) - assert entity_config.options == {"cloud.alexa": {"should_expose": False}} - - entity_default = entity_registry.async_get(entity_default.entity_id) - assert entity_default.options == {"cloud.alexa": {"should_expose": True}} - - entity_blocked = entity_registry.async_get(entity_blocked.entity_id) - assert entity_blocked.options == {"cloud.alexa": {"should_expose": False}} + with pytest.raises(HomeAssistantError): + async_get_entity_settings(hass, "light.unknown") + assert async_get_entity_settings(hass, entity_migrated.entity_id) == { + "cloud.alexa": {"should_expose": False} + } + assert async_get_entity_settings(hass, entity_migrated.entity_id) == { + "cloud.alexa": {"should_expose": False} + } + assert async_get_entity_settings(hass, entity_config.entity_id) == { + "cloud.alexa": {"should_expose": False} + } + assert async_get_entity_settings(hass, entity_default.entity_id) == { + "cloud.alexa": {"should_expose": True} + } + assert async_get_entity_settings(hass, entity_blocked.entity_id) == { + "cloud.alexa": {"should_expose": False} + } async def test_alexa_config_migrate_expose_entity_prefs_default_none( @@ -646,8 +651,9 @@ async def test_alexa_config_migrate_expose_entity_prefs_default_none( ) await conf.async_initialize() - entity_default = entity_registry.async_get(entity_default.entity_id) - assert entity_default.options == {"cloud.alexa": {"should_expose": True}} + assert async_get_entity_settings(hass, entity_default.entity_id) == { + "cloud.alexa": {"should_expose": True} + } async def test_alexa_config_migrate_expose_entity_prefs_default( @@ -723,26 +729,21 @@ async def test_alexa_config_migrate_expose_entity_prefs_default( ) await conf.async_initialize() - binary_sensor_supported = entity_registry.async_get( - binary_sensor_supported.entity_id - ) - assert binary_sensor_supported.options == {"cloud.alexa": {"should_expose": True}} - - binary_sensor_unsupported = entity_registry.async_get( - binary_sensor_unsupported.entity_id - ) - assert binary_sensor_unsupported.options == { + assert async_get_entity_settings(hass, binary_sensor_supported.entity_id) == { + "cloud.alexa": {"should_expose": True} + } + assert async_get_entity_settings(hass, binary_sensor_unsupported.entity_id) == { + "cloud.alexa": {"should_expose": False} + } + assert async_get_entity_settings(hass, light.entity_id) == { + "cloud.alexa": {"should_expose": True} + } + assert async_get_entity_settings(hass, sensor_supported.entity_id) == { + "cloud.alexa": {"should_expose": True} + } + assert async_get_entity_settings(hass, sensor_unsupported.entity_id) == { + "cloud.alexa": {"should_expose": False} + } + assert async_get_entity_settings(hass, water_heater.entity_id) == { "cloud.alexa": {"should_expose": False} } - - light = entity_registry.async_get(light.entity_id) - assert light.options == {"cloud.alexa": {"should_expose": True}} - - sensor_supported = entity_registry.async_get(sensor_supported.entity_id) - assert sensor_supported.options == {"cloud.alexa": {"should_expose": True}} - - sensor_unsupported = entity_registry.async_get(sensor_unsupported.entity_id) - assert sensor_unsupported.options == {"cloud.alexa": {"should_expose": False}} - - water_heater = entity_registry.async_get(water_heater.entity_id) - assert water_heater.options == {"cloud.alexa": {"should_expose": False}} diff --git a/tests/components/cloud/test_google_config.py b/tests/components/cloud/test_google_config.py index c6e08f9e152..0738bc6b029 100644 --- a/tests/components/cloud/test_google_config.py +++ b/tests/components/cloud/test_google_config.py @@ -19,9 +19,11 @@ from homeassistant.components.homeassistant.exposed_entities import ( DATA_EXPOSED_ENTITIES, ExposedEntities, async_expose_entity, + async_get_entity_settings, ) from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EntityCategory from homeassistant.core import CoreState, HomeAssistant, State +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component from homeassistant.util.dt import utcnow @@ -553,27 +555,24 @@ async def test_google_config_migrate_expose_entity_prefs( ) await conf.async_initialize() - entity_exposed = entity_registry.async_get(entity_exposed.entity_id) - assert entity_exposed.options == {"cloud.google_assistant": {"should_expose": True}} - - entity_migrated = entity_registry.async_get(entity_migrated.entity_id) - assert entity_migrated.options == { + with pytest.raises(HomeAssistantError): + async_get_entity_settings(hass, "light.unknown") + assert async_get_entity_settings(hass, entity_exposed.entity_id) == { + "cloud.google_assistant": {"should_expose": True} + } + assert async_get_entity_settings(hass, entity_migrated.entity_id) == { "cloud.google_assistant": {"should_expose": False} } - - entity_no_2fa_exposed = entity_registry.async_get(entity_no_2fa_exposed.entity_id) - assert entity_no_2fa_exposed.options == { + assert async_get_entity_settings(hass, entity_no_2fa_exposed.entity_id) == { "cloud.google_assistant": {"disable_2fa": True, "should_expose": True} } - - entity_config = entity_registry.async_get(entity_config.entity_id) - assert entity_config.options == {"cloud.google_assistant": {"should_expose": False}} - - entity_default = entity_registry.async_get(entity_default.entity_id) - assert entity_default.options == {"cloud.google_assistant": {"should_expose": True}} - - entity_blocked = entity_registry.async_get(entity_blocked.entity_id) - assert entity_blocked.options == { + assert async_get_entity_settings(hass, entity_config.entity_id) == { + "cloud.google_assistant": {"should_expose": False} + } + assert async_get_entity_settings(hass, entity_default.entity_id) == { + "cloud.google_assistant": {"should_expose": True} + } + assert async_get_entity_settings(hass, entity_blocked.entity_id) == { "cloud.google_assistant": {"should_expose": False} } @@ -605,8 +604,9 @@ async def test_google_config_migrate_expose_entity_prefs_default_none( ) await conf.async_initialize() - entity_default = entity_registry.async_get(entity_default.entity_id) - assert entity_default.options == {"cloud.google_assistant": {"should_expose": True}} + assert async_get_entity_settings(hass, entity_default.entity_id) == { + "cloud.google_assistant": {"should_expose": True} + } async def test_google_config_migrate_expose_entity_prefs_default( @@ -681,32 +681,21 @@ async def test_google_config_migrate_expose_entity_prefs_default( ) await conf.async_initialize() - binary_sensor_supported = entity_registry.async_get( - binary_sensor_supported.entity_id - ) - assert binary_sensor_supported.options == { + assert async_get_entity_settings(hass, binary_sensor_supported.entity_id) == { "cloud.google_assistant": {"should_expose": True} } - - binary_sensor_unsupported = entity_registry.async_get( - binary_sensor_unsupported.entity_id - ) - assert binary_sensor_unsupported.options == { + assert async_get_entity_settings(hass, binary_sensor_unsupported.entity_id) == { "cloud.google_assistant": {"should_expose": False} } - - light = entity_registry.async_get(light.entity_id) - assert light.options == {"cloud.google_assistant": {"should_expose": True}} - - sensor_supported = entity_registry.async_get(sensor_supported.entity_id) - assert sensor_supported.options == { + assert async_get_entity_settings(hass, light.entity_id) == { "cloud.google_assistant": {"should_expose": True} } - - sensor_unsupported = entity_registry.async_get(sensor_unsupported.entity_id) - assert sensor_unsupported.options == { + assert async_get_entity_settings(hass, sensor_supported.entity_id) == { + "cloud.google_assistant": {"should_expose": True} + } + assert async_get_entity_settings(hass, sensor_unsupported.entity_id) == { + "cloud.google_assistant": {"should_expose": False} + } + assert async_get_entity_settings(hass, water_heater.entity_id) == { "cloud.google_assistant": {"should_expose": False} } - - water_heater = entity_registry.async_get(water_heater.entity_id) - assert water_heater.options == {"cloud.google_assistant": {"should_expose": False}} diff --git a/tests/components/cloud/test_http_api.py b/tests/components/cloud/test_http_api.py index 3e7523cc02c..8911b34055a 100644 --- a/tests/components/cloud/test_http_api.py +++ b/tests/components/cloud/test_http_api.py @@ -1060,8 +1060,8 @@ async def test_update_alexa_entity( response = await client.receive_json() assert response["success"] - assert entity_registry.async_get(entry.entity_id).options["cloud.alexa"] == { - "should_expose": False + assert exposed_entities.async_get_entity_settings(hass, entry.entity_id) == { + "cloud.alexa": {"should_expose": False} }