Enforce RegistryEntryDisabler enum (#69017)

Co-authored-by: Franck Nijhof <git@frenck.dev>
pull/69031/head
Paulus Schoutsen 2022-03-31 13:10:07 -07:00 committed by GitHub
parent a9a14d6544
commit 5eb19b8a70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 41 deletions

View File

@ -44,7 +44,6 @@ from homeassistant.util.yaml import load_yaml
from . import device_registry as dr, storage from . import device_registry as dr, storage
from .device_registry import EVENT_DEVICE_REGISTRY_UPDATED from .device_registry import EVENT_DEVICE_REGISTRY_UPDATED
from .frame import report
from .typing import UNDEFINED, UndefinedType from .typing import UNDEFINED, UndefinedType
if TYPE_CHECKING: if TYPE_CHECKING:
@ -92,14 +91,6 @@ class RegistryEntryHider(StrEnum):
USER = "user" 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( def _convert_to_entity_category(
value: EntityCategory | str | None, raise_report: bool = True value: EntityCategory | str | None, raise_report: bool = True
) -> EntityCategory | None: ) -> EntityCategory | None:
@ -389,17 +380,10 @@ class EntityRegistry:
domain, suggested_object_id or f"{platform}_{unique_id}", known_object_ids domain, suggested_object_id or f"{platform}_{unique_id}", known_object_ids
) )
if isinstance(disabled_by, str) and not isinstance( if disabled_by and not isinstance(disabled_by, RegistryEntryDisabler):
disabled_by, RegistryEntryDisabler raise ValueError("disabled_by must be a RegistryEntryDisabler value")
):
report( # type: ignore[unreachable] if (
"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 (
disabled_by is None disabled_by is None
and config_entry and config_entry
and config_entry.pref_disable_new_entities and config_entry.pref_disable_new_entities
@ -537,16 +521,12 @@ class EntityRegistry:
new_values: dict[str, Any] = {} # Dict with new key/value pairs new_values: dict[str, Any] = {} # Dict with new key/value pairs
old_values: dict[str, Any] = {} # Dict with old key/value pairs old_values: dict[str, Any] = {} # Dict with old key/value pairs
if isinstance(disabled_by, str) and not isinstance( if (
disabled_by, RegistryEntryDisabler disabled_by
and disabled_by is not UNDEFINED
and not isinstance(disabled_by, RegistryEntryDisabler)
): ):
report( # type: ignore[unreachable] raise ValueError("disabled_by must be a RegistryEntryDisabler value")
"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)
for attr_name, value in ( for attr_name, value in (
("area_id", area_id), ("area_id", area_id),

View File

@ -782,9 +782,12 @@ async def test_disabling_entity(
assert "climate.heatpump" in registry.entities assert "climate.heatpump" in registry.entities
registry.async_update_entity( registry.async_update_entity(
entity_id=data["sensor_1"].entity_id, 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() await hass.async_block_till_done()
body = await generate_latest_metrics(client) body = await generate_latest_metrics(client)

View File

@ -1217,17 +1217,20 @@ def test_entity_registry_items():
assert entities.get_entry(entry2.id) is None assert entities.get_entry(entry2.id) is None
async def test_deprecated_disabled_by_str(hass, registry, caplog): async def test_disabled_by_str_not_allowed(hass):
"""Test deprecated str use of disabled_by converts to enum and logs a warning.""" """Test we need to pass entity category type."""
entry = registry.async_get_or_create( reg = er.async_get(hass)
domain="light.kitchen",
platform="hue",
unique_id="5678",
disabled_by=er.RegistryEntryDisabler.USER.value,
)
assert entry.disabled_by is er.RegistryEntryDisabler.USER with pytest.raises(ValueError):
assert " str for entity registry disabled_by. This is deprecated " in caplog.text 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): async def test_deprecated_entity_category_str(hass, registry, caplog):