From b81f15725fe359285e1c4257ab5ac3870f65ad52 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Thu, 9 Nov 2023 19:47:23 +0100 Subject: [PATCH] Update bond tests to use entity & device registry fixtures (#103708) --- tests/components/bond/test_button.py | 15 ++++---- tests/components/bond/test_cover.py | 9 ++--- tests/components/bond/test_fan.py | 31 ++++++++++------- tests/components/bond/test_init.py | 27 ++++++++------- tests/components/bond/test_light.py | 51 +++++++++++++++++----------- tests/components/bond/test_switch.py | 9 ++--- 6 files changed, 82 insertions(+), 60 deletions(-) diff --git a/tests/components/bond/test_button.py b/tests/components/bond/test_button.py index 9878050e6cf..6984831626d 100644 --- a/tests/components/bond/test_button.py +++ b/tests/components/bond/test_button.py @@ -6,7 +6,6 @@ from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRE from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from homeassistant.helpers.entity_registry import EntityRegistry from .common import patch_bond_action, patch_bond_device_state, setup_platform @@ -57,7 +56,10 @@ def light(name: str): } -async def test_entity_registry(hass: HomeAssistant) -> None: +async def test_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that the devices are registered in the entity registry.""" await setup_platform( hass, @@ -67,14 +69,13 @@ async def test_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["button.name_1_stop_actions"] + entity = entity_registry.entities["button.name_1_stop_actions"] assert entity.unique_id == "test-hub-id_test-device-id_stop" - entity = registry.entities["button.name_1_start_increasing_brightness"] + entity = entity_registry.entities["button.name_1_start_increasing_brightness"] assert entity.unique_id == "test-hub-id_test-device-id_startincreasingbrightness" - entity = registry.entities["button.name_1_start_decreasing_brightness"] + entity = entity_registry.entities["button.name_1_start_decreasing_brightness"] assert entity.unique_id == "test-hub-id_test-device-id_startdecreasingbrightness" - entity = registry.entities["button.name_1_start_dimmer"] + entity = entity_registry.entities["button.name_1_start_dimmer"] assert entity.unique_id == "test-hub-id_test-device-id_startdimmer" diff --git a/tests/components/bond/test_cover.py b/tests/components/bond/test_cover.py index 8f3e9c09922..e489f8550d6 100644 --- a/tests/components/bond/test_cover.py +++ b/tests/components/bond/test_cover.py @@ -23,7 +23,6 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow from .common import ( @@ -72,7 +71,10 @@ def tilt_shades(name: str): } -async def test_entity_registry(hass: HomeAssistant) -> None: +async def test_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that the devices are registered in the entity registry.""" await setup_platform( hass, @@ -82,8 +84,7 @@ async def test_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["cover.name_1"] + entity = entity_registry.entities["cover.name_1"] assert entity.unique_id == "test-hub-id_test-device-id" diff --git a/tests/components/bond/test_fan.py b/tests/components/bond/test_fan.py index f2fa109af22..db1c0fc787d 100644 --- a/tests/components/bond/test_fan.py +++ b/tests/components/bond/test_fan.py @@ -36,7 +36,6 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er -from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow from .common import ( @@ -81,7 +80,11 @@ async def turn_fan_on( await hass.async_block_till_done() -async def test_entity_registry(hass: HomeAssistant) -> None: +async def test_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +) -> None: """Tests that the devices are registered in the entity registry.""" await setup_platform( hass, @@ -91,11 +94,9 @@ async def test_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["fan.name_1"] + entity = entity_registry.entities["fan.name_1"] assert entity.unique_id == "test-hub-id_test-device-id" - device_registry = dr.async_get(hass) device = device_registry.async_get(entity.device_id) assert device.configuration_url == "http://some host" @@ -476,7 +477,11 @@ async def test_fan_available(hass: HomeAssistant) -> None: ) -async def test_setup_smart_by_bond_fan(hass: HomeAssistant) -> None: +async def test_setup_smart_by_bond_fan( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +) -> None: """Test setting up a fan without a hub.""" config_entry = await setup_platform( hass, @@ -491,10 +496,8 @@ async def test_setup_smart_by_bond_fan(hass: HomeAssistant) -> None: }, ) assert hass.states.get("fan.name_1") is not None - registry = er.async_get(hass) - entry = registry.async_get("fan.name_1") + entry = entity_registry.async_get("fan.name_1") assert entry.device_id is not None - device_registry = dr.async_get(hass) device = device_registry.async_get(entry.device_id) assert device is not None assert device.sw_version == "test-version" @@ -505,7 +508,11 @@ async def test_setup_smart_by_bond_fan(hass: HomeAssistant) -> None: await hass.async_block_till_done() -async def test_setup_hub_template_fan(hass: HomeAssistant) -> None: +async def test_setup_hub_template_fan( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +) -> None: """Test setting up a fan on a hub created from a template.""" config_entry = await setup_platform( hass, @@ -521,10 +528,8 @@ async def test_setup_hub_template_fan(hass: HomeAssistant) -> None: }, ) assert hass.states.get("fan.name_1") is not None - registry = er.async_get(hass) - entry = registry.async_get("fan.name_1") + entry = entity_registry.async_get("fan.name_1") assert entry.device_id is not None - device_registry = dr.async_get(hass) device = device_registry.async_get(entry.device_id) assert device is not None assert device.sw_version is None diff --git a/tests/components/bond/test_init.py b/tests/components/bond/test_init.py index 33919219301..92c11028173 100644 --- a/tests/components/bond/test_init.py +++ b/tests/components/bond/test_init.py @@ -12,7 +12,6 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_ASSUMED_STATE, CONF_ACCESS_TOKEN, CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er -from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.setup import async_setup_component from .common import ( @@ -82,6 +81,7 @@ async def test_async_setup_raises_fails_if_auth_fails(hass: HomeAssistant) -> No async def test_async_setup_entry_sets_up_hub_and_supported_domains( hass: HomeAssistant, + device_registry: dr.DeviceRegistry, ) -> None: """Test that configuring entry sets up cover domain.""" config_entry = MockConfigEntry( @@ -112,7 +112,6 @@ async def test_async_setup_entry_sets_up_hub_and_supported_domains( assert config_entry.unique_id == "ZXXX12345" # verify hub device is registered correctly - device_registry = dr.async_get(hass) hub = device_registry.async_get_device(identifiers={(DOMAIN, "ZXXX12345")}) assert hub.name == "bond-name" assert hub.manufacturer == "Olibra" @@ -153,7 +152,9 @@ async def test_unload_config_entry(hass: HomeAssistant) -> None: assert config_entry.state is ConfigEntryState.NOT_LOADED -async def test_old_identifiers_are_removed(hass: HomeAssistant) -> None: +async def test_old_identifiers_are_removed( + hass: HomeAssistant, device_registry: dr.DeviceRegistry +) -> None: """Test we remove the old non-unique identifiers.""" config_entry = MockConfigEntry( domain=DOMAIN, @@ -163,7 +164,6 @@ async def test_old_identifiers_are_removed(hass: HomeAssistant) -> None: old_identifers = (DOMAIN, "device_id") new_identifiers = (DOMAIN, "ZXXX12345", "device_id") - device_registry = dr.async_get(hass) device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, identifiers={old_identifers}, @@ -201,7 +201,9 @@ async def test_old_identifiers_are_removed(hass: HomeAssistant) -> None: assert device_registry.async_get_device(identifiers={new_identifiers}) is not None -async def test_smart_by_bond_device_suggested_area(hass: HomeAssistant) -> None: +async def test_smart_by_bond_device_suggested_area( + hass: HomeAssistant, device_registry: dr.DeviceRegistry +) -> None: """Test we can setup a smart by bond device and get the suggested area.""" config_entry = MockConfigEntry( domain=DOMAIN, @@ -238,13 +240,14 @@ async def test_smart_by_bond_device_suggested_area(hass: HomeAssistant) -> None: assert config_entry.state is ConfigEntryState.LOADED assert config_entry.unique_id == "KXXX12345" - device_registry = dr.async_get(hass) device = device_registry.async_get_device(identifiers={(DOMAIN, "KXXX12345")}) assert device is not None assert device.suggested_area == "Den" -async def test_bridge_device_suggested_area(hass: HomeAssistant) -> None: +async def test_bridge_device_suggested_area( + hass: HomeAssistant, device_registry: dr.DeviceRegistry +) -> None: """Test we can setup a bridge bond device and get the suggested area.""" config_entry = MockConfigEntry( domain=DOMAIN, @@ -284,14 +287,16 @@ async def test_bridge_device_suggested_area(hass: HomeAssistant) -> None: assert config_entry.state is ConfigEntryState.LOADED assert config_entry.unique_id == "ZXXX12345" - device_registry = dr.async_get(hass) device = device_registry.async_get_device(identifiers={(DOMAIN, "ZXXX12345")}) assert device is not None assert device.suggested_area == "Office" async def test_device_remove_devices( - hass: HomeAssistant, hass_ws_client: WebSocketGenerator + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, ) -> None: """Test we can only remove a device that no longer exists.""" assert await async_setup_component(hass, "config", {}) @@ -304,11 +309,9 @@ async def test_device_remove_devices( bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["fan.name_1"] + entity = entity_registry.entities["fan.name_1"] assert entity.unique_id == "test-hub-id_test-device-id" - device_registry = dr.async_get(hass) device_entry = device_registry.async_get(entity.device_id) assert ( await remove_device( diff --git a/tests/components/bond/test_light.py b/tests/components/bond/test_light.py index 6cbd43b221b..10395f395dd 100644 --- a/tests/components/bond/test_light.py +++ b/tests/components/bond/test_light.py @@ -32,7 +32,6 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er -from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow from .common import ( @@ -153,7 +152,10 @@ def light_brightness_increase_decrease_only(name: str): } -async def test_fan_entity_registry(hass: HomeAssistant) -> None: +async def test_fan_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that fan with light devices are registered in the entity registry.""" await setup_platform( hass, @@ -163,12 +165,14 @@ async def test_fan_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["light.fan_name"] + entity = entity_registry.entities["light.fan_name"] assert entity.unique_id == "test-hub-id_test-device-id" -async def test_fan_up_light_entity_registry(hass: HomeAssistant) -> None: +async def test_fan_up_light_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that fan with up light devices are registered in the entity registry.""" await setup_platform( hass, @@ -178,12 +182,14 @@ async def test_fan_up_light_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["light.fan_name_up_light"] + entity = entity_registry.entities["light.fan_name_up_light"] assert entity.unique_id == "test-hub-id_test-device-id_up_light" -async def test_fan_down_light_entity_registry(hass: HomeAssistant) -> None: +async def test_fan_down_light_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that fan with down light devices are registered in the entity registry.""" await setup_platform( hass, @@ -193,12 +199,14 @@ async def test_fan_down_light_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["light.fan_name_down_light"] + entity = entity_registry.entities["light.fan_name_down_light"] assert entity.unique_id == "test-hub-id_test-device-id_down_light" -async def test_fireplace_entity_registry(hass: HomeAssistant) -> None: +async def test_fireplace_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that flame fireplace devices are registered in the entity registry.""" await setup_platform( hass, @@ -208,12 +216,14 @@ async def test_fireplace_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["light.fireplace_name"] + entity = entity_registry.entities["light.fireplace_name"] assert entity.unique_id == "test-hub-id_test-device-id" -async def test_fireplace_with_light_entity_registry(hass: HomeAssistant) -> None: +async def test_fireplace_with_light_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that flame+light devices are registered in the entity registry.""" await setup_platform( hass, @@ -223,14 +233,16 @@ async def test_fireplace_with_light_entity_registry(hass: HomeAssistant) -> None bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity_flame = registry.entities["light.fireplace_name"] + entity_flame = entity_registry.entities["light.fireplace_name"] assert entity_flame.unique_id == "test-hub-id_test-device-id" - entity_light = registry.entities["light.fireplace_name_light"] + entity_light = entity_registry.entities["light.fireplace_name_light"] assert entity_light.unique_id == "test-hub-id_test-device-id_light" -async def test_light_entity_registry(hass: HomeAssistant) -> None: +async def test_light_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests lights are registered in the entity registry.""" await setup_platform( hass, @@ -240,8 +252,7 @@ async def test_light_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["light.light_name"] + entity = entity_registry.entities["light.light_name"] assert entity.unique_id == "test-hub-id_test-device-id" diff --git a/tests/components/bond/test_switch.py b/tests/components/bond/test_switch.py index 06d2e0b4c64..1ab9ef2165c 100644 --- a/tests/components/bond/test_switch.py +++ b/tests/components/bond/test_switch.py @@ -14,7 +14,6 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er -from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util import utcnow from .common import ( @@ -33,7 +32,10 @@ def generic_device(name: str): return {"name": name, "type": DeviceType.GENERIC_DEVICE} -async def test_entity_registry(hass: HomeAssistant) -> None: +async def test_entity_registry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: """Tests that the devices are registered in the entity registry.""" await setup_platform( hass, @@ -43,8 +45,7 @@ async def test_entity_registry(hass: HomeAssistant) -> None: bond_device_id="test-device-id", ) - registry: EntityRegistry = er.async_get(hass) - entity = registry.entities["switch.name_1"] + entity = entity_registry.entities["switch.name_1"] assert entity.unique_id == "test-hub-id_test-device-id"