From dc8e1773f17b11a7af94c39d868afa25fd617e04 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 25 Apr 2025 12:41:58 +0200 Subject: [PATCH] Remove unused defaults from entity_registry.RegistryEntry (#143655) --- homeassistant/helpers/entity_registry.py | 39 +++++++------- tests/auth/permissions/test_entities.py | 9 ++-- tests/common.py | 31 ++++++++++- .../components/config/test_entity_registry.py | 52 +++++++++---------- tests/components/device_tracker/test_init.py | 4 +- tests/components/harmony/test_init.py | 12 ++--- tests/components/overkiz/test_init.py | 12 ++--- tests/components/sleepiq/test_init.py | 13 +++-- tests/helpers/test_entity.py | 5 +- tests/helpers/test_entity_platform.py | 25 +++++---- tests/helpers/test_entity_registry.py | 31 +++++++---- tests/helpers/test_service.py | 51 +++++++++--------- 12 files changed, 166 insertions(+), 118 deletions(-) diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index 684d00fe344..cdadc06d323 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -164,7 +164,7 @@ def _protect_entity_options( return ReadOnlyDict({key: ReadOnlyDict(val) for key, val in data.items()}) -@attr.s(frozen=True, slots=True) +@attr.s(frozen=True, kw_only=True, slots=True) class RegistryEntry: """Entity Registry Entry.""" @@ -175,35 +175,32 @@ class RegistryEntry: aliases: set[str] = attr.ib(factory=set) area_id: str | None = attr.ib(default=None) categories: dict[str, str] = attr.ib(factory=dict) - capabilities: Mapping[str, Any] | None = attr.ib(default=None) - config_entry_id: str | None = attr.ib(default=None) - config_subentry_id: str | None = attr.ib(default=None) - created_at: datetime = attr.ib(factory=utcnow) + capabilities: Mapping[str, Any] | None = attr.ib() + config_entry_id: str | None = attr.ib() + config_subentry_id: str | None = attr.ib() + created_at: datetime = attr.ib() device_class: str | None = attr.ib(default=None) - device_id: str | None = attr.ib(default=None) + device_id: str | None = attr.ib() domain: str = attr.ib(init=False, repr=False) - disabled_by: RegistryEntryDisabler | None = attr.ib(default=None) - entity_category: EntityCategory | None = attr.ib(default=None) - hidden_by: RegistryEntryHider | None = attr.ib(default=None) + disabled_by: RegistryEntryDisabler | None = attr.ib() + entity_category: EntityCategory | None = attr.ib() + has_entity_name: bool = attr.ib() + hidden_by: RegistryEntryHider | None = attr.ib() icon: str | None = attr.ib(default=None) id: str = attr.ib( - default=None, - converter=attr.converters.default_if_none(factory=uuid_util.random_uuid_hex), # type: ignore[misc] + converter=attr.converters.default_if_none(factory=uuid_util.random_uuid_hex) # type: ignore[misc] ) - has_entity_name: bool = attr.ib(default=False) labels: set[str] = attr.ib(factory=set) modified_at: datetime = attr.ib(factory=utcnow) name: str | None = attr.ib(default=None) - options: ReadOnlyEntityOptionsType = attr.ib( - default=None, converter=_protect_entity_options - ) + options: ReadOnlyEntityOptionsType = attr.ib(converter=_protect_entity_options) # As set by integration - original_device_class: str | None = attr.ib(default=None) - original_icon: str | None = attr.ib(default=None) - original_name: str | None = attr.ib(default=None) - supported_features: int = attr.ib(default=0) - translation_key: str | None = attr.ib(default=None) - unit_of_measurement: str | None = attr.ib(default=None) + original_device_class: str | None = attr.ib() + original_icon: str | None = attr.ib() + original_name: str | None = attr.ib() + supported_features: int = attr.ib() + translation_key: str | None = attr.ib() + unit_of_measurement: str | None = attr.ib() _cache: dict[str, Any] = attr.ib(factory=dict, eq=False, init=False) @domain.default diff --git a/tests/auth/permissions/test_entities.py b/tests/auth/permissions/test_entities.py index 7f5355b3cc0..cb96c9396c2 100644 --- a/tests/auth/permissions/test_entities.py +++ b/tests/auth/permissions/test_entities.py @@ -10,9 +10,8 @@ from homeassistant.auth.permissions.entities import ( from homeassistant.auth.permissions.models import PermissionLookup from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntry -from homeassistant.helpers.entity_registry import RegistryEntry -from tests.common import mock_device_registry, mock_registry +from tests.common import RegistryEntryWithDefaults, mock_device_registry, mock_registry def test_entities_none() -> None: @@ -156,13 +155,13 @@ def test_entities_device_id_boolean(hass: HomeAssistant) -> None: entity_registry = mock_registry( hass, { - "test_domain.allowed": RegistryEntry( + "test_domain.allowed": RegistryEntryWithDefaults( entity_id="test_domain.allowed", unique_id="1234", platform="test_platform", device_id="mock-allowed-dev-id", ), - "test_domain.not_allowed": RegistryEntry( + "test_domain.not_allowed": RegistryEntryWithDefaults( entity_id="test_domain.not_allowed", unique_id="5678", platform="test_platform", @@ -196,7 +195,7 @@ def test_entities_areas_area_true(hass: HomeAssistant) -> None: entity_registry = mock_registry( hass, { - "light.kitchen": RegistryEntry( + "light.kitchen": RegistryEntryWithDefaults( entity_id="light.kitchen", unique_id="1234", platform="test_platform", diff --git a/tests/common.py b/tests/common.py index 7b4bf987608..d439021a9df 100644 --- a/tests/common.py +++ b/tests/common.py @@ -30,6 +30,7 @@ from unittest.mock import AsyncMock, Mock, patch from aiohttp.test_utils import unused_port as get_test_instance_port # noqa: F401 from annotatedyaml import load_yaml_dict, loader as yaml_loader +import attr import pytest from syrupy import SnapshotAssertion import voluptuous as vol @@ -98,7 +99,7 @@ from homeassistant.helpers.entity_platform import ( ) from homeassistant.helpers.json import JSONEncoder, _orjson_default_encoder, json_dumps from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from homeassistant.util import dt as dt_util, ulid as ulid_util +from homeassistant.util import dt as dt_util, ulid as ulid_util, uuid as uuid_util from homeassistant.util.async_ import ( _SHUTDOWN_RUN_CALLBACK_THREADSAFE, get_scheduled_timer_handles, @@ -645,6 +646,34 @@ def mock_registry( return registry +@attr.s(frozen=True, kw_only=True, slots=True) +class RegistryEntryWithDefaults(er.RegistryEntry): + """Helper to create a registry entry with defaults.""" + + capabilities: Mapping[str, Any] | None = attr.ib(default=None) + config_entry_id: str | None = attr.ib(default=None) + config_subentry_id: str | None = attr.ib(default=None) + created_at: datetime = attr.ib(factory=dt_util.utcnow) + device_id: str | None = attr.ib(default=None) + disabled_by: er.RegistryEntryDisabler | None = attr.ib(default=None) + entity_category: er.EntityCategory | None = attr.ib(default=None) + hidden_by: er.RegistryEntryHider | None = attr.ib(default=None) + id: str = attr.ib( + default=None, + converter=attr.converters.default_if_none(factory=uuid_util.random_uuid_hex), # type: ignore[misc] + ) + has_entity_name: bool = attr.ib(default=False) + options: er.ReadOnlyEntityOptionsType = attr.ib( + default=None, converter=er._protect_entity_options + ) + original_device_class: str | None = attr.ib(default=None) + original_icon: str | None = attr.ib(default=None) + original_name: str | None = attr.ib(default=None) + supported_features: int = attr.ib(default=0) + translation_key: str | None = attr.ib(default=None) + unit_of_measurement: str | None = attr.ib(default=None) + + def mock_area_registry( hass: HomeAssistant, mock_entries: dict[str, ar.AreaEntry] | None = None ) -> ar.AreaRegistry: diff --git a/tests/components/config/test_entity_registry.py b/tests/components/config/test_entity_registry.py index 2e3de33d808..ea7a65f25d3 100644 --- a/tests/components/config/test_entity_registry.py +++ b/tests/components/config/test_entity_registry.py @@ -12,7 +12,6 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.device_registry import DeviceEntryDisabler from homeassistant.helpers.entity_registry import ( - RegistryEntry, RegistryEntryDisabler, RegistryEntryHider, ) @@ -23,6 +22,7 @@ from tests.common import ( MockConfigEntry, MockEntity, MockEntityPlatform, + RegistryEntryWithDefaults, mock_registry, ) from tests.typing import MockHAClientWebSocket, WebSocketGenerator @@ -45,13 +45,13 @@ async def test_list_entities( mock_registry( hass, { - "test_domain.name": RegistryEntry( + "test_domain.name": RegistryEntryWithDefaults( entity_id="test_domain.name", unique_id="1234", platform="test_platform", name="Hello World", ), - "test_domain.no_name": RegistryEntry( + "test_domain.no_name": RegistryEntryWithDefaults( entity_id="test_domain.no_name", unique_id="6789", platform="test_platform", @@ -117,13 +117,13 @@ async def test_list_entities( mock_registry( hass, { - "test_domain.name": RegistryEntry( + "test_domain.name": RegistryEntryWithDefaults( entity_id="test_domain.name", unique_id="1234", platform="test_platform", name="Hello World", ), - "test_domain.name_2": RegistryEntry( + "test_domain.name_2": RegistryEntryWithDefaults( entity_id="test_domain.name_2", unique_id="6789", platform="test_platform", @@ -169,7 +169,7 @@ async def test_list_entities_for_display( mock_registry( hass, { - "test_domain.test": RegistryEntry( + "test_domain.test": RegistryEntryWithDefaults( area_id="area52", device_id="device123", entity_category=EntityCategory.DIAGNOSTIC, @@ -181,7 +181,7 @@ async def test_list_entities_for_display( translation_key="translations_galore", unique_id="1234", ), - "test_domain.nameless": RegistryEntry( + "test_domain.nameless": RegistryEntryWithDefaults( area_id="area52", device_id="device123", entity_id="test_domain.nameless", @@ -191,7 +191,7 @@ async def test_list_entities_for_display( platform="test_platform", unique_id="2345", ), - "test_domain.renamed": RegistryEntry( + "test_domain.renamed": RegistryEntryWithDefaults( area_id="area52", device_id="device123", entity_id="test_domain.renamed", @@ -201,31 +201,31 @@ async def test_list_entities_for_display( platform="test_platform", unique_id="3456", ), - "test_domain.boring": RegistryEntry( + "test_domain.boring": RegistryEntryWithDefaults( entity_id="test_domain.boring", platform="test_platform", unique_id="4567", ), - "test_domain.disabled": RegistryEntry( + "test_domain.disabled": RegistryEntryWithDefaults( disabled_by=RegistryEntryDisabler.USER, entity_id="test_domain.disabled", hidden_by=RegistryEntryHider.USER, platform="test_platform", unique_id="789A", ), - "test_domain.hidden": RegistryEntry( + "test_domain.hidden": RegistryEntryWithDefaults( entity_id="test_domain.hidden", hidden_by=RegistryEntryHider.USER, platform="test_platform", unique_id="89AB", ), - "sensor.default_precision": RegistryEntry( + "sensor.default_precision": RegistryEntryWithDefaults( entity_id="sensor.default_precision", options={"sensor": {"suggested_display_precision": 0}}, platform="test_platform", unique_id="9ABC", ), - "sensor.user_precision": RegistryEntry( + "sensor.user_precision": RegistryEntryWithDefaults( entity_id="sensor.user_precision", options={ "sensor": {"display_precision": 0, "suggested_display_precision": 1} @@ -303,7 +303,7 @@ async def test_list_entities_for_display( mock_registry( hass, { - "test_domain.test": RegistryEntry( + "test_domain.test": RegistryEntryWithDefaults( area_id="area52", device_id="device123", entity_id="test_domain.test", @@ -312,7 +312,7 @@ async def test_list_entities_for_display( platform="test_platform", unique_id="1234", ), - "test_domain.name_2": RegistryEntry( + "test_domain.name_2": RegistryEntryWithDefaults( entity_id="test_domain.name_2", has_entity_name=True, original_name=Unserializable(), @@ -348,7 +348,7 @@ async def test_get_entity(hass: HomeAssistant, client: MockHAClientWebSocket) -> mock_registry( hass, { - "test_domain.name": RegistryEntry( + "test_domain.name": RegistryEntryWithDefaults( entity_id="test_domain.name", unique_id="1234", platform="test_platform", @@ -356,7 +356,7 @@ async def test_get_entity(hass: HomeAssistant, client: MockHAClientWebSocket) -> created_at=name_created_at, modified_at=name_created_at, ), - "test_domain.no_name": RegistryEntry( + "test_domain.no_name": RegistryEntryWithDefaults( entity_id="test_domain.no_name", unique_id="6789", platform="test_platform", @@ -445,7 +445,7 @@ async def test_get_entities(hass: HomeAssistant, client: MockHAClientWebSocket) mock_registry( hass, { - "test_domain.name": RegistryEntry( + "test_domain.name": RegistryEntryWithDefaults( entity_id="test_domain.name", unique_id="1234", platform="test_platform", @@ -453,7 +453,7 @@ async def test_get_entities(hass: HomeAssistant, client: MockHAClientWebSocket) created_at=name_created_at, modified_at=name_created_at, ), - "test_domain.no_name": RegistryEntry( + "test_domain.no_name": RegistryEntryWithDefaults( entity_id="test_domain.no_name", unique_id="6789", platform="test_platform", @@ -545,7 +545,7 @@ async def test_update_entity( registry = mock_registry( hass, { - "test_domain.world": RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -1009,7 +1009,7 @@ async def test_update_entity_no_changes( mock_registry( hass, { - "test_domain.world": RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -1110,7 +1110,7 @@ async def test_update_entity_id( mock_registry( hass, { - "test_domain.world": RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -1179,13 +1179,13 @@ async def test_update_existing_entity_id( mock_registry( hass, { - "test_domain.world": RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" platform="test_platform", ), - "test_domain.planet": RegistryEntry( + "test_domain.planet": RegistryEntryWithDefaults( entity_id="test_domain.planet", unique_id="2345", # Using component.async_add_entities is equal to platform "domain" @@ -1217,7 +1217,7 @@ async def test_update_invalid_entity_id( mock_registry( hass, { - "test_domain.world": RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -1249,7 +1249,7 @@ async def test_remove_entity( registry = mock_registry( hass, { - "test_domain.world": RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" diff --git a/tests/components/device_tracker/test_init.py b/tests/components/device_tracker/test_init.py index ea07365bd2f..94e1803a92d 100644 --- a/tests/components/device_tracker/test_init.py +++ b/tests/components/device_tracker/test_init.py @@ -25,7 +25,6 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant, State, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import discovery -from homeassistant.helpers.entity_registry import RegistryEntry from homeassistant.helpers.json import JSONEncoder from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util @@ -34,6 +33,7 @@ from . import common from .common import MockScanner, mock_legacy_device_tracker_setup from tests.common import ( + RegistryEntryWithDefaults, assert_setup_component, async_fire_time_changed, mock_registry, @@ -400,7 +400,7 @@ async def test_see_service_guard_config_entry( mock_registry( hass, { - entity_id: RegistryEntry( + entity_id: RegistryEntryWithDefaults( entity_id=entity_id, unique_id=1, platform=const.DOMAIN ) }, diff --git a/tests/components/harmony/test_init.py b/tests/components/harmony/test_init.py index 971983fc3b6..10befc40b8e 100644 --- a/tests/components/harmony/test_init.py +++ b/tests/components/harmony/test_init.py @@ -17,7 +17,7 @@ from .const import ( WATCH_TV_ACTIVITY_ID, ) -from tests.common import MockConfigEntry, mock_registry +from tests.common import MockConfigEntry, RegistryEntryWithDefaults, mock_registry async def test_unique_id_migration( @@ -33,35 +33,35 @@ async def test_unique_id_migration( hass, { # old format - ENTITY_WATCH_TV: er.RegistryEntry( + ENTITY_WATCH_TV: RegistryEntryWithDefaults( entity_id=ENTITY_WATCH_TV, unique_id="123443-Watch TV", platform="harmony", config_entry_id=entry.entry_id, ), # old format, activity name with - - ENTITY_NILE_TV: er.RegistryEntry( + ENTITY_NILE_TV: RegistryEntryWithDefaults( entity_id=ENTITY_NILE_TV, unique_id="123443-Nile-TV", platform="harmony", config_entry_id=entry.entry_id, ), # new format - ENTITY_PLAY_MUSIC: er.RegistryEntry( + ENTITY_PLAY_MUSIC: RegistryEntryWithDefaults( entity_id=ENTITY_PLAY_MUSIC, unique_id=f"activity_{PLAY_MUSIC_ACTIVITY_ID}", platform="harmony", config_entry_id=entry.entry_id, ), # old entity which no longer has a matching activity on the hub. skipped. - "switch.some_other_activity": er.RegistryEntry( + "switch.some_other_activity": RegistryEntryWithDefaults( entity_id="switch.some_other_activity", unique_id="123443-Some Other Activity", platform="harmony", config_entry_id=entry.entry_id, ), # select entity - ENTITY_SELECT: er.RegistryEntry( + ENTITY_SELECT: RegistryEntryWithDefaults( entity_id=ENTITY_SELECT, unique_id=f"{HUB_NAME}_activities", platform="harmony", diff --git a/tests/components/overkiz/test_init.py b/tests/components/overkiz/test_init.py index ba4de56ad86..d1961d79735 100644 --- a/tests/components/overkiz/test_init.py +++ b/tests/components/overkiz/test_init.py @@ -7,7 +7,7 @@ from homeassistant.setup import async_setup_component from .test_config_flow import TEST_EMAIL, TEST_GATEWAY_ID, TEST_PASSWORD, TEST_SERVER -from tests.common import MockConfigEntry, mock_registry +from tests.common import MockConfigEntry, RegistryEntryWithDefaults, mock_registry ENTITY_SENSOR_DISCRETE_RSSI_LEVEL = "sensor.zipscreen_woonkamer_discrete_rssi_level" ENTITY_ALARM_CONTROL_PANEL = "alarm_control_panel.alarm" @@ -33,35 +33,35 @@ async def test_unique_id_migration(hass: HomeAssistant) -> None: hass, { # This entity will be migrated to "io://1234-5678-1234/3541212-core:DiscreteRSSILevelState" - ENTITY_SENSOR_DISCRETE_RSSI_LEVEL: er.RegistryEntry( + ENTITY_SENSOR_DISCRETE_RSSI_LEVEL: RegistryEntryWithDefaults( entity_id=ENTITY_SENSOR_DISCRETE_RSSI_LEVEL, unique_id="io://1234-5678-1234/3541212-OverkizState.CORE_DISCRETE_RSSI_LEVEL", platform=DOMAIN, config_entry_id=mock_entry.entry_id, ), # This entity will be migrated to "internal://1234-5678-1234/alarm/0-TSKAlarmController" - ENTITY_ALARM_CONTROL_PANEL: er.RegistryEntry( + ENTITY_ALARM_CONTROL_PANEL: RegistryEntryWithDefaults( entity_id=ENTITY_ALARM_CONTROL_PANEL, unique_id="internal://1234-5678-1234/alarm/0-UIWidget.TSKALARM_CONTROLLER", platform=DOMAIN, config_entry_id=mock_entry.entry_id, ), # This entity will be migrated to "io://1234-5678-1234/0-OnOff" - ENTITY_SWITCH_GARAGE: er.RegistryEntry( + ENTITY_SWITCH_GARAGE: RegistryEntryWithDefaults( entity_id=ENTITY_SWITCH_GARAGE, unique_id="io://1234-5678-1234/0-UIClass.ON_OFF", platform=DOMAIN, config_entry_id=mock_entry.entry_id, ), # This entity will be removed since "io://1234-5678-1234/3541212-core:TargetClosureState" already exists - ENTITY_SENSOR_TARGET_CLOSURE_STATE: er.RegistryEntry( + ENTITY_SENSOR_TARGET_CLOSURE_STATE: RegistryEntryWithDefaults( entity_id=ENTITY_SENSOR_TARGET_CLOSURE_STATE, unique_id="io://1234-5678-1234/3541212-OverkizState.CORE_TARGET_CLOSURE", platform=DOMAIN, config_entry_id=mock_entry.entry_id, ), # This entity will not be migrated" - ENTITY_SENSOR_TARGET_CLOSURE_STATE_2: er.RegistryEntry( + ENTITY_SENSOR_TARGET_CLOSURE_STATE_2: RegistryEntryWithDefaults( entity_id=ENTITY_SENSOR_TARGET_CLOSURE_STATE_2, unique_id="io://1234-5678-1234/3541212-core:TargetClosureState", platform=DOMAIN, diff --git a/tests/components/sleepiq/test_init.py b/tests/components/sleepiq/test_init.py index 216d0e49b08..65e9e63a372 100644 --- a/tests/components/sleepiq/test_init.py +++ b/tests/components/sleepiq/test_init.py @@ -29,7 +29,12 @@ from .conftest import ( setup_platform, ) -from tests.common import MockConfigEntry, async_fire_time_changed, mock_registry +from tests.common import ( + MockConfigEntry, + RegistryEntryWithDefaults, + async_fire_time_changed, + mock_registry, +) ENTITY_IS_IN_BED = f"sensor.sleepnumber_{BED_ID}_{SLEEPER_L_NAME_LOWER}_{IS_IN_BED}" ENTITY_PRESSURE = f"sensor.sleepnumber_{BED_ID}_{SLEEPER_L_NAME_LOWER}_{PRESSURE}" @@ -103,19 +108,19 @@ async def test_unique_id_migration(hass: HomeAssistant, mock_asyncsleepiq) -> No mock_registry( hass, { - ENTITY_IS_IN_BED: er.RegistryEntry( + ENTITY_IS_IN_BED: RegistryEntryWithDefaults( entity_id=ENTITY_IS_IN_BED, unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{IS_IN_BED}", platform=DOMAIN, config_entry_id=mock_entry.entry_id, ), - ENTITY_PRESSURE: er.RegistryEntry( + ENTITY_PRESSURE: RegistryEntryWithDefaults( entity_id=ENTITY_PRESSURE, unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{PRESSURE}", platform=DOMAIN, config_entry_id=mock_entry.entry_id, ), - ENTITY_SLEEP_NUMBER: er.RegistryEntry( + ENTITY_SLEEP_NUMBER: RegistryEntryWithDefaults( entity_id=ENTITY_SLEEP_NUMBER, unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{SLEEP_NUMBER}", platform=DOMAIN, diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index 04ea0295d72..04159a91d6b 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -44,6 +44,7 @@ from tests.common import ( MockEntityPlatform, MockModule, MockPlatform, + RegistryEntryWithDefaults, mock_integration, mock_registry, ) @@ -683,7 +684,7 @@ async def test_warn_disabled( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test we warn once if we write to a disabled entity.""" - entry = er.RegistryEntry( + entry = RegistryEntryWithDefaults( entity_id="hello.world", unique_id="test-unique-id", platform="test-platform", @@ -710,7 +711,7 @@ async def test_warn_disabled( async def test_disabled_in_entity_registry(hass: HomeAssistant) -> None: """Test entity is removed if we disable entity registry entry.""" - entry = er.RegistryEntry( + entry = RegistryEntryWithDefaults( entity_id="hello.world", unique_id="test-unique-id", platform="test-platform", diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index 41b7271150a..8a1bdcb2f0c 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -48,6 +48,7 @@ from tests.common import ( MockEntity, MockEntityPlatform, MockPlatform, + RegistryEntryWithDefaults, async_fire_time_changed, mock_platform, mock_registry, @@ -752,7 +753,7 @@ async def test_overriding_name_from_registry(hass: HomeAssistant) -> None: mock_registry( hass, { - "test_domain.world": er.RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -785,7 +786,7 @@ async def test_registry_respect_entity_disabled(hass: HomeAssistant) -> None: mock_registry( hass, { - "test_domain.world": er.RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -832,7 +833,7 @@ async def test_entity_registry_updates_name(hass: HomeAssistant) -> None: registry = mock_registry( hass, { - "test_domain.world": er.RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -1065,7 +1066,7 @@ async def test_entity_registry_updates_entity_id(hass: HomeAssistant) -> None: registry = mock_registry( hass, { - "test_domain.world": er.RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" @@ -1097,14 +1098,14 @@ async def test_entity_registry_updates_invalid_entity_id(hass: HomeAssistant) -> registry = mock_registry( hass, { - "test_domain.world": er.RegistryEntry( + "test_domain.world": RegistryEntryWithDefaults( entity_id="test_domain.world", unique_id="1234", # Using component.async_add_entities is equal to platform "domain" platform="test_platform", name="Some name", ), - "test_domain.existing": er.RegistryEntry( + "test_domain.existing": RegistryEntryWithDefaults( entity_id="test_domain.existing", unique_id="5678", platform="test_platform", @@ -1529,14 +1530,19 @@ async def test_entity_info_added_to_entity_registry( entry_default = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "default") assert entry_default == er.RegistryEntry( - "test_domain.best_name", - "default", - "test_domain", + entity_id="test_domain.best_name", + unique_id="default", + platform="test_domain", capabilities={"max": 100}, + config_entry_id=None, + config_subentry_id=None, created_at=dt_util.utcnow(), device_class=None, + device_id=None, + disabled_by=None, entity_category=EntityCategory.CONFIG, has_entity_name=True, + hidden_by=None, icon=None, id=ANY, modified_at=dt_util.utcnow(), @@ -1544,6 +1550,7 @@ async def test_entity_info_added_to_entity_registry( original_device_class="mock-device-class", original_icon="nice:icon", original_name="best name", + options=None, supported_features=5, translation_key="my_translation_key", unit_of_measurement=PERCENTAGE, diff --git a/tests/helpers/test_entity_registry.py b/tests/helpers/test_entity_registry.py index 416f2d5121d..dd27c0eff0d 100644 --- a/tests/helpers/test_entity_registry.py +++ b/tests/helpers/test_entity_registry.py @@ -24,6 +24,7 @@ from homeassistant.util.dt import utc_from_timestamp from tests.common import ( ANY, MockConfigEntry, + RegistryEntryWithDefaults, async_capture_events, async_fire_time_changed, flush_store, @@ -122,9 +123,9 @@ def test_get_or_create_updates_data( assert set(entity_registry.async_device_ids()) == {orig_device_entry.id} assert orig_entry == er.RegistryEntry( - "light.hue_5678", - "5678", - "hue", + entity_id="light.hue_5678", + unique_id="5678", + platform="hue", capabilities={"max": 100}, config_entry_id=orig_config_entry.entry_id, config_subentry_id=config_subentry_id, @@ -139,6 +140,7 @@ def test_get_or_create_updates_data( id=orig_entry.id, modified_at=created, name=None, + options=None, original_device_class="mock-device-class", original_icon="initial-original_icon", original_name="initial-original_name", @@ -177,9 +179,9 @@ def test_get_or_create_updates_data( ) assert new_entry == er.RegistryEntry( - "light.hue_5678", - "5678", - "hue", + entity_id="light.hue_5678", + unique_id="5678", + platform="hue", aliases=set(), area_id=None, capabilities={"new-max": 150}, @@ -196,6 +198,7 @@ def test_get_or_create_updates_data( id=orig_entry.id, modified_at=modified, name=None, + options=None, original_device_class="new-mock-device-class", original_icon="updated-original_icon", original_name="updated-original_name", @@ -228,13 +231,14 @@ def test_get_or_create_updates_data( ) assert new_entry == er.RegistryEntry( - "light.hue_5678", - "5678", - "hue", + entity_id="light.hue_5678", + unique_id="5678", + platform="hue", aliases=set(), area_id=None, capabilities=None, config_entry_id=None, + config_subentry_id=None, created_at=created, device_class=None, device_id=None, @@ -246,6 +250,7 @@ def test_get_or_create_updates_data( id=orig_entry.id, modified_at=modified, name=None, + options=None, original_device_class=None, original_icon=None, original_name=None, @@ -2095,8 +2100,12 @@ def test_entity_registry_items() -> None: assert entities.get_entity_id(("a", "b", "c")) is None assert entities.get_entry("abc") is None - entry1 = er.RegistryEntry("test.entity1", "1234", "hue") - entry2 = er.RegistryEntry("test.entity2", "2345", "hue") + entry1 = RegistryEntryWithDefaults( + entity_id="test.entity1", unique_id="1234", platform="hue" + ) + entry2 = RegistryEntryWithDefaults( + entity_id="test.entity2", unique_id="2345", platform="hue" + ) entities["test.entity1"] = entry1 entities["test.entity2"] = entry2 diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 70ab20e87fa..4582bce3e05 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -49,6 +49,7 @@ from tests.common import ( MockEntity, MockModule, MockUser, + RegistryEntryWithDefaults, async_mock_service, mock_area_registry, mock_device_registry, @@ -158,94 +159,94 @@ def floor_area_mock(hass: HomeAssistant) -> None: }, ) - entity_in_own_area = er.RegistryEntry( + entity_in_own_area = RegistryEntryWithDefaults( entity_id="light.in_own_area", unique_id="in-own-area-id", platform="test", area_id="own-area", ) - config_entity_in_own_area = er.RegistryEntry( + config_entity_in_own_area = RegistryEntryWithDefaults( entity_id="light.config_in_own_area", unique_id="config-in-own-area-id", platform="test", area_id="own-area", entity_category=EntityCategory.CONFIG, ) - hidden_entity_in_own_area = er.RegistryEntry( + hidden_entity_in_own_area = RegistryEntryWithDefaults( entity_id="light.hidden_in_own_area", unique_id="hidden-in-own-area-id", platform="test", area_id="own-area", hidden_by=er.RegistryEntryHider.USER, ) - entity_in_area = er.RegistryEntry( + entity_in_area = RegistryEntryWithDefaults( entity_id="light.in_area", unique_id="in-area-id", platform="test", device_id=device_in_area.id, ) - config_entity_in_area = er.RegistryEntry( + config_entity_in_area = RegistryEntryWithDefaults( entity_id="light.config_in_area", unique_id="config-in-area-id", platform="test", device_id=device_in_area.id, entity_category=EntityCategory.CONFIG, ) - hidden_entity_in_area = er.RegistryEntry( + hidden_entity_in_area = RegistryEntryWithDefaults( entity_id="light.hidden_in_area", unique_id="hidden-in-area-id", platform="test", device_id=device_in_area.id, hidden_by=er.RegistryEntryHider.USER, ) - entity_in_other_area = er.RegistryEntry( + entity_in_other_area = RegistryEntryWithDefaults( entity_id="light.in_other_area", unique_id="in-area-a-id", platform="test", device_id=device_in_area.id, area_id="other-area", ) - entity_assigned_to_area = er.RegistryEntry( + entity_assigned_to_area = RegistryEntryWithDefaults( entity_id="light.assigned_to_area", unique_id="assigned-area-id", platform="test", device_id=device_in_area.id, area_id="test-area", ) - entity_no_area = er.RegistryEntry( + entity_no_area = RegistryEntryWithDefaults( entity_id="light.no_area", unique_id="no-area-id", platform="test", device_id=device_no_area.id, ) - config_entity_no_area = er.RegistryEntry( + config_entity_no_area = RegistryEntryWithDefaults( entity_id="light.config_no_area", unique_id="config-no-area-id", platform="test", device_id=device_no_area.id, entity_category=EntityCategory.CONFIG, ) - hidden_entity_no_area = er.RegistryEntry( + hidden_entity_no_area = RegistryEntryWithDefaults( entity_id="light.hidden_no_area", unique_id="hidden-no-area-id", platform="test", device_id=device_no_area.id, hidden_by=er.RegistryEntryHider.USER, ) - entity_diff_area = er.RegistryEntry( + entity_diff_area = RegistryEntryWithDefaults( entity_id="light.diff_area", unique_id="diff-area-id", platform="test", device_id=device_diff_area.id, ) - entity_in_area_a = er.RegistryEntry( + entity_in_area_a = RegistryEntryWithDefaults( entity_id="light.in_area_a", unique_id="in-area-a-id", platform="test", device_id=device_area_a.id, area_id="area-a", ) - entity_in_area_b = er.RegistryEntry( + entity_in_area_b = RegistryEntryWithDefaults( entity_id="light.in_area_b", unique_id="in-area-b-id", platform="test", @@ -329,53 +330,53 @@ def label_mock(hass: HomeAssistant) -> None: }, ) - entity_with_my_label = er.RegistryEntry( + entity_with_my_label = RegistryEntryWithDefaults( entity_id="light.with_my_label", unique_id="with_my_label", platform="test", labels={"my-label"}, ) - hidden_entity_with_my_label = er.RegistryEntry( + hidden_entity_with_my_label = RegistryEntryWithDefaults( entity_id="light.hidden_with_my_label", unique_id="hidden_with_my_label", platform="test", labels={"my-label"}, hidden_by=er.RegistryEntryHider.USER, ) - config_entity_with_my_label = er.RegistryEntry( + config_entity_with_my_label = RegistryEntryWithDefaults( entity_id="light.config_with_my_label", unique_id="config_with_my_label", platform="test", labels={"my-label"}, entity_category=EntityCategory.CONFIG, ) - entity_with_label1_from_device = er.RegistryEntry( + entity_with_label1_from_device = RegistryEntryWithDefaults( entity_id="light.with_label1_from_device", unique_id="with_label1_from_device", platform="test", device_id=device_has_label1.id, ) - entity_with_label1_from_device_and_different_area = er.RegistryEntry( + entity_with_label1_from_device_and_different_area = RegistryEntryWithDefaults( entity_id="light.with_label1_from_device_diff_area", unique_id="with_label1_from_device_diff_area", platform="test", device_id=device_has_label1.id, area_id=area_without_labels.id, ) - entity_with_label1_and_label2_from_device = er.RegistryEntry( + entity_with_label1_and_label2_from_device = RegistryEntryWithDefaults( entity_id="light.with_label1_and_label2_from_device", unique_id="with_label1_and_label2_from_device", platform="test", labels={"label1"}, device_id=device_has_label2.id, ) - entity_with_labels_from_device = er.RegistryEntry( + entity_with_labels_from_device = RegistryEntryWithDefaults( entity_id="light.with_labels_from_device", unique_id="with_labels_from_device", platform="test", device_id=device_has_labels.id, ) - entity_with_no_labels = er.RegistryEntry( + entity_with_no_labels = RegistryEntryWithDefaults( entity_id="light.no_labels", unique_id="no_labels", platform="test", @@ -1697,7 +1698,7 @@ async def test_domain_control_unauthorized( mock_registry( hass, { - "light.kitchen": er.RegistryEntry( + "light.kitchen": RegistryEntryWithDefaults( entity_id="light.kitchen", unique_id="kitchen", platform="test_domain", @@ -1738,7 +1739,7 @@ async def test_domain_control_admin( mock_registry( hass, { - "light.kitchen": er.RegistryEntry( + "light.kitchen": RegistryEntryWithDefaults( entity_id="light.kitchen", unique_id="kitchen", platform="test_domain", @@ -1776,7 +1777,7 @@ async def test_domain_control_no_user(hass: HomeAssistant) -> None: mock_registry( hass, { - "light.kitchen": er.RegistryEntry( + "light.kitchen": RegistryEntryWithDefaults( entity_id="light.kitchen", unique_id="kitchen", platform="test_domain",