From c7b4d4f3614230969000a92c7c36ce9e9c1d0562 Mon Sep 17 00:00:00 2001 From: Erik Montnemery <erik@montnemery.com> Date: Thu, 10 Aug 2023 19:28:16 +0200 Subject: [PATCH] Adjust helpers tests which create devices (#98214) --- tests/helpers/test_entity.py | 1 + tests/helpers/test_entity_platform.py | 25 +++++++++++++++++-------- tests/helpers/test_entity_registry.py | 7 +++++++ tests/helpers/test_intent.py | 8 +++++++- tests/helpers/test_template.py | 7 +++++++ 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index 0d9ee76ac62..200b0230adb 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -1360,6 +1360,7 @@ async def test_friendly_name_updated( platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index 3eaad662d8b..77914a49894 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -1062,8 +1062,10 @@ async def test_entity_registry_updates_invalid_entity_id(hass: HomeAssistant) -> async def test_device_info_called(hass: HomeAssistant) -> None: """Test device info is forwarded correctly.""" registry = dr.async_get(hass) + config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) via = registry.async_get_or_create( - config_entry_id="123", + config_entry_id=config_entry.entry_id, connections=set(), identifiers={("hue", "via-id")}, manufacturer="manufacturer", @@ -1098,7 +1100,6 @@ async def test_device_info_called(hass: HomeAssistant) -> None: return True platform = MockPlatform(async_setup_entry=async_setup_entry) - config_entry = MockConfigEntry(entry_id="super-mock-id") entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1126,8 +1127,10 @@ async def test_device_info_called(hass: HomeAssistant) -> None: async def test_device_info_not_overrides(hass: HomeAssistant) -> None: """Test device info is forwarded correctly.""" registry = dr.async_get(hass) + config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) device = registry.async_get_or_create( - config_entry_id="bla", + config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "abcd")}, manufacturer="test-manufacturer", model="test-model", @@ -1154,7 +1157,6 @@ async def test_device_info_not_overrides(hass: HomeAssistant) -> None: return True platform = MockPlatform(async_setup_entry=async_setup_entry) - config_entry = MockConfigEntry(entry_id="super-mock-id") entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1176,8 +1178,10 @@ async def test_device_info_homeassistant_url( ) -> None: """Test device info with homeassistant URL.""" registry = dr.async_get(hass) + config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) registry.async_get_or_create( - config_entry_id="123", + config_entry_id=config_entry.entry_id, connections=set(), identifiers={("mqtt", "via-id")}, manufacturer="manufacturer", @@ -1201,7 +1205,6 @@ async def test_device_info_homeassistant_url( return True platform = MockPlatform(async_setup_entry=async_setup_entry) - config_entry = MockConfigEntry(entry_id="super-mock-id") entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1222,8 +1225,10 @@ async def test_device_info_change_to_no_url( ) -> None: """Test device info changes to no URL.""" registry = dr.async_get(hass) + config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) registry.async_get_or_create( - config_entry_id="123", + config_entry_id=config_entry.entry_id, connections=set(), identifiers={("mqtt", "via-id")}, manufacturer="manufacturer", @@ -1248,7 +1253,6 @@ async def test_device_info_change_to_no_url( return True platform = MockPlatform(async_setup_entry=async_setup_entry) - config_entry = MockConfigEntry(entry_id="super-mock-id") entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1304,6 +1308,7 @@ async def test_entity_disabled_by_device(hass: HomeAssistant) -> None: platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id", domain=DOMAIN) + config_entry.add_to_hass(hass) entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1621,6 +1626,7 @@ async def test_entity_name_influences_entity_id( platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1690,6 +1696,7 @@ async def test_translated_entity_name_influences_entity_id( platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1773,6 +1780,7 @@ async def test_translated_device_class_name_influences_entity_id( platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id") + config_entry.add_to_hass(hass) entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) @@ -1878,6 +1886,7 @@ async def test_device_type_error_checking( config_entry = MockConfigEntry( title="Mock Config Entry Title", entry_id="super-mock-id" ) + config_entry.add_to_hass(hass) entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) diff --git a/tests/helpers/test_entity_registry.py b/tests/helpers/test_entity_registry.py index 57622d330d9..f62addb9a64 100644 --- a/tests/helpers/test_entity_registry.py +++ b/tests/helpers/test_entity_registry.py @@ -1017,6 +1017,7 @@ async def test_remove_device_removes_entities( ) -> None: """Test that we remove entities tied to a device.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, @@ -1046,7 +1047,9 @@ async def test_remove_config_entry_from_device_removes_entities( ) -> None: """Test that we remove entities tied to a device when config entry is removed.""" config_entry_1 = MockConfigEntry(domain="hue") + config_entry_1.add_to_hass(hass) config_entry_2 = MockConfigEntry(domain="device_tracker") + config_entry_2.add_to_hass(hass) # Create device with two config entries device_registry.async_get_or_create( @@ -1112,7 +1115,9 @@ async def test_remove_config_entry_from_device_removes_entities_2( ) -> None: """Test that we don't remove entities with no config entry when device is modified.""" config_entry_1 = MockConfigEntry(domain="hue") + config_entry_1.add_to_hass(hass) config_entry_2 = MockConfigEntry(domain="device_tracker") + config_entry_2.add_to_hass(hass) # Create device with two config entries device_registry.async_get_or_create( @@ -1155,6 +1160,7 @@ async def test_update_device_race( ) -> None: """Test race when a device is created, updated and removed.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) # Create device device_entry = device_registry.async_get_or_create( @@ -1331,6 +1337,7 @@ async def test_disabled_entities_excluded_from_entity_list( ) -> None: """Test that disabled entities are excluded from async_entries_for_device.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, diff --git a/tests/helpers/test_intent.py b/tests/helpers/test_intent.py index 98e93785f58..8d473338058 100644 --- a/tests/helpers/test_intent.py +++ b/tests/helpers/test_intent.py @@ -18,6 +18,8 @@ from homeassistant.helpers import ( ) from homeassistant.setup import async_setup_component +from tests.common import MockConfigEntry + class MockIntentHandler(intent.IntentHandler): """Provide a mock intent handler.""" @@ -116,11 +118,15 @@ async def test_match_device_area( entity_registry: er.EntityRegistry, ) -> None: """Test async_match_state with a device in an area.""" + config_entry = MockConfigEntry() + config_entry.add_to_hass(hass) area_kitchen = area_registry.async_get_or_create("kitchen") area_bedroom = area_registry.async_get_or_create("bedroom") kitchen_device = device_registry.async_get_or_create( - config_entry_id="1234", connections=set(), identifiers={("demo", "id-1234")} + config_entry_id=config_entry.entry_id, + connections=set(), + identifiers={("demo", "id-1234")}, ) device_registry.async_update_device(kitchen_device.id, area_id=area_kitchen.id) diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 9994f0cadc1..d14496d321e 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -2709,6 +2709,7 @@ async def test_device_entities( ) -> None: """Test device_entities function.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) # Test non existing device ids info = render_to_info(hass, "{{ device_entities('abc123') }}") @@ -2858,6 +2859,7 @@ async def test_device_id( ) -> None: """Test device_id function.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, @@ -2903,6 +2905,7 @@ async def test_device_attr( ) -> None: """Test device_attr and is_device_attr functions.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) # Test non existing device ids (device_attr) info = render_to_info(hass, "{{ device_attr('abc123', 'id') }}") @@ -3049,6 +3052,7 @@ async def test_area_id( ) -> None: """Test area_id function.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) # Test non existing entity id info = render_to_info(hass, "{{ area_id('sensor.fake') }}") @@ -3155,6 +3159,7 @@ async def test_area_name( ) -> None: """Test area_name function.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) # Test non existing entity id info = render_to_info(hass, "{{ area_name('sensor.fake') }}") @@ -3236,6 +3241,7 @@ async def test_area_entities( ) -> None: """Test area_entities function.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) # Test non existing device id info = render_to_info(hass, "{{ area_entities('deadbeef') }}") @@ -3290,6 +3296,7 @@ async def test_area_devices( ) -> None: """Test area_devices function.""" config_entry = MockConfigEntry(domain="light") + config_entry.add_to_hass(hass) # Test non existing device id info = render_to_info(hass, "{{ area_devices('deadbeef') }}") -- GitLab