mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Make device entry disabled by an enum (#60239)
This commit is contained in:
@@ -41,10 +41,6 @@ CONNECTION_NETWORK_MAC = "mac"
|
||||
CONNECTION_UPNP = "upnp"
|
||||
CONNECTION_ZIGBEE = "zigbee"
|
||||
|
||||
DISABLED_CONFIG_ENTRY = "config_entry"
|
||||
DISABLED_INTEGRATION = "integration"
|
||||
DISABLED_USER = "user"
|
||||
|
||||
ORPHANED_DEVICE_KEEP_SECONDS = 86400 * 30
|
||||
|
||||
|
||||
@@ -53,6 +49,14 @@ class _DeviceIndex(NamedTuple):
|
||||
connections: dict[tuple[str, str], str]
|
||||
|
||||
|
||||
class DeviceEntryDisabler(StrEnum):
|
||||
"""What disabled a device entry."""
|
||||
|
||||
CONFIG_ENTRY = "config_entry"
|
||||
INTEGRATION = "integration"
|
||||
USER = "user"
|
||||
|
||||
|
||||
class DeviceEntryType(StrEnum):
|
||||
"""Device entry type."""
|
||||
|
||||
@@ -67,17 +71,7 @@ class DeviceEntry:
|
||||
config_entries: set[str] = attr.ib(converter=set, factory=set)
|
||||
configuration_url: str | None = attr.ib(default=None)
|
||||
connections: set[tuple[str, str]] = attr.ib(converter=set, factory=set)
|
||||
disabled_by: str | None = attr.ib(
|
||||
default=None,
|
||||
validator=attr.validators.in_(
|
||||
(
|
||||
DISABLED_CONFIG_ENTRY,
|
||||
DISABLED_INTEGRATION,
|
||||
DISABLED_USER,
|
||||
None,
|
||||
)
|
||||
),
|
||||
)
|
||||
disabled_by: DeviceEntryDisabler | None = attr.ib(default=None)
|
||||
entry_type: DeviceEntryType | None = attr.ib(default=None)
|
||||
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
|
||||
identifiers: set[tuple[str, str]] = attr.ib(converter=set, factory=set)
|
||||
@@ -302,7 +296,7 @@ class DeviceRegistry:
|
||||
default_model: str | None | UndefinedType = UNDEFINED,
|
||||
default_name: str | None | UndefinedType = UNDEFINED,
|
||||
# To disable a device if it gets created
|
||||
disabled_by: str | None | UndefinedType = UNDEFINED,
|
||||
disabled_by: DeviceEntryDisabler | None | UndefinedType = UNDEFINED,
|
||||
entry_type: DeviceEntryType | None | UndefinedType = UNDEFINED,
|
||||
identifiers: set[tuple[str, str]] | None = None,
|
||||
manufacturer: str | None | UndefinedType = UNDEFINED,
|
||||
@@ -389,7 +383,7 @@ class DeviceRegistry:
|
||||
add_config_entry_id: str | UndefinedType = UNDEFINED,
|
||||
area_id: str | None | UndefinedType = UNDEFINED,
|
||||
configuration_url: str | None | UndefinedType = UNDEFINED,
|
||||
disabled_by: str | None | UndefinedType = UNDEFINED,
|
||||
disabled_by: DeviceEntryDisabler | None | UndefinedType = UNDEFINED,
|
||||
manufacturer: str | None | UndefinedType = UNDEFINED,
|
||||
model: str | None | UndefinedType = UNDEFINED,
|
||||
name_by_user: str | None | UndefinedType = UNDEFINED,
|
||||
@@ -426,7 +420,7 @@ class DeviceRegistry:
|
||||
add_config_entry_id: str | UndefinedType = UNDEFINED,
|
||||
area_id: str | None | UndefinedType = UNDEFINED,
|
||||
configuration_url: str | None | UndefinedType = UNDEFINED,
|
||||
disabled_by: str | None | UndefinedType = UNDEFINED,
|
||||
disabled_by: DeviceEntryDisabler | None | UndefinedType = UNDEFINED,
|
||||
entry_type: DeviceEntryType | None | UndefinedType = UNDEFINED,
|
||||
manufacturer: str | None | UndefinedType = UNDEFINED,
|
||||
merge_connections: set[tuple[str, str]] | UndefinedType = UNDEFINED,
|
||||
@@ -447,6 +441,16 @@ class DeviceRegistry:
|
||||
|
||||
config_entries = old.config_entries
|
||||
|
||||
if isinstance(disabled_by, str) and not isinstance(
|
||||
disabled_by, DeviceEntryDisabler
|
||||
):
|
||||
report( # type: ignore[unreachable]
|
||||
"uses str for device registry disabled_by. This is deprecated, "
|
||||
"it should be updated to use DeviceEntryDisabler instead",
|
||||
error_if_core=False,
|
||||
)
|
||||
disabled_by = DeviceEntryDisabler(disabled_by)
|
||||
|
||||
if (
|
||||
suggested_area not in (UNDEFINED, None, "")
|
||||
and area_id is UNDEFINED
|
||||
@@ -737,7 +741,7 @@ def async_config_entry_disabled_by_changed(
|
||||
Disable devices in the registry that are associated with a config entry when
|
||||
the config entry is disabled, enable devices in the registry that are associated
|
||||
with a config entry when the config entry is enabled and the devices are marked
|
||||
DISABLED_CONFIG_ENTRY.
|
||||
DeviceEntryDisabler.CONFIG_ENTRY.
|
||||
Only disable a device if all associated config entries are disabled.
|
||||
"""
|
||||
|
||||
@@ -745,7 +749,7 @@ def async_config_entry_disabled_by_changed(
|
||||
|
||||
if not config_entry.disabled_by:
|
||||
for device in devices:
|
||||
if device.disabled_by != DISABLED_CONFIG_ENTRY:
|
||||
if device.disabled_by is not DeviceEntryDisabler.CONFIG_ENTRY:
|
||||
continue
|
||||
registry.async_update_device(device.id, disabled_by=None)
|
||||
return
|
||||
@@ -764,7 +768,9 @@ def async_config_entry_disabled_by_changed(
|
||||
enabled_config_entries
|
||||
):
|
||||
continue
|
||||
registry.async_update_device(device.id, disabled_by=DISABLED_CONFIG_ENTRY)
|
||||
registry.async_update_device(
|
||||
device.id, disabled_by=DeviceEntryDisabler.CONFIG_ENTRY
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
|
||||
Reference in New Issue
Block a user