Use common registry fixtures in lifx (#121004)

pull/121012/head
epenet 2024-07-02 12:32:17 +02:00 committed by GitHub
parent 6fd1f0a34f
commit 76a62028ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 35 deletions

View File

@ -8,8 +8,6 @@ from homeassistant.components.lifx import config_flow, coordinator, util
from . import _patch_discovery
from tests.common import mock_device_registry, mock_registry
@pytest.fixture
def mock_discovery():
@ -61,15 +59,3 @@ def lifx_mock_async_get_ipv4_broadcast_addresses():
return_value=["255.255.255.255"],
):
yield
@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)

View File

@ -11,8 +11,6 @@ from homeassistant.components.lifx import DOMAIN, discovery
from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceRegistry
from homeassistant.helpers.entity_registry import EntityRegistry
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -31,20 +29,22 @@ from tests.common import MockConfigEntry, async_fire_time_changed
async def test_migration_device_online_end_to_end(
hass: HomeAssistant, device_reg: DeviceRegistry, entity_reg: EntityRegistry
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test migration from single config entry."""
config_entry = MockConfigEntry(
domain=DOMAIN, title="LEGACY", data={}, unique_id=DOMAIN
)
config_entry.add_to_hass(hass)
device = device_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, SERIAL)},
connections={(dr.CONNECTION_NETWORK_MAC, MAC_ADDRESS)},
name=LABEL,
)
light_entity_reg = entity_reg.async_get_or_create(
light_entity_reg = entity_registry.async_get_or_create(
config_entry=config_entry,
platform=DOMAIN,
domain="light",
@ -67,7 +67,7 @@ async def test_migration_device_online_end_to_end(
assert device.config_entries == {migrated_entry.entry_id}
assert light_entity_reg.config_entry_id == migrated_entry.entry_id
assert er.async_entries_for_config_entry(entity_reg, config_entry) == []
assert er.async_entries_for_config_entry(entity_registry, config_entry) == []
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
@ -84,20 +84,22 @@ async def test_migration_device_online_end_to_end(
async def test_discovery_is_more_frequent_during_migration(
hass: HomeAssistant, device_reg: DeviceRegistry, entity_reg: EntityRegistry
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that discovery is more frequent during migration."""
config_entry = MockConfigEntry(
domain=DOMAIN, title="LEGACY", data={}, unique_id=DOMAIN
)
config_entry.add_to_hass(hass)
device = device_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, SERIAL)},
connections={(dr.CONNECTION_NETWORK_MAC, MAC_ADDRESS)},
name=LABEL,
)
entity_reg.async_get_or_create(
entity_registry.async_get_or_create(
config_entry=config_entry,
platform=DOMAIN,
domain="light",
@ -160,7 +162,9 @@ async def test_discovery_is_more_frequent_during_migration(
async def test_migration_device_online_end_to_end_after_downgrade(
hass: HomeAssistant, device_reg: DeviceRegistry, entity_reg: EntityRegistry
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test migration from single config entry can happen again after a downgrade."""
config_entry = MockConfigEntry(
@ -172,13 +176,13 @@ async def test_migration_device_online_end_to_end_after_downgrade(
domain=DOMAIN, data={CONF_HOST: IP_ADDRESS}, unique_id=SERIAL
)
already_migrated_config_entry.add_to_hass(hass)
device = device_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, SERIAL)},
connections={(dr.CONNECTION_NETWORK_MAC, MAC_ADDRESS)},
name=LABEL,
)
light_entity_reg = entity_reg.async_get_or_create(
light_entity_reg = entity_registry.async_get_or_create(
config_entry=config_entry,
platform=DOMAIN,
domain="light",
@ -197,7 +201,7 @@ async def test_migration_device_online_end_to_end_after_downgrade(
assert device.config_entries == {config_entry.entry_id}
assert light_entity_reg.config_entry_id == config_entry.entry_id
assert er.async_entries_for_config_entry(entity_reg, config_entry) == []
assert er.async_entries_for_config_entry(entity_registry, config_entry) == []
legacy_entry = None
for entry in hass.config_entries.async_entries(DOMAIN):
@ -209,7 +213,9 @@ async def test_migration_device_online_end_to_end_after_downgrade(
async def test_migration_device_online_end_to_end_ignores_other_devices(
hass: HomeAssistant, device_reg: DeviceRegistry, entity_reg: EntityRegistry
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test migration from single config entry."""
legacy_config_entry = MockConfigEntry(
@ -221,18 +227,18 @@ async def test_migration_device_online_end_to_end_ignores_other_devices(
domain="other_domain", data={}, unique_id="other_domain"
)
other_domain_config_entry.add_to_hass(hass)
device = device_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=legacy_config_entry.entry_id,
identifiers={(DOMAIN, SERIAL)},
connections={(dr.CONNECTION_NETWORK_MAC, MAC_ADDRESS)},
name=LABEL,
)
other_device = device_reg.async_get_or_create(
other_device = device_registry.async_get_or_create(
config_entry_id=other_domain_config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "556655665566")},
name=LABEL,
)
light_entity_reg = entity_reg.async_get_or_create(
light_entity_reg = entity_registry.async_get_or_create(
config_entry=legacy_config_entry,
platform=DOMAIN,
domain="light",
@ -240,7 +246,7 @@ async def test_migration_device_online_end_to_end_ignores_other_devices(
original_name=LABEL,
device_id=device.id,
)
ignored_entity_reg = entity_reg.async_get_or_create(
ignored_entity_reg = entity_registry.async_get_or_create(
config_entry=other_domain_config_entry,
platform=DOMAIN,
domain="sensor",
@ -248,7 +254,7 @@ async def test_migration_device_online_end_to_end_ignores_other_devices(
original_name=LABEL,
device_id=device.id,
)
garbage_entity_reg = entity_reg.async_get_or_create(
garbage_entity_reg = entity_registry.async_get_or_create(
config_entry=legacy_config_entry,
platform=DOMAIN,
domain="sensor",
@ -281,5 +287,11 @@ async def test_migration_device_online_end_to_end_ignores_other_devices(
assert ignored_entity_reg.config_entry_id == other_domain_config_entry.entry_id
assert garbage_entity_reg.config_entry_id == legacy_config_entry.entry_id
assert er.async_entries_for_config_entry(entity_reg, legacy_config_entry) == []
assert dr.async_entries_for_config_entry(device_reg, legacy_config_entry) == []
assert (
er.async_entries_for_config_entry(entity_registry, legacy_config_entry)
== []
)
assert (
dr.async_entries_for_config_entry(device_registry, legacy_config_entry)
== []
)