From ee78864b0568330a8118dcf54e9d461b55134516 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 1 Mar 2023 16:54:00 +0100 Subject: [PATCH] Adjust entity registry access in homekit tests (#88959) --- tests/components/homekit/conftest.py | 14 +- tests/components/homekit/test_aidmanager.py | 55 +++--- tests/components/homekit/test_config_flow.py | 26 +-- tests/components/homekit/test_diagnostics.py | 7 +- tests/components/homekit/test_homekit.py | 162 ++++++++++-------- tests/components/homekit/test_init.py | 5 +- .../components/homekit/test_type_triggers.py | 10 +- 7 files changed, 149 insertions(+), 130 deletions(-) diff --git a/tests/components/homekit/conftest.py b/tests/components/homekit/conftest.py index 92cd46b51e6..fe151c902cb 100644 --- a/tests/components/homekit/conftest.py +++ b/tests/components/homekit/conftest.py @@ -10,7 +10,7 @@ from homeassistant.components.homekit.accessories import HomeDriver from homeassistant.components.homekit.const import BRIDGE_NAME, EVENT_HOMEKIT_CHANGED from homeassistant.components.homekit.iidmanager import AccessoryIIDStorage -from tests.common import async_capture_events, mock_device_registry, mock_registry +from tests.common import async_capture_events @pytest.fixture @@ -103,18 +103,6 @@ def events(hass): return async_capture_events(hass, EVENT_HOMEKIT_CHANGED) -@pytest.fixture(name="device_reg") -def device_reg_fixture(hass): - """Return an empty, loaded, registry.""" - return mock_device_registry(hass) - - -@pytest.fixture(name="entity_reg") -def entity_reg_fixture(hass): - """Return an empty, loaded, registry.""" - return mock_registry(hass) - - @pytest.fixture def demo_cleanup(hass): """Clean up device tracker demo file.""" diff --git a/tests/components/homekit/test_aidmanager.py b/tests/components/homekit/test_aidmanager.py index 1dafe7a11ad..64a44cd38a9 100644 --- a/tests/components/homekit/test_aidmanager.py +++ b/tests/components/homekit/test_aidmanager.py @@ -3,7 +3,6 @@ import os from unittest.mock import patch from fnvhash import fnv1a_32 -import pytest from homeassistant.components.homekit.aidmanager import ( AccessoryAidStorage, @@ -11,39 +10,31 @@ from homeassistant.components.homekit.aidmanager import ( get_system_unique_id, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers import device_registry +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.storage import STORAGE_DIR -from tests.common import MockConfigEntry, mock_device_registry, mock_registry +from tests.common import MockConfigEntry -@pytest.fixture -def device_reg(hass): - """Return an empty, loaded, registry.""" - return mock_device_registry(hass) - - -@pytest.fixture -def entity_reg(hass): - """Return an empty, loaded, registry.""" - return mock_registry(hass) - - -async def test_aid_generation(hass: HomeAssistant, device_reg, entity_reg) -> None: +async def test_aid_generation( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: """Test generating aids.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - light_ent = entity_reg.async_get_or_create( + light_ent = entity_registry.async_get_or_create( "light", "device", "unique_id", device_id=device_entry.id ) - light_ent2 = entity_reg.async_get_or_create( + light_ent2 = entity_registry.async_get_or_create( "light", "device", "other_unique_id", device_id=device_entry.id ) - remote_ent = entity_reg.async_get_or_create( + remote_ent = entity_registry.async_get_or_create( "remote", "device", "unique_id", device_id=device_entry.id ) hass.states.async_set(light_ent.entity_id, "on") @@ -99,13 +90,17 @@ async def test_aid_generation(hass: HomeAssistant, device_reg, entity_reg) -> No ) -async def test_no_aid_collision(hass: HomeAssistant, device_reg, entity_reg) -> None: +async def test_no_aid_collision( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: """Test generating aids.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) with patch( @@ -117,7 +112,7 @@ async def test_no_aid_collision(hass: HomeAssistant, device_reg, entity_reg) -> seen_aids = set() for unique_id in range(0, 202): - ent = entity_reg.async_get_or_create( + ent = entity_registry.async_get_or_create( "light", "device", unique_id, device_id=device_entry.id ) hass.states.async_set(ent.entity_id, "on") @@ -127,7 +122,9 @@ async def test_no_aid_collision(hass: HomeAssistant, device_reg, entity_reg) -> async def test_aid_generation_no_unique_ids_handles_collision( - hass: HomeAssistant, device_reg, entity_reg + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, ) -> None: """Test colliding aids is stable.""" config_entry = MockConfigEntry(domain="test", data={}) @@ -135,9 +132,9 @@ async def test_aid_generation_no_unique_ids_handles_collision( aid_storage = AccessoryAidStorage(hass, config_entry) await aid_storage.async_initialize() - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) seen_aids = set() @@ -154,7 +151,7 @@ async def test_aid_generation_no_unique_ids_handles_collision( assert aid not in seen_aids seen_aids.add(aid) - light_ent = entity_reg.async_get_or_create( + light_ent = entity_registry.async_get_or_create( "light", "device", "unique_id", device_id=device_entry.id ) hass.states.async_set(light_ent.entity_id, "on") diff --git a/tests/components/homekit/test_config_flow.py b/tests/components/homekit/test_config_flow.py index 2beece05b6e..3de10491f39 100644 --- a/tests/components/homekit/test_config_flow.py +++ b/tests/components/homekit/test_config_flow.py @@ -13,7 +13,7 @@ from homeassistant.components.homekit.const import ( from homeassistant.config_entries import SOURCE_IGNORE, SOURCE_IMPORT from homeassistant.const import CONF_NAME, CONF_PORT, EntityCategory from homeassistant.core import HomeAssistant -from homeassistant.helpers.entity_registry import RegistryEntry, RegistryEntryHider +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.entityfilter import CONF_INCLUDE_DOMAINS from homeassistant.setup import async_setup_component @@ -398,8 +398,8 @@ async def test_options_flow_devices( port_mock, hass: HomeAssistant, demo_cleanup, - device_reg, - entity_reg, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, mock_get_source_ip, mock_async_zeroconf: None, ) -> None: @@ -451,7 +451,7 @@ async def test_options_flow_devices( assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["step_id"] == "exclude" - entry = entity_reg.async_get("light.ceiling_lights") + entry = entity_registry.async_get("light.ceiling_lights") assert entry is not None device_id = entry.device_id @@ -1379,7 +1379,7 @@ async def test_options_flow_exclude_mode_skips_category_entities( mock_get_source_ip, hk_driver, mock_async_zeroconf: None, - entity_reg, + entity_registry: er.EntityRegistry, ) -> None: """Ensure exclude mode does not offer category entities.""" config_entry = _mock_config_entry_with_options_populated() @@ -1389,7 +1389,7 @@ async def test_options_flow_exclude_mode_skips_category_entities( hass.states.async_set("media_player.sonos", "off") hass.states.async_set("switch.other", "off") - sonos_config_switch: RegistryEntry = entity_reg.async_get_or_create( + sonos_config_switch = entity_registry.async_get_or_create( "switch", "sonos", "config", @@ -1398,7 +1398,7 @@ async def test_options_flow_exclude_mode_skips_category_entities( ) hass.states.async_set(sonos_config_switch.entity_id, "off") - sonos_notconfig_switch: RegistryEntry = entity_reg.async_get_or_create( + sonos_notconfig_switch = entity_registry.async_get_or_create( "switch", "sonos", "notconfig", @@ -1484,7 +1484,7 @@ async def test_options_flow_exclude_mode_skips_hidden_entities( mock_get_source_ip, hk_driver, mock_async_zeroconf: None, - entity_reg, + entity_registry: er.EntityRegistry, ) -> None: """Ensure exclude mode does not offer hidden entities.""" config_entry = _mock_config_entry_with_options_populated() @@ -1494,12 +1494,12 @@ async def test_options_flow_exclude_mode_skips_hidden_entities( hass.states.async_set("media_player.sonos", "off") hass.states.async_set("switch.other", "off") - sonos_hidden_switch: RegistryEntry = entity_reg.async_get_or_create( + sonos_hidden_switch = entity_registry.async_get_or_create( "switch", "sonos", "config", device_id="1234", - hidden_by=RegistryEntryHider.INTEGRATION, + hidden_by=er.RegistryEntryHider.INTEGRATION, ) hass.states.async_set(sonos_hidden_switch.entity_id, "off") await hass.async_block_till_done() @@ -1569,7 +1569,7 @@ async def test_options_flow_include_mode_allows_hidden_entities( mock_get_source_ip, hk_driver, mock_async_zeroconf: None, - entity_reg, + entity_registry: er.EntityRegistry, ) -> None: """Ensure include mode does not offer hidden entities.""" config_entry = _mock_config_entry_with_options_populated() @@ -1579,12 +1579,12 @@ async def test_options_flow_include_mode_allows_hidden_entities( hass.states.async_set("media_player.sonos", "off") hass.states.async_set("switch.other", "off") - sonos_hidden_switch: RegistryEntry = entity_reg.async_get_or_create( + sonos_hidden_switch = entity_registry.async_get_or_create( "switch", "sonos", "config", device_id="1234", - hidden_by=RegistryEntryHider.INTEGRATION, + hidden_by=er.RegistryEntryHider.INTEGRATION, ) hass.states.async_set(sonos_hidden_switch.entity_id, "off") await hass.async_block_till_done() diff --git a/tests/components/homekit/test_diagnostics.py b/tests/components/homekit/test_diagnostics.py index 8d4dfe36c04..58babc0ccb0 100644 --- a/tests/components/homekit/test_diagnostics.py +++ b/tests/components/homekit/test_diagnostics.py @@ -9,6 +9,7 @@ from homeassistant.components.homekit.const import ( ) from homeassistant.const import CONF_NAME, CONF_PORT, EVENT_HOMEASSISTANT_STARTED from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component from .util import async_init_integration @@ -314,8 +315,8 @@ async def test_config_entry_with_trigger_accessory( mock_async_zeroconf: None, events, demo_cleanup, - device_reg, - entity_reg, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, ) -> None: """Test generating diagnostics for a bridge config entry with a trigger accessory.""" assert await async_setup_component(hass, "demo", {"demo": {}}) @@ -326,7 +327,7 @@ async def test_config_entry_with_trigger_accessory( assert await async_setup_component(hass, "demo", {"demo": {}}) await hass.async_block_till_done() - entry = entity_reg.async_get("light.ceiling_lights") + entry = entity_registry.async_get("light.ceiling_lights") assert entry is not None device_id = entry.device_id diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index 5b6a6d50184..a6dbdbebdd4 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -46,9 +46,14 @@ from homeassistant.const import ( PERCENTAGE, SERVICE_RELOAD, STATE_ON, + EntityCategory, ) from homeassistant.core import HomeAssistant, HomeAssistantError, State -from homeassistant.helpers import device_registry, entity_registry as er, instance_id +from homeassistant.helpers import ( + device_registry as dr, + entity_registry as er, + instance_id, +) from homeassistant.helpers.entityfilter import ( CONF_EXCLUDE_DOMAINS, CONF_EXCLUDE_ENTITIES, @@ -503,15 +508,12 @@ async def test_homekit_entity_glob_filter( async def test_homekit_entity_glob_filter_with_config_entities( - hass: HomeAssistant, mock_async_zeroconf: None, entity_reg + hass: HomeAssistant, mock_async_zeroconf: None, entity_registry: er.EntityRegistry ) -> None: """Test the entity filter with configuration entities.""" entry = await async_init_integration(hass) - from homeassistant.const import EntityCategory - from homeassistant.helpers.entity_registry import RegistryEntry - - select_config_entity: RegistryEntry = entity_reg.async_get_or_create( + select_config_entity = entity_registry.async_get_or_create( "select", "any", "any", @@ -520,7 +522,7 @@ async def test_homekit_entity_glob_filter_with_config_entities( ) hass.states.async_set(select_config_entity.entity_id, "off") - switch_config_entity: RegistryEntry = entity_reg.async_get_or_create( + switch_config_entity = entity_registry.async_get_or_create( "switch", "any", "any", @@ -559,14 +561,12 @@ async def test_homekit_entity_glob_filter_with_config_entities( async def test_homekit_entity_glob_filter_with_hidden_entities( - hass: HomeAssistant, mock_async_zeroconf: None, entity_reg + hass: HomeAssistant, mock_async_zeroconf: None, entity_registry: er.EntityRegistry ) -> None: """Test the entity filter with hidden entities.""" entry = await async_init_integration(hass) - from homeassistant.helpers.entity_registry import RegistryEntry - - select_config_entity: RegistryEntry = entity_reg.async_get_or_create( + select_config_entity = entity_registry.async_get_or_create( "select", "any", "any", @@ -575,7 +575,7 @@ async def test_homekit_entity_glob_filter_with_hidden_entities( ) hass.states.async_set(select_config_entity.entity_id, "off") - switch_config_entity: RegistryEntry = entity_reg.async_get_or_create( + switch_config_entity = entity_registry.async_get_or_create( "switch", "any", "any", @@ -614,7 +614,10 @@ async def test_homekit_entity_glob_filter_with_hidden_entities( async def test_homekit_start( - hass: HomeAssistant, hk_driver, mock_async_zeroconf: None, device_reg + hass: HomeAssistant, + hk_driver, + mock_async_zeroconf: None, + device_registry: dr.DeviceRegistry, ) -> None: """Test HomeKit start method.""" entry = await async_init_integration(hass) @@ -627,8 +630,8 @@ async def test_homekit_start( acc = Accessory(hk_driver, "any") homekit.driver.accessory = acc - connection = (device_registry.CONNECTION_NETWORK_MAC, "AA:BB:CC:DD:EE:FF") - bridge_with_wrong_mac = device_reg.async_get_or_create( + connection = (dr.CONNECTION_NETWORK_MAC, "AA:BB:CC:DD:EE:FF") + bridge_with_wrong_mac = device_registry.async_get_or_create( config_entry_id=entry.entry_id, connections={connection}, manufacturer="Any", @@ -661,14 +664,14 @@ async def test_homekit_start( await hass.async_block_till_done() assert not hk_driver_start.called - assert device_reg.async_get(bridge_with_wrong_mac.id) is None + assert device_registry.async_get(bridge_with_wrong_mac.id) is None - device = device_reg.async_get_device( + device = device_registry.async_get_device( {(DOMAIN, entry.entry_id, BRIDGE_SERIAL_NUMBER)} ) assert device - formatted_mac = device_registry.format_mac(homekit.driver.state.mac) - assert (device_registry.CONNECTION_NETWORK_MAC, formatted_mac) in device.connections + formatted_mac = dr.format_mac(homekit.driver.state.mac) + assert (dr.CONNECTION_NETWORK_MAC, formatted_mac) in device.connections # Start again to make sure the registry entry is kept homekit.status = STATUS_READY @@ -679,14 +682,14 @@ async def test_homekit_start( ) as hk_driver_start: await homekit.async_start() - device = device_reg.async_get_device( + device = device_registry.async_get_device( {(DOMAIN, entry.entry_id, BRIDGE_SERIAL_NUMBER)} ) assert device - formatted_mac = device_registry.format_mac(homekit.driver.state.mac) - assert (device_registry.CONNECTION_NETWORK_MAC, formatted_mac) in device.connections + formatted_mac = dr.format_mac(homekit.driver.state.mac) + assert (dr.CONNECTION_NETWORK_MAC, formatted_mac) in device.connections - assert len(device_reg.devices) == 1 + assert len(device_registry.devices) == 1 assert homekit.driver.state.config_version == 1 @@ -736,8 +739,8 @@ async def test_homekit_start_with_a_device( hk_driver, mock_async_zeroconf: None, demo_cleanup, - device_reg, - entity_reg, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, ) -> None: """Test HomeKit start method with a device.""" @@ -747,7 +750,7 @@ async def test_homekit_start_with_a_device( assert await async_setup_component(hass, "demo", {"demo": {}}) await hass.async_block_till_done() - reg_entry = entity_reg.async_get("light.ceiling_lights") + reg_entry = entity_registry.async_get("light.ceiling_lights") assert reg_entry is not None device_id = reg_entry.device_id await async_init_entry(hass, entry) @@ -841,7 +844,7 @@ async def test_homekit_reset_accessories( async def test_homekit_unpair( - hass: HomeAssistant, device_reg, mock_async_zeroconf: None + hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_async_zeroconf: None ) -> None: """Test unpairing HomeKit accessories.""" @@ -873,9 +876,9 @@ async def test_homekit_unpair( state.add_paired_client("client4", "any", b"0") state.add_paired_client("client5", "any", b"0") - formatted_mac = device_registry.format_mac(state.mac) - hk_bridge_dev = device_reg.async_get_device( - {}, {(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)} + formatted_mac = dr.format_mac(state.mac) + hk_bridge_dev = device_registry.async_get_device( + {}, {(dr.CONNECTION_NETWORK_MAC, formatted_mac)} ) await hass.services.async_call( @@ -890,7 +893,7 @@ async def test_homekit_unpair( async def test_homekit_unpair_missing_device_id( - hass: HomeAssistant, device_reg, mock_async_zeroconf: None + hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_async_zeroconf: None ) -> None: """Test unpairing HomeKit accessories with invalid device id.""" @@ -930,7 +933,7 @@ async def test_homekit_unpair_missing_device_id( async def test_homekit_unpair_not_homekit_device( - hass: HomeAssistant, device_reg, mock_async_zeroconf: None + hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_async_zeroconf: None ) -> None: """Test unpairing HomeKit accessories with a non-homekit device id.""" @@ -957,12 +960,12 @@ async def test_homekit_unpair_not_homekit_device( homekit.bridge.accessories = {aid: acc_mock} homekit.status = STATUS_RUNNING - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=not_homekit_entry.entry_id, sw_version="0.16.0", model="Powerwall 2", manufacturer="Tesla", - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) state = homekit.driver.state @@ -1299,7 +1302,11 @@ async def test_homekit_too_many_accessories( async def test_homekit_finds_linked_batteries( - hass: HomeAssistant, hk_driver, device_reg, entity_reg, mock_async_zeroconf: None + hass: HomeAssistant, + hk_driver, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + mock_async_zeroconf: None, ) -> None: """Test HomeKit start method.""" entry = await async_init_integration(hass) @@ -1311,30 +1318,30 @@ async def test_homekit_finds_linked_batteries( config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, sw_version="0.16.0", hw_version="2.34", model="Powerwall 2", manufacturer="Tesla", - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - binary_charging_sensor = entity_reg.async_get_or_create( + binary_charging_sensor = entity_registry.async_get_or_create( "binary_sensor", "powerwall", "battery_charging", device_id=device_entry.id, original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING, ) - battery_sensor = entity_reg.async_get_or_create( + battery_sensor = entity_registry.async_get_or_create( "sensor", "powerwall", "battery", device_id=device_entry.id, original_device_class=SensorDeviceClass.BATTERY, ) - light = entity_reg.async_get_or_create( + light = entity_registry.async_get_or_create( "light", "powerwall", "demo", device_id=device_entry.id ) @@ -1372,7 +1379,11 @@ async def test_homekit_finds_linked_batteries( async def test_homekit_async_get_integration_fails( - hass: HomeAssistant, hk_driver, device_reg, entity_reg, mock_async_zeroconf: None + hass: HomeAssistant, + hk_driver, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + mock_async_zeroconf: None, ) -> None: """Test that we continue if async_get_integration fails.""" entry = await async_init_integration(hass) @@ -1383,28 +1394,28 @@ async def test_homekit_async_get_integration_fails( config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, sw_version="0.16.0", model="Powerwall 2", - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - binary_charging_sensor = entity_reg.async_get_or_create( + binary_charging_sensor = entity_registry.async_get_or_create( "binary_sensor", "invalid_integration_does_not_exist", "battery_charging", device_id=device_entry.id, original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING, ) - battery_sensor = entity_reg.async_get_or_create( + battery_sensor = entity_registry.async_get_or_create( "sensor", "invalid_integration_does_not_exist", "battery", device_id=device_entry.id, original_device_class=SensorDeviceClass.BATTERY, ) - light = entity_reg.async_get_or_create( + light = entity_registry.async_get_or_create( "light", "invalid_integration_does_not_exist", "demo", device_id=device_entry.id ) @@ -1594,7 +1605,11 @@ async def test_homekit_uses_system_zeroconf( async def test_homekit_ignored_missing_devices( - hass: HomeAssistant, hk_driver, device_reg, entity_reg, mock_async_zeroconf: None + hass: HomeAssistant, + hk_driver, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + mock_async_zeroconf: None, ) -> None: """Test HomeKit handles a device in the entity registry but missing from the device registry.""" @@ -1606,40 +1621,40 @@ async def test_homekit_ignored_missing_devices( config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, sw_version="0.16.0", model="Powerwall 2", manufacturer="Tesla", - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - entity_reg.async_get_or_create( + entity_registry.async_get_or_create( "binary_sensor", "powerwall", "battery_charging", device_id=device_entry.id, original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING, ) - entity_reg.async_get_or_create( + entity_registry.async_get_or_create( "sensor", "powerwall", "battery", device_id=device_entry.id, original_device_class=SensorDeviceClass.BATTERY, ) - light = entity_reg.async_get_or_create( + light = entity_registry.async_get_or_create( "light", "powerwall", "demo", device_id=device_entry.id ) - before_removal = entity_reg.entities.copy() + before_removal = entity_registry.entities.copy() # Delete the device to make sure we fallback # to using the platform - device_reg.async_remove_device(device_entry.id) + device_registry.async_remove_device(device_entry.id) # Wait for the entities to be removed await asyncio.sleep(0) await asyncio.sleep(0) # Restore the registry - entity_reg.entities = before_removal + entity_registry.entities = before_removal hass.states.async_set(light.entity_id, STATE_ON) hass.states.async_set("light.two", STATE_ON) @@ -1664,7 +1679,11 @@ async def test_homekit_ignored_missing_devices( async def test_homekit_finds_linked_motion_sensors( - hass: HomeAssistant, hk_driver, device_reg, entity_reg, mock_async_zeroconf: None + hass: HomeAssistant, + hk_driver, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + mock_async_zeroconf: None, ) -> None: """Test HomeKit start method.""" entry = await async_init_integration(hass) @@ -1676,22 +1695,22 @@ async def test_homekit_finds_linked_motion_sensors( config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, sw_version="0.16.0", model="Camera Server", manufacturer="Ubq", - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - binary_motion_sensor = entity_reg.async_get_or_create( + binary_motion_sensor = entity_registry.async_get_or_create( "binary_sensor", "camera", "motion_sensor", device_id=device_entry.id, original_device_class=BinarySensorDeviceClass.MOTION, ) - camera = entity_reg.async_get_or_create( + camera = entity_registry.async_get_or_create( "camera", "camera", "demo", device_id=device_entry.id ) @@ -1726,7 +1745,11 @@ async def test_homekit_finds_linked_motion_sensors( async def test_homekit_finds_linked_humidity_sensors( - hass: HomeAssistant, hk_driver, device_reg, entity_reg, mock_async_zeroconf: None + hass: HomeAssistant, + hk_driver, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + mock_async_zeroconf: None, ) -> None: """Test HomeKit start method.""" entry = await async_init_integration(hass) @@ -1738,22 +1761,22 @@ async def test_homekit_finds_linked_humidity_sensors( config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( + device_entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, sw_version="0.16.1", model="Smart Brainy Clever Humidifier", manufacturer="Home Assistant", - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - humidity_sensor = entity_reg.async_get_or_create( + humidity_sensor = entity_registry.async_get_or_create( "sensor", "humidifier", "humidity_sensor", device_id=device_entry.id, original_device_class=SensorDeviceClass.HUMIDITY, ) - humidifier = entity_reg.async_get_or_create( + humidifier = entity_registry.async_get_or_create( "humidifier", "humidifier", "demo", device_id=device_entry.id ) @@ -1862,7 +1885,10 @@ async def test_reload(hass: HomeAssistant, mock_async_zeroconf: None) -> None: async def test_homekit_start_in_accessory_mode( - hass: HomeAssistant, hk_driver, mock_async_zeroconf: None, device_reg + hass: HomeAssistant, + hk_driver, + mock_async_zeroconf: None, + device_registry: dr.DeviceRegistry, ) -> None: """Test HomeKit start method in accessory mode.""" entry = await async_init_integration(hass) @@ -1896,7 +1922,7 @@ async def test_homekit_start_in_accessory_mode_unsupported_entity( hass: HomeAssistant, hk_driver, mock_async_zeroconf: None, - device_reg, + device_registry: dr.DeviceRegistry, caplog: pytest.LogCaptureFixture, ) -> None: """Test HomeKit start method in accessory mode with an unsupported entity.""" @@ -1930,7 +1956,7 @@ async def test_homekit_start_in_accessory_mode_missing_entity( hass: HomeAssistant, hk_driver, mock_async_zeroconf: None, - device_reg, + device_registry: dr.DeviceRegistry, caplog: pytest.LogCaptureFixture, ) -> None: """Test HomeKit start method in accessory mode when entity is not available.""" diff --git a/tests/components/homekit/test_init.py b/tests/components/homekit/test_init.py index 5445d3c8ae1..2bb9a4972a3 100644 --- a/tests/components/homekit/test_init.py +++ b/tests/components/homekit/test_init.py @@ -16,6 +16,7 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_STARTED, ) from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from homeassistant.setup import async_setup_component from .util import PATH_HOMEKIT @@ -71,7 +72,7 @@ async def test_bridge_with_triggers( hass: HomeAssistant, hk_driver, mock_async_zeroconf: None, - entity_reg, + entity_registry: er.EntityRegistry, caplog: pytest.LogCaptureFixture, ) -> None: """Test we can setup a bridge with triggers and we ignore numeric states. @@ -83,7 +84,7 @@ async def test_bridge_with_triggers( assert await async_setup_component(hass, "demo", {"demo": {}}) await hass.async_block_till_done() - entry = entity_reg.async_get("cover.living_room_window") + entry = entity_registry.async_get("cover.living_room_window") assert entry is not None device_id = entry.device_id diff --git a/tests/components/homekit/test_type_triggers.py b/tests/components/homekit/test_type_triggers.py index e46bcaf82d2..fd77499ff09 100644 --- a/tests/components/homekit/test_type_triggers.py +++ b/tests/components/homekit/test_type_triggers.py @@ -6,13 +6,19 @@ from homeassistant.components.homekit.const import CHAR_PROGRAMMABLE_SWITCH_EVEN from homeassistant.components.homekit.type_triggers import DeviceTriggerAccessory from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry, async_get_device_automations async def test_programmable_switch_button_fires_on_trigger( - hass: HomeAssistant, hk_driver, events, demo_cleanup, device_reg, entity_reg + hass: HomeAssistant, + hk_driver, + events, + demo_cleanup, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, ) -> None: """Test that DeviceTriggerAccessory fires the programmable switch event on trigger.""" hk_driver.publish = MagicMock() @@ -24,7 +30,7 @@ async def test_programmable_switch_button_fires_on_trigger( hass.states.async_set("light.ceiling_lights", STATE_OFF) await hass.async_block_till_done() - entry = entity_reg.async_get("light.ceiling_lights") + entry = entity_registry.async_get("light.ceiling_lights") assert entry is not None device_id = entry.device_id