mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Implement suggested_area in the device registry (#45940)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
@@ -71,6 +71,7 @@ class DeviceEntry:
|
||||
)
|
||||
),
|
||||
)
|
||||
suggested_area: Optional[str] = attr.ib(default=None)
|
||||
|
||||
@property
|
||||
def disabled(self) -> bool:
|
||||
@@ -251,6 +252,7 @@ class DeviceRegistry:
|
||||
via_device: Optional[Tuple[str, str]] = None,
|
||||
# To disable a device if it gets created
|
||||
disabled_by: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
suggested_area: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
) -> Optional[DeviceEntry]:
|
||||
"""Get device. Create if it doesn't exist."""
|
||||
if not identifiers and not connections:
|
||||
@@ -304,6 +306,7 @@ class DeviceRegistry:
|
||||
sw_version=sw_version,
|
||||
entry_type=entry_type,
|
||||
disabled_by=disabled_by,
|
||||
suggested_area=suggested_area,
|
||||
)
|
||||
|
||||
@callback
|
||||
@@ -321,6 +324,7 @@ class DeviceRegistry:
|
||||
via_device_id: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
remove_config_entry_id: Union[str, UndefinedType] = UNDEFINED,
|
||||
disabled_by: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
suggested_area: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
) -> Optional[DeviceEntry]:
|
||||
"""Update properties of a device."""
|
||||
return self._async_update_device(
|
||||
@@ -335,6 +339,7 @@ class DeviceRegistry:
|
||||
via_device_id=via_device_id,
|
||||
remove_config_entry_id=remove_config_entry_id,
|
||||
disabled_by=disabled_by,
|
||||
suggested_area=suggested_area,
|
||||
)
|
||||
|
||||
@callback
|
||||
@@ -356,6 +361,7 @@ class DeviceRegistry:
|
||||
area_id: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
name_by_user: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
disabled_by: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
suggested_area: Union[str, None, UndefinedType] = UNDEFINED,
|
||||
) -> Optional[DeviceEntry]:
|
||||
"""Update device attributes."""
|
||||
old = self.devices[device_id]
|
||||
@@ -364,6 +370,16 @@ class DeviceRegistry:
|
||||
|
||||
config_entries = old.config_entries
|
||||
|
||||
if (
|
||||
suggested_area not in (UNDEFINED, None, "")
|
||||
and area_id is UNDEFINED
|
||||
and old.area_id is None
|
||||
):
|
||||
area = self.hass.helpers.area_registry.async_get(
|
||||
self.hass
|
||||
).async_get_or_create(suggested_area)
|
||||
area_id = area.id
|
||||
|
||||
if (
|
||||
add_config_entry_id is not UNDEFINED
|
||||
and add_config_entry_id not in old.config_entries
|
||||
@@ -403,6 +419,7 @@ class DeviceRegistry:
|
||||
("entry_type", entry_type),
|
||||
("via_device_id", via_device_id),
|
||||
("disabled_by", disabled_by),
|
||||
("suggested_area", suggested_area),
|
||||
):
|
||||
if value is not UNDEFINED and value != getattr(old, attr_name):
|
||||
changes[attr_name] = value
|
||||
|
||||
Reference in New Issue
Block a user