Adjust entity registry access in homekit tests (#88959)

This commit is contained in:
epenet 2023-03-01 16:54:00 +01:00 committed by GitHub
parent 3818e318db
commit ee78864b05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 149 additions and 130 deletions

View File

@ -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.const import BRIDGE_NAME, EVENT_HOMEKIT_CHANGED
from homeassistant.components.homekit.iidmanager import AccessoryIIDStorage 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 @pytest.fixture
@ -103,18 +103,6 @@ def events(hass):
return async_capture_events(hass, EVENT_HOMEKIT_CHANGED) 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 @pytest.fixture
def demo_cleanup(hass): def demo_cleanup(hass):
"""Clean up device tracker demo file.""" """Clean up device tracker demo file."""

View File

@ -3,7 +3,6 @@ import os
from unittest.mock import patch from unittest.mock import patch
from fnvhash import fnv1a_32 from fnvhash import fnv1a_32
import pytest
from homeassistant.components.homekit.aidmanager import ( from homeassistant.components.homekit.aidmanager import (
AccessoryAidStorage, AccessoryAidStorage,
@ -11,39 +10,31 @@ from homeassistant.components.homekit.aidmanager import (
get_system_unique_id, get_system_unique_id,
) )
from homeassistant.core import HomeAssistant 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 homeassistant.helpers.storage import STORAGE_DIR
from tests.common import MockConfigEntry, mock_device_registry, mock_registry from tests.common import MockConfigEntry
@pytest.fixture async def test_aid_generation(
def device_reg(hass): hass: HomeAssistant,
"""Return an empty, loaded, registry.""" device_registry: dr.DeviceRegistry,
return mock_device_registry(hass) entity_registry: er.EntityRegistry,
) -> None:
@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:
"""Test generating aids.""" """Test generating aids."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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, 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", "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 "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 "remote", "device", "unique_id", device_id=device_entry.id
) )
hass.states.async_set(light_ent.entity_id, "on") 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.""" """Test generating aids."""
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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, 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( with patch(
@ -117,7 +112,7 @@ async def test_no_aid_collision(hass: HomeAssistant, device_reg, entity_reg) ->
seen_aids = set() seen_aids = set()
for unique_id in range(0, 202): 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 "light", "device", unique_id, device_id=device_entry.id
) )
hass.states.async_set(ent.entity_id, "on") 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( 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: ) -> None:
"""Test colliding aids is stable.""" """Test colliding aids is stable."""
config_entry = MockConfigEntry(domain="test", data={}) 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) aid_storage = AccessoryAidStorage(hass, config_entry)
await aid_storage.async_initialize() 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, 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() seen_aids = set()
@ -154,7 +151,7 @@ async def test_aid_generation_no_unique_ids_handles_collision(
assert aid not in seen_aids assert aid not in seen_aids
seen_aids.add(aid) 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 "light", "device", "unique_id", device_id=device_entry.id
) )
hass.states.async_set(light_ent.entity_id, "on") hass.states.async_set(light_ent.entity_id, "on")

View File

