From 5eb19b8a705334b480febac4f6ec62282696a29a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 31 Mar 2022 13:10:07 -0700 Subject: [PATCH] Enforce RegistryEntryDisabler enum (#69017) Co-authored-by: Franck Nijhof --- homeassistant/helpers/entity_registry.py | 38 ++++++------------------ tests/components/prometheus/test_init.py | 7 +++-- tests/helpers/test_entity_registry.py | 23 +++++++------- 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index c02ffde4672..6c1c443ad2b 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -44,7 +44,6 @@ from homeassistant.util.yaml import load_yaml from . import device_registry as dr, storage from .device_registry import EVENT_DEVICE_REGISTRY_UPDATED -from .frame import report from .typing import UNDEFINED, UndefinedType if TYPE_CHECKING: @@ -92,14 +91,6 @@ class RegistryEntryHider(StrEnum): USER = "user" -# DISABLED_* are deprecated, to be removed in 2022.3 -DISABLED_CONFIG_ENTRY = RegistryEntryDisabler.CONFIG_ENTRY.value -DISABLED_DEVICE = RegistryEntryDisabler.DEVICE.value -DISABLED_HASS = RegistryEntryDisabler.HASS.value -DISABLED_INTEGRATION = RegistryEntryDisabler.INTEGRATION.value -DISABLED_USER = RegistryEntryDisabler.USER.value - - def _convert_to_entity_category( value: EntityCategory | str | None, raise_report: bool = True ) -> EntityCategory | None: @@ -389,17 +380,10 @@ class EntityRegistry: domain, suggested_object_id or f"{platform}_{unique_id}", known_object_ids ) - if isinstance(disabled_by, str) and not isinstance( - disabled_by, RegistryEntryDisabler - ): - report( # type: ignore[unreachable] - "uses str for entity registry disabled_by. This is deprecated and will " - "stop working in Home Assistant 2022.3, it should be updated to use " - "RegistryEntryDisabler instead", - error_if_core=False, - ) - disabled_by = RegistryEntryDisabler(disabled_by) - elif ( + if disabled_by and not isinstance(disabled_by, RegistryEntryDisabler): + raise ValueError("disabled_by must be a RegistryEntryDisabler value") + + if ( disabled_by is None and config_entry and config_entry.pref_disable_new_entities @@ -537,16 +521,12 @@ class EntityRegistry: new_values: dict[str, Any] = {} # Dict with new key/value pairs old_values: dict[str, Any] = {} # Dict with old key/value pairs - if isinstance(disabled_by, str) and not isinstance( - disabled_by, RegistryEntryDisabler + if ( + disabled_by + and disabled_by is not UNDEFINED + and not isinstance(disabled_by, RegistryEntryDisabler) ): - report( # type: ignore[unreachable] - "uses str for entity registry disabled_by. This is deprecated and will " - "stop working in Home Assistant 2022.3, it should be updated to use " - "RegistryEntryDisabler instead", - error_if_core=False, - ) - disabled_by = RegistryEntryDisabler(disabled_by) + raise ValueError("disabled_by must be a RegistryEntryDisabler value") for attr_name, value in ( ("area_id", area_id), diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 096f1405168..5864b551b3c 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -782,9 +782,12 @@ async def test_disabling_entity( assert "climate.heatpump" in registry.entities registry.async_update_entity( entity_id=data["sensor_1"].entity_id, - disabled_by="user", + disabled_by=entity_registry.RegistryEntryDisabler.USER, + ) + registry.async_update_entity( + entity_id="climate.heatpump", + disabled_by=entity_registry.RegistryEntryDisabler.USER, ) - registry.async_update_entity(entity_id="climate.heatpump", disabled_by="user") await hass.async_block_till_done() body = await generate_latest_metrics(client) diff --git a/tests/helpers/test_entity_registry.py b/tests/helpers/test_entity_registry.py index 7dc749b8592..eeac78a2654 100644 --- a/tests/helpers/test_entity_registry.py +++ b/tests/helpers/test_entity_registry.py @@ -1217,17 +1217,20 @@ def test_entity_registry_items(): assert entities.get_entry(entry2.id) is None -async def test_deprecated_disabled_by_str(hass, registry, caplog): - """Test deprecated str use of disabled_by converts to enum and logs a warning.""" - entry = registry.async_get_or_create( - domain="light.kitchen", - platform="hue", - unique_id="5678", - disabled_by=er.RegistryEntryDisabler.USER.value, - ) +async def test_disabled_by_str_not_allowed(hass): + """Test we need to pass entity category type.""" + reg = er.async_get(hass) - assert entry.disabled_by is er.RegistryEntryDisabler.USER - assert " str for entity registry disabled_by. This is deprecated " in caplog.text + with pytest.raises(ValueError): + reg.async_get_or_create( + "light", "hue", "1234", disabled_by=er.RegistryEntryDisabler.USER.value + ) + + entity_id = reg.async_get_or_create("light", "hue", "1234").entity_id + with pytest.raises(ValueError): + reg.async_update_entity( + entity_id, disabled_by=er.RegistryEntryDisabler.USER.value + ) async def test_deprecated_entity_category_str(hass, registry, caplog):