From 6e3e09f2c342126615c354a9e157638304e1d455 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Sun, 31 Mar 2024 09:08:07 +0200 Subject: [PATCH] Use entity & device registry mocks instead of `hass.helpers` in airthings_ble tests (#114520) --- tests/components/airthings_ble/__init__.py | 6 +-- tests/components/airthings_ble/test_sensor.py | 47 ++++++++++--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/tests/components/airthings_ble/__init__.py b/tests/components/airthings_ble/__init__.py index d60b42eddf2..ba213e0c2e7 100644 --- a/tests/components/airthings_ble/__init__.py +++ b/tests/components/airthings_ble/__init__.py @@ -12,7 +12,8 @@ from airthings_ble import ( from homeassistant.components.airthings_ble.const import DOMAIN from homeassistant.components.bluetooth.models import BluetoothServiceInfoBleak -from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH +from homeassistant.config_entries import ConfigEntry +from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceRegistry from tests.common import MockConfigEntry, MockEntity from tests.components.bluetooth import generate_advertisement_data, generate_ble_device @@ -232,9 +233,8 @@ def create_entry(hass): return entry -def create_device(hass, entry): +def create_device(entry: ConfigEntry, device_registry: DeviceRegistry): """Create a device for the given entry.""" - device_registry = hass.helpers.device_registry.async_get(hass) device = device_registry.async_get_or_create( config_entry_id=entry.entry_id, connections={(CONNECTION_BLUETOOTH, WAVE_SERVICE_INFO.address)}, diff --git a/tests/components/airthings_ble/test_sensor.py b/tests/components/airthings_ble/test_sensor.py index eee4de263d6..9949528ccc7 100644 --- a/tests/components/airthings_ble/test_sensor.py +++ b/tests/components/airthings_ble/test_sensor.py @@ -5,6 +5,7 @@ import logging from homeassistant.components.airthings_ble.const import DOMAIN from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr, entity_registry as er from tests.components.airthings_ble import ( CO2_V1, @@ -25,18 +26,20 @@ from tests.components.bluetooth import inject_bluetooth_service_info _LOGGER = logging.getLogger(__name__) -async def test_migration_from_v1_to_v3_unique_id(hass: HomeAssistant): +async def test_migration_from_v1_to_v3_unique_id( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +): """Verify that we can migrate from v1 (pre 2023.9.0) to the latest unique id format.""" entry = create_entry(hass) - device = create_device(hass, entry) + device = create_device(entry, device_registry) assert entry is not None assert device is not None new_unique_id = f"{WAVE_DEVICE_INFO.address}_temperature" - entity_registry = hass.helpers.entity_registry.async_get(hass) - sensor = entity_registry.async_get_or_create( domain=DOMAIN, platform=Platform.SENSOR, @@ -64,18 +67,18 @@ async def test_migration_from_v1_to_v3_unique_id(hass: HomeAssistant): assert entity_registry.async_get(sensor.entity_id).unique_id == new_unique_id -async def test_migration_from_v2_to_v3_unique_id(hass: HomeAssistant): +async def test_migration_from_v2_to_v3_unique_id( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +): """Verify that we can migrate from v2 (introduced in 2023.9.0) to the latest unique id format.""" entry = create_entry(hass) - device = create_device(hass, entry) + device = create_device(entry, device_registry) assert entry is not None assert device is not None - entity_registry = hass.helpers.entity_registry.async_get(hass) - - await hass.async_block_till_done() - sensor = entity_registry.async_get_or_create( domain=DOMAIN, platform=Platform.SENSOR, @@ -105,18 +108,18 @@ async def test_migration_from_v2_to_v3_unique_id(hass: HomeAssistant): assert entity_registry.async_get(sensor.entity_id).unique_id == new_unique_id -async def test_migration_from_v1_and_v2_to_v3_unique_id(hass: HomeAssistant): +async def test_migration_from_v1_and_v2_to_v3_unique_id( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +): """Test if migration works when we have both v1 (pre 2023.9.0) and v2 (introduced in 2023.9.0) unique ids.""" entry = create_entry(hass) - device = create_device(hass, entry) + device = create_device(entry, device_registry) assert entry is not None assert device is not None - entity_registry = hass.helpers.entity_registry.async_get(hass) - - await hass.async_block_till_done() - v2 = entity_registry.async_get_or_create( domain=DOMAIN, platform=Platform.SENSOR, @@ -155,18 +158,18 @@ async def test_migration_from_v1_and_v2_to_v3_unique_id(hass: HomeAssistant): assert entity_registry.async_get(v2.entity_id).unique_id == CO2_V2.unique_id -async def test_migration_with_all_unique_ids(hass: HomeAssistant): +async def test_migration_with_all_unique_ids( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +): """Test if migration works when we have all unique ids.""" entry = create_entry(hass) - device = create_device(hass, entry) + device = create_device(entry, device_registry) assert entry is not None assert device is not None - entity_registry = hass.helpers.entity_registry.async_get(hass) - - await hass.async_block_till_done() - v1 = entity_registry.async_get_or_create( domain=DOMAIN, platform=Platform.SENSOR,