@ -13,7 +13,7 @@ from homeassistant.components.homekit.const import (
from homeassistant.config_entries import SOURCE_IGNORE, SOURCE_IMPORT from homeassistant.config_entries import SOURCE_IGNORE, SOURCE_IMPORT
from homeassistant.const import CONF_NAME, CONF_PORT, EntityCategory from homeassistant.const import CONF_NAME, CONF_PORT, EntityCategory
from homeassistant.core import HomeAssistant 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.helpers.entityfilter import CONF_INCLUDE_DOMAINS
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -398,8 +398,8 @@ async def test_options_flow_devices(
port_mock, port_mock,
hass: HomeAssistant, hass: HomeAssistant,
demo_cleanup, demo_cleanup,
device_reg, device_registry: dr.DeviceRegistry,
entity_reg, entity_registry: er.EntityRegistry,
mock_get_source_ip, mock_get_source_ip,
mock_async_zeroconf: None, mock_async_zeroconf: None,
) -> None: ) -> None:
@ -451,7 +451,7 @@ async def test_options_flow_devices(
assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "exclude" 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 assert entry is not None
device_id = entry.device_id device_id = entry.device_id
@ -1379,7 +1379,7 @@ async def test_options_flow_exclude_mode_skips_category_entities(
mock_get_source_ip, mock_get_source_ip,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: None,
entity_reg, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Ensure exclude mode does not offer category entities.""" """Ensure exclude mode does not offer category entities."""
config_entry = _mock_config_entry_with_options_populated() 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("media_player.sonos", "off")
hass.states.async_set("switch.other", "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", "switch",
"sonos", "sonos",
"config", "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") 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", "switch",
"sonos", "sonos",
"notconfig", "notconfig",
@ -1484,7 +1484,7 @@ async def test_options_flow_exclude_mode_skips_hidden_entities(
mock_get_source_ip, mock_get_source_ip,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: None,
entity_reg, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Ensure exclude mode does not offer hidden entities.""" """Ensure exclude mode does not offer hidden entities."""
config_entry = _mock_config_entry_with_options_populated() 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("media_player.sonos", "off")
hass.states.async_set("switch.other", "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", "switch",
"sonos", "sonos",
"config", "config",
device_id="1234", device_id="1234",
hidden_by=RegistryEntryHider.INTEGRATION, hidden_by=er.RegistryEntryHider.INTEGRATION,
) )
hass.states.async_set(sonos_hidden_switch.entity_id, "off") hass.states.async_set(sonos_hidden_switch.entity_id, "off")
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1569,7 +1569,7 @@ async def test_options_flow_include_mode_allows_hidden_entities(
mock_get_source_ip, mock_get_source_ip,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: None,
entity_reg, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Ensure include mode does not offer hidden entities.""" """Ensure include mode does not offer hidden entities."""
config_entry = _mock_config_entry_with_options_populated() 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("media_player.sonos", "off")
hass.states.async_set("switch.other", "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", "switch",
"sonos", "sonos",
"config", "config",
device_id="1234", device_id="1234",
hidden_by=RegistryEntryHider.INTEGRATION, hidden_by=er.RegistryEntryHider.INTEGRATION,
) )
hass.states.async_set(sonos_hidden_switch.entity_id, "off") hass.states.async_set(sonos_hidden_switch.entity_id, "off")
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -9,6 +9,7 @@ from homeassistant.components.homekit.const import (
) )
from homeassistant.const import CONF_NAME, CONF_PORT, EVENT_HOMEASSISTANT_STARTED from homeassistant.const import CONF_NAME, CONF_PORT, EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import HomeAssistant 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 homeassistant.setup import async_setup_component
from .util import async_init_integration from .util import async_init_integration
@ -314,8 +315,8 @@ async def test_config_entry_with_trigger_accessory(
mock_async_zeroconf: None, mock_async_zeroconf: None,
events, events,
demo_cleanup, demo_cleanup,
device_reg, device_registry: dr.DeviceRegistry,
entity_reg, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test generating diagnostics for a bridge config entry with a trigger accessory.""" """Test generating diagnostics for a bridge config entry with a trigger accessory."""
assert await async_setup_component(hass, "demo", {"demo": {}}) 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": {}}) assert await async_setup_component(hass, "demo", {"demo": {}})
await hass.async_block_till_done() 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 assert entry is not None
device_id = entry.device_id device_id = entry.device_id

View File

@ -46,9 +46,14 @@ from homeassistant.const import (
PERCENTAGE, PERCENTAGE,
SERVICE_RELOAD, SERVICE_RELOAD,
STATE_ON, STATE_ON,
EntityCategory,
) )
from homeassistant.core import HomeAssistant, HomeAssistantError, State 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 ( from homeassistant.helpers.entityfilter import (
CONF_EXCLUDE_DOMAINS, CONF_EXCLUDE_DOMAINS,
CONF_EXCLUDE_ENTITIES, CONF_EXCLUDE_ENTITIES,
@ -503,15 +508,12 @@ async def test_homekit_entity_glob_filter(
async def test_homekit_entity_glob_filter_with_config_entities( 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: ) -> None:
"""Test the entity filter with configuration entities.""" """Test the entity filter with configuration entities."""
entry = await async_init_integration(hass) entry = await async_init_integration(hass)
from homeassistant.const import EntityCategory select_config_entity = entity_registry.async_get_or_create(
from homeassistant.helpers.entity_registry import RegistryEntry
select_config_entity: RegistryEntry = entity_reg.async_get_or_create(
"select", "select",
"any", "any",
"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") 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", "switch",
"any", "any",
"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( 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: ) -> None:
"""Test the entity filter with hidden entities.""" """Test the entity filter with hidden entities."""
entry = await async_init_integration(hass) entry = await async_init_integration(hass)
from homeassistant.helpers.entity_registry import RegistryEntry select_config_entity = entity_registry.async_get_or_create(
select_config_entity: RegistryEntry = entity_reg.async_get_or_create(
"select", "select",
"any", "any",
"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") 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", "switch",
"any", "any",
"any", "any",
@ -614,7 +614,10 @@ async def test_homekit_entity_glob_filter_with_hidden_entities(
async def test_homekit_start( 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: ) -> None:
"""Test HomeKit start method.""" """Test HomeKit start method."""
entry = await async_init_integration(hass) entry = await async_init_integration(hass)
@ -627,8 +630,8 @@ async def test_homekit_start(
acc = Accessory(hk_driver, "any") acc = Accessory(hk_driver, "any")
homekit.driver.accessory = acc homekit.driver.accessory = acc
connection = (device_registry.CONNECTION_NETWORK_MAC, "AA:BB:CC:DD:EE:FF") connection = (dr.CONNECTION_NETWORK_MAC, "AA:BB:CC:DD:EE:FF")
bridge_with_wrong_mac = device_reg.async_get_or_create( bridge_with_wrong_mac = device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
connections={connection}, connections={connection},
manufacturer="Any", manufacturer="Any",
@ -661,14 +664,14 @@ async def test_homekit_start(
await hass.async_block_till_done() await hass.async_block_till_done()
assert not hk_driver_start.called 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)} {(DOMAIN, entry.entry_id, BRIDGE_SERIAL_NUMBER)}
) )
assert device assert device
formatted_mac = device_registry.format_mac(homekit.driver.state.mac) formatted_mac = dr.format_mac(homekit.driver.state.mac)
assert (device_registry.CONNECTION_NETWORK_MAC, formatted_mac) in device.connections assert (dr.CONNECTION_NETWORK_MAC, formatted_mac) in device.connections
# Start again to make sure the registry entry is kept # Start again to make sure the registry entry is kept
homekit.status = STATUS_READY homekit.status = STATUS_READY
@ -679,14 +682,14 @@ async def test_homekit_start(
) as hk_driver_start: ) as hk_driver_start:
await homekit.async_start() await homekit.async_start()
device = device_reg.async_get_device( device = device_registry.async_get_device(
{(DOMAIN, entry.entry_id, BRIDGE_SERIAL_NUMBER)} {(DOMAIN, entry.entry_id, BRIDGE_SERIAL_NUMBER)}
) )
assert device assert device
formatted_mac = device_registry.format_mac(homekit.driver.state.mac) formatted_mac = dr.format_mac(homekit.driver.state.mac)
assert (device_registry.CONNECTION_NETWORK_MAC, formatted_mac) in device.connections 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 assert homekit.driver.state.config_version == 1
@ -736,8 +739,8 @@ async def test_homekit_start_with_a_device(
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: None,
demo_cleanup, demo_cleanup,
device_reg, device_registry: dr.DeviceRegistry,
entity_reg, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test HomeKit start method with a device.""" """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": {}}) assert await async_setup_component(hass, "demo", {"demo": {}})
await hass.async_block_till_done() 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 assert reg_entry is not None
device_id = reg_entry.device_id device_id = reg_entry.device_id
await async_init_entry(hass, entry) await async_init_entry(hass, entry)
@ -841,7 +844,7 @@ async def test_homekit_reset_accessories(
async def test_homekit_unpair( async def test_homekit_unpair(
hass: HomeAssistant, device_reg, mock_async_zeroconf: None hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_async_zeroconf: None
) -> None: ) -> None:
"""Test unpairing HomeKit accessories.""" """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("client4", "any", b"0")
state.add_paired_client("client5", "any", b"0") state.add_paired_client("client5", "any", b"0")
formatted_mac = device_registry.format_mac(state.mac) formatted_mac = dr.format_mac(state.mac)
hk_bridge_dev = device_reg.async_get_device( hk_bridge_dev = device_registry.async_get_device(
{}, {(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)} {}, {(dr.CONNECTION_NETWORK_MAC, formatted_mac)}
) )
await hass.services.async_call( await hass.services.async_call(
@ -890,7 +893,7 @@ async def test_homekit_unpair(
async def test_homekit_unpair_missing_device_id( 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: ) -> None:
"""Test unpairing HomeKit accessories with invalid device id.""" """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( 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: ) -> None:
"""Test unpairing HomeKit accessories with a non-homekit device id.""" """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.bridge.accessories = {aid: acc_mock}
homekit.status = STATUS_RUNNING 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, config_entry_id=not_homekit_entry.entry_id,
sw_version="0.16.0", sw_version="0.16.0",
model="Powerwall 2", model="Powerwall 2",
manufacturer="Tesla", 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 state = homekit.driver.state
@ -1299,7 +1302,11 @@ async def test_homekit_too_many_accessories(
async def test_homekit_finds_linked_batteries( 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: ) -> None:
"""Test HomeKit start method.""" """Test HomeKit start method."""
entry = await async_init_integration(hass) 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 = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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, config_entry_id=config_entry.entry_id,
sw_version="0.16.0", sw_version="0.16.0",
hw_version="2.34", hw_version="2.34",
model="Powerwall 2", model="Powerwall 2",
manufacturer="Tesla", 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", "binary_sensor",
"powerwall", "powerwall",
"battery_charging", "battery_charging",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING, original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
) )
battery_sensor = entity_reg.async_get_or_create( battery_sensor = entity_registry.async_get_or_create(
"sensor", "sensor",
"powerwall", "powerwall",
"battery", "battery",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=SensorDeviceClass.BATTERY, 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 "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( 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: ) -> None:
"""Test that we continue if async_get_integration fails.""" """Test that we continue if async_get_integration fails."""
entry = await async_init_integration(hass) 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 = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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, config_entry_id=config_entry.entry_id,
sw_version="0.16.0", sw_version="0.16.0",
model="Powerwall 2", 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", "binary_sensor",
"invalid_integration_does_not_exist", "invalid_integration_does_not_exist",
"battery_charging", "battery_charging",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING, original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
) )
battery_sensor = entity_reg.async_get_or_create( battery_sensor = entity_registry.async_get_or_create(
"sensor", "sensor",
"invalid_integration_does_not_exist", "invalid_integration_does_not_exist",
"battery", "battery",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=SensorDeviceClass.BATTERY, 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 "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( 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: ) -> None:
"""Test HomeKit handles a device in the entity registry but missing from the device registry.""" """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 = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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, config_entry_id=config_entry.entry_id,
sw_version="0.16.0", sw_version="0.16.0",
model="Powerwall 2", model="Powerwall 2",
manufacturer="Tesla", 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", "binary_sensor",
"powerwall", "powerwall",
"battery_charging", "battery_charging",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING, original_device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
) )
entity_reg.async_get_or_create( entity_registry.async_get_or_create(
"sensor", "sensor",
"powerwall", "powerwall",
"battery", "battery",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=SensorDeviceClass.BATTERY, 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 "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 # Delete the device to make sure we fallback
# to using the platform # 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 # Wait for the entities to be removed
await asyncio.sleep(0) await asyncio.sleep(0)
await asyncio.sleep(0) await asyncio.sleep(0)
# Restore the registry # 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.entity_id, STATE_ON)
hass.states.async_set("light.two", 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( 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: ) -> None:
"""Test HomeKit start method.""" """Test HomeKit start method."""
entry = await async_init_integration(hass) 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 = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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, config_entry_id=config_entry.entry_id,
sw_version="0.16.0", sw_version="0.16.0",
model="Camera Server", model="Camera Server",
manufacturer="Ubq", 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", "binary_sensor",
"camera", "camera",
"motion_sensor", "motion_sensor",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=BinarySensorDeviceClass.MOTION, 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 "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( 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: ) -> None:
"""Test HomeKit start method.""" """Test HomeKit start method."""
entry = await async_init_integration(hass) 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 = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) 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, config_entry_id=config_entry.entry_id,
sw_version="0.16.1", sw_version="0.16.1",
model="Smart Brainy Clever Humidifier", model="Smart Brainy Clever Humidifier",
manufacturer="Home Assistant", 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", "sensor",
"humidifier", "humidifier",
"humidity_sensor", "humidity_sensor",
device_id=device_entry.id, device_id=device_entry.id,
original_device_class=SensorDeviceClass.HUMIDITY, 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 "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( 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: ) -> None:
"""Test HomeKit start method in accessory mode.""" """Test HomeKit start method in accessory mode."""
entry = await async_init_integration(hass) entry = await async_init_integration(hass)
@ -1896,7 +1922,7 @@ async def test_homekit_start_in_accessory_mode_unsupported_entity(
hass: HomeAssistant, hass: HomeAssistant,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: None,
device_reg, device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test HomeKit start method in accessory mode with an unsupported entity.""" """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, hass: HomeAssistant,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: None,
device_reg, device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test HomeKit start method in accessory mode when entity is not available.""" """Test HomeKit start method in accessory mode when entity is not available."""

View File

@ -16,6 +16,7 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STARTED,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from .util import PATH_HOMEKIT from .util import PATH_HOMEKIT
@ -71,7 +72,7 @@ async def test_bridge_with_triggers(
hass: HomeAssistant, hass: HomeAssistant,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: None,
entity_reg, entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test we can setup a bridge with triggers and we ignore numeric states. """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": {}}) assert await async_setup_component(hass, "demo", {"demo": {}})
await hass.async_block_till_done() 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 assert entry is not None
device_id = entry.device_id device_id = entry.device_id

View File

@ -6,13 +6,19 @@ from homeassistant.components.homekit.const import CHAR_PROGRAMMABLE_SWITCH_EVEN
from homeassistant.components.homekit.type_triggers import DeviceTriggerAccessory from homeassistant.components.homekit.type_triggers import DeviceTriggerAccessory
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant 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 homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, async_get_device_automations from tests.common import MockConfigEntry, async_get_device_automations
async def test_programmable_switch_button_fires_on_trigger( 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: ) -> None:
"""Test that DeviceTriggerAccessory fires the programmable switch event on trigger.""" """Test that DeviceTriggerAccessory fires the programmable switch event on trigger."""
hk_driver.publish = MagicMock() 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) hass.states.async_set("light.ceiling_lights", STATE_OFF)
await hass.async_block_till_done() 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 assert entry is not None
device_id = entry.device_id device_id = entry.device_id