Sort some entity_registry code (#59876)
* Sort some entity_registry code * Sort some more entity_registry code * Tweak
This commit is contained in:
parent
87f2eb3bd7
commit
1609c0cc2c
4 changed files with 93 additions and 93 deletions
|
@ -69,9 +69,9 @@ async def websocket_get_entity(hass, connection, msg):
|
||||||
vol.Required("type"): "config/entity_registry/update",
|
vol.Required("type"): "config/entity_registry/update",
|
||||||
vol.Required("entity_id"): cv.entity_id,
|
vol.Required("entity_id"): cv.entity_id,
|
||||||
# If passed in, we update value. Passing None will remove old value.
|
# If passed in, we update value. Passing None will remove old value.
|
||||||
vol.Optional("name"): vol.Any(str, None),
|
|
||||||
vol.Optional("icon"): vol.Any(str, None),
|
|
||||||
vol.Optional("area_id"): vol.Any(str, None),
|
vol.Optional("area_id"): vol.Any(str, None),
|
||||||
|
vol.Optional("icon"): vol.Any(str, None),
|
||||||
|
vol.Optional("name"): vol.Any(str, None),
|
||||||
vol.Optional("new_entity_id"): str,
|
vol.Optional("new_entity_id"): str,
|
||||||
# We only allow setting disabled_by user via API.
|
# We only allow setting disabled_by user via API.
|
||||||
vol.Optional("disabled_by"): vol.Any(DISABLED_USER, None),
|
vol.Optional("disabled_by"): vol.Any(DISABLED_USER, None),
|
||||||
|
@ -92,7 +92,7 @@ async def websocket_update_entity(hass, connection, msg):
|
||||||
|
|
||||||
changes = {}
|
changes = {}
|
||||||
|
|
||||||
for key in ("name", "icon", "area_id", "disabled_by"):
|
for key in ("area_id", "disabled_by", "icon", "name"):
|
||||||
if key in msg:
|
if key in msg:
|
||||||
changes[key] = msg[key]
|
changes[key] = msg[key]
|
||||||
|
|
||||||
|
@ -168,15 +168,15 @@ async def websocket_remove_entity(hass, connection, msg):
|
||||||
def _entry_dict(entry):
|
def _entry_dict(entry):
|
||||||
"""Convert entry to API format."""
|
"""Convert entry to API format."""
|
||||||
return {
|
return {
|
||||||
|
"area_id": entry.area_id,
|
||||||
"config_entry_id": entry.config_entry_id,
|
"config_entry_id": entry.config_entry_id,
|
||||||
"device_id": entry.device_id,
|
"device_id": entry.device_id,
|
||||||
"area_id": entry.area_id,
|
|
||||||
"disabled_by": entry.disabled_by,
|
"disabled_by": entry.disabled_by,
|
||||||
"entity_id": entry.entity_id,
|
|
||||||
"name": entry.name,
|
|
||||||
"icon": entry.icon,
|
|
||||||
"platform": entry.platform,
|
|
||||||
"entity_category": entry.entity_category,
|
"entity_category": entry.entity_category,
|
||||||
|
"entity_id": entry.entity_id,
|
||||||
|
"icon": entry.icon,
|
||||||
|
"name": entry.name,
|
||||||
|
"platform": entry.platform,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,8 +184,8 @@ def _entry_dict(entry):
|
||||||
def _entry_ext_dict(entry):
|
def _entry_ext_dict(entry):
|
||||||
"""Convert entry to API format."""
|
"""Convert entry to API format."""
|
||||||
data = _entry_dict(entry)
|
data = _entry_dict(entry)
|
||||||
data["original_name"] = entry.original_name
|
|
||||||
data["original_icon"] = entry.original_icon
|
|
||||||
data["unique_id"] = entry.unique_id
|
|
||||||
data["capabilities"] = entry.capabilities
|
data["capabilities"] = entry.capabilities
|
||||||
|
data["original_icon"] = entry.original_icon
|
||||||
|
data["original_name"] = entry.original_name
|
||||||
|
data["unique_id"] = entry.unique_id
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -534,26 +534,26 @@ class Entity(ABC):
|
||||||
|
|
||||||
entry = self.registry_entry
|
entry = self.registry_entry
|
||||||
# pylint: disable=consider-using-ternary
|
# pylint: disable=consider-using-ternary
|
||||||
if (name := (entry and entry.name) or self.name) is not None:
|
|
||||||
attr[ATTR_FRIENDLY_NAME] = name
|
|
||||||
|
|
||||||
if (icon := (entry and entry.icon) or self.icon) is not None:
|
|
||||||
attr[ATTR_ICON] = icon
|
|
||||||
|
|
||||||
if (entity_picture := self.entity_picture) is not None:
|
|
||||||
attr[ATTR_ENTITY_PICTURE] = entity_picture
|
|
||||||
|
|
||||||
if assumed_state := self.assumed_state:
|
if assumed_state := self.assumed_state:
|
||||||
attr[ATTR_ASSUMED_STATE] = assumed_state
|
attr[ATTR_ASSUMED_STATE] = assumed_state
|
||||||
|
|
||||||
if (supported_features := self.supported_features) is not None:
|
if (attribution := self.attribution) is not None:
|
||||||
attr[ATTR_SUPPORTED_FEATURES] = supported_features
|
attr[ATTR_ATTRIBUTION] = attribution
|
||||||
|
|
||||||
if (device_class := self.device_class) is not None:
|
if (device_class := self.device_class) is not None:
|
||||||
attr[ATTR_DEVICE_CLASS] = str(device_class)
|
attr[ATTR_DEVICE_CLASS] = str(device_class)
|
||||||
|
|
||||||
if (attribution := self.attribution) is not None:
|
if (entity_picture := self.entity_picture) is not None:
|
||||||
attr[ATTR_ATTRIBUTION] = attribution
|
attr[ATTR_ENTITY_PICTURE] = entity_picture
|
||||||
|
|
||||||
|
if (icon := (entry and entry.icon) or self.icon) is not None:
|
||||||
|
attr[ATTR_ICON] = icon
|
||||||
|
|
||||||
|
if (name := (entry and entry.name) or self.name) is not None:
|
||||||
|
attr[ATTR_FRIENDLY_NAME] = name
|
||||||
|
|
||||||
|
if (supported_features := self.supported_features) is not None:
|
||||||
|
attr[ATTR_SUPPORTED_FEATURES] = supported_features
|
||||||
|
|
||||||
end = timer()
|
end = timer()
|
||||||
|
|
||||||
|
|
|
@ -509,18 +509,18 @@ class EntityPlatform:
|
||||||
self.domain,
|
self.domain,
|
||||||
self.platform_name,
|
self.platform_name,
|
||||||
entity.unique_id,
|
entity.unique_id,
|
||||||
suggested_object_id=suggested_object_id,
|
|
||||||
config_entry=self.config_entry,
|
|
||||||
device_id=device_id,
|
|
||||||
known_object_ids=self.entities.keys(),
|
|
||||||
disabled_by=disabled_by,
|
|
||||||
capabilities=entity.capability_attributes,
|
capabilities=entity.capability_attributes,
|
||||||
supported_features=entity.supported_features,
|
config_entry=self.config_entry,
|
||||||
device_class=entity.device_class,
|
device_class=entity.device_class,
|
||||||
unit_of_measurement=entity.unit_of_measurement,
|
device_id=device_id,
|
||||||
original_name=entity.name,
|
disabled_by=disabled_by,
|
||||||
original_icon=entity.icon,
|
|
||||||
entity_category=entity.entity_category,
|
entity_category=entity.entity_category,
|
||||||
|
known_object_ids=self.entities.keys(),
|
||||||
|
original_icon=entity.icon,
|
||||||
|
original_name=entity.name,
|
||||||
|
suggested_object_id=suggested_object_id,
|
||||||
|
supported_features=entity.supported_features,
|
||||||
|
unit_of_measurement=entity.unit_of_measurement,
|
||||||
)
|
)
|
||||||
|
|
||||||
entity.registry_entry = entry
|
entity.registry_entry = entry
|
||||||
|
|
|
@ -64,12 +64,12 @@ STORAGE_KEY = "core.entity_registry"
|
||||||
# Attributes relevant to describing entity
|
# Attributes relevant to describing entity
|
||||||
# to external services.
|
# to external services.
|
||||||
ENTITY_DESCRIBING_ATTRIBUTES = {
|
ENTITY_DESCRIBING_ATTRIBUTES = {
|
||||||
|
"capabilities",
|
||||||
|
"device_class",
|
||||||
"entity_id",
|
"entity_id",
|
||||||
"name",
|
"name",
|
||||||
"original_name",
|
"original_name",
|
||||||
"capabilities",
|
|
||||||
"supported_features",
|
"supported_features",
|
||||||
"device_class",
|
|
||||||
"unit_of_measurement",
|
"unit_of_measurement",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,11 +81,12 @@ class RegistryEntry:
|
||||||
entity_id: str = attr.ib()
|
entity_id: str = attr.ib()
|
||||||
unique_id: str = attr.ib()
|
unique_id: str = attr.ib()
|
||||||
platform: str = attr.ib()
|
platform: str = attr.ib()
|
||||||
name: str | None = attr.ib(default=None)
|
|
||||||
icon: str | None = attr.ib(default=None)
|
|
||||||
device_id: str | None = attr.ib(default=None)
|
|
||||||
area_id: str | None = attr.ib(default=None)
|
area_id: str | None = attr.ib(default=None)
|
||||||
|
capabilities: Mapping[str, Any] | None = attr.ib(default=None)
|
||||||
config_entry_id: str | None = attr.ib(default=None)
|
config_entry_id: str | None = attr.ib(default=None)
|
||||||
|
device_class: str | None = attr.ib(default=None)
|
||||||
|
device_id: str | None = attr.ib(default=None)
|
||||||
|
domain: str = attr.ib(init=False, repr=False)
|
||||||
disabled_by: str | None = attr.ib(
|
disabled_by: str | None = attr.ib(
|
||||||
default=None,
|
default=None,
|
||||||
validator=attr.validators.in_(
|
validator=attr.validators.in_(
|
||||||
|
@ -99,15 +100,14 @@ class RegistryEntry:
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
capabilities: Mapping[str, Any] | None = attr.ib(default=None)
|
|
||||||
supported_features: int = attr.ib(default=0)
|
|
||||||
device_class: str | None = attr.ib(default=None)
|
|
||||||
unit_of_measurement: str | None = attr.ib(default=None)
|
|
||||||
# As set by integration
|
|
||||||
original_name: str | None = attr.ib(default=None)
|
|
||||||
original_icon: str | None = attr.ib(default=None)
|
|
||||||
entity_category: str | None = attr.ib(default=None)
|
entity_category: str | None = attr.ib(default=None)
|
||||||
domain: str = attr.ib(init=False, repr=False)
|
icon: str | None = attr.ib(default=None)
|
||||||
|
name: str | None = attr.ib(default=None)
|
||||||
|
# As set by integration
|
||||||
|
original_icon: str | None = attr.ib(default=None)
|
||||||
|
original_name: str | None = attr.ib(default=None)
|
||||||
|
supported_features: int = attr.ib(default=0)
|
||||||
|
unit_of_measurement: str | None = attr.ib(default=None)
|
||||||
|
|
||||||
@domain.default
|
@domain.default
|
||||||
def _domain_default(self) -> str:
|
def _domain_default(self) -> str:
|
||||||
|
@ -127,22 +127,22 @@ class RegistryEntry:
|
||||||
if self.capabilities is not None:
|
if self.capabilities is not None:
|
||||||
attrs.update(self.capabilities)
|
attrs.update(self.capabilities)
|
||||||
|
|
||||||
if self.supported_features is not None:
|
|
||||||
attrs[ATTR_SUPPORTED_FEATURES] = self.supported_features
|
|
||||||
|
|
||||||
if self.device_class is not None:
|
if self.device_class is not None:
|
||||||
attrs[ATTR_DEVICE_CLASS] = self.device_class
|
attrs[ATTR_DEVICE_CLASS] = self.device_class
|
||||||
|
|
||||||
if self.unit_of_measurement is not None:
|
icon = self.icon or self.original_icon
|
||||||
attrs[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement
|
if icon is not None:
|
||||||
|
attrs[ATTR_ICON] = icon
|
||||||
|
|
||||||
name = self.name or self.original_name
|
name = self.name or self.original_name
|
||||||
if name is not None:
|
if name is not None:
|
||||||
attrs[ATTR_FRIENDLY_NAME] = name
|
attrs[ATTR_FRIENDLY_NAME] = name
|
||||||
|
|
||||||
icon = self.icon or self.original_icon
|
if self.supported_features is not None:
|
||||||
if icon is not None:
|
attrs[ATTR_SUPPORTED_FEATURES] = self.supported_features
|
||||||
attrs[ATTR_ICON] = icon
|
|
||||||
|
if self.unit_of_measurement is not None:
|
||||||
|
attrs[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement
|
||||||
|
|
||||||
hass.states.async_set(self.entity_id, STATE_UNAVAILABLE, attrs)
|
hass.states.async_set(self.entity_id, STATE_UNAVAILABLE, attrs)
|
||||||
|
|
||||||
|
@ -271,16 +271,16 @@ class EntityRegistry:
|
||||||
if entity_id:
|
if entity_id:
|
||||||
return self._async_update_entity(
|
return self._async_update_entity(
|
||||||
entity_id,
|
entity_id,
|
||||||
config_entry_id=config_entry_id or UNDEFINED,
|
|
||||||
device_id=device_id or UNDEFINED,
|
|
||||||
area_id=area_id or UNDEFINED,
|
area_id=area_id or UNDEFINED,
|
||||||
capabilities=capabilities or UNDEFINED,
|
capabilities=capabilities or UNDEFINED,
|
||||||
supported_features=supported_features or UNDEFINED,
|
config_entry_id=config_entry_id or UNDEFINED,
|
||||||
device_class=device_class or UNDEFINED,
|
device_class=device_class or UNDEFINED,
|
||||||
unit_of_measurement=unit_of_measurement or UNDEFINED,
|
device_id=device_id or UNDEFINED,
|
||||||
original_name=original_name or UNDEFINED,
|
|
||||||
original_icon=original_icon or UNDEFINED,
|
|
||||||
entity_category=entity_category or UNDEFINED,
|
entity_category=entity_category or UNDEFINED,
|
||||||
|
original_icon=original_icon or UNDEFINED,
|
||||||
|
original_name=original_name or UNDEFINED,
|
||||||
|
supported_features=supported_features or UNDEFINED,
|
||||||
|
unit_of_measurement=unit_of_measurement or UNDEFINED,
|
||||||
# When we changed our slugify algorithm, we invalidated some
|
# When we changed our slugify algorithm, we invalidated some
|
||||||
# stored entity IDs with either a __ or ending in _.
|
# stored entity IDs with either a __ or ending in _.
|
||||||
# Fix introduced in 0.86 (Jan 23, 2019). Next line can be
|
# Fix introduced in 0.86 (Jan 23, 2019). Next line can be
|
||||||
|
@ -443,19 +443,19 @@ class EntityRegistry:
|
||||||
old_values = {} # Dict with old key/value pairs
|
old_values = {} # Dict with old key/value pairs
|
||||||
|
|
||||||
for attr_name, value in (
|
for attr_name, value in (
|
||||||
("name", name),
|
|
||||||
("icon", icon),
|
|
||||||
("config_entry_id", config_entry_id),
|
|
||||||
("device_id", device_id),
|
|
||||||
("area_id", area_id),
|
("area_id", area_id),
|
||||||
("disabled_by", disabled_by),
|
|
||||||
("capabilities", capabilities),
|
("capabilities", capabilities),
|
||||||
("supported_features", supported_features),
|
("config_entry_id", config_entry_id),
|
||||||
("device_class", device_class),
|
("device_class", device_class),
|
||||||
("unit_of_measurement", unit_of_measurement),
|
("device_id", device_id),
|
||||||
("original_name", original_name),
|
("disabled_by", disabled_by),
|
||||||
("original_icon", original_icon),
|
|
||||||
("entity_category", entity_category),
|
("entity_category", entity_category),
|
||||||
|
("icon", icon),
|
||||||
|
("name", name),
|
||||||
|
("original_icon", original_icon),
|
||||||
|
("original_name", original_name),
|
||||||
|
("supported_features", supported_features),
|
||||||
|
("unit_of_measurement", unit_of_measurement),
|
||||||
):
|
):
|
||||||
if value is not UNDEFINED and value != getattr(old, attr_name):
|
if value is not UNDEFINED and value != getattr(old, attr_name):
|
||||||
new_values[attr_name] = value
|
new_values[attr_name] = value
|
||||||
|
@ -526,22 +526,22 @@ class EntityRegistry:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
entities[entity["entity_id"]] = RegistryEntry(
|
entities[entity["entity_id"]] = RegistryEntry(
|
||||||
entity_id=entity["entity_id"],
|
|
||||||
config_entry_id=entity.get("config_entry_id"),
|
|
||||||
device_id=entity.get("device_id"),
|
|
||||||
area_id=entity.get("area_id"),
|
area_id=entity.get("area_id"),
|
||||||
unique_id=entity["unique_id"],
|
|
||||||
platform=entity["platform"],
|
|
||||||
name=entity.get("name"),
|
|
||||||
icon=entity.get("icon"),
|
|
||||||
disabled_by=entity.get("disabled_by"),
|
|
||||||
capabilities=entity.get("capabilities") or {},
|
capabilities=entity.get("capabilities") or {},
|
||||||
supported_features=entity.get("supported_features", 0),
|
config_entry_id=entity.get("config_entry_id"),
|
||||||
device_class=entity.get("device_class"),
|
device_class=entity.get("device_class"),
|
||||||
unit_of_measurement=entity.get("unit_of_measurement"),
|
device_id=entity.get("device_id"),
|
||||||
original_name=entity.get("original_name"),
|
disabled_by=entity.get("disabled_by"),
|
||||||
original_icon=entity.get("original_icon"),
|
|
||||||
entity_category=entity.get("entity_category"),
|
entity_category=entity.get("entity_category"),
|
||||||
|
entity_id=entity["entity_id"],
|
||||||
|
icon=entity.get("icon"),
|
||||||
|
name=entity.get("name"),
|
||||||
|
original_icon=entity.get("original_icon"),
|
||||||
|
original_name=entity.get("original_name"),
|
||||||
|
platform=entity["platform"],
|
||||||
|
supported_features=entity.get("supported_features", 0),
|
||||||
|
unique_id=entity["unique_id"],
|
||||||
|
unit_of_measurement=entity.get("unit_of_measurement"),
|
||||||
)
|
)
|
||||||
|
|
||||||
self.entities = entities
|
self.entities = entities
|
||||||
|
@ -559,22 +559,22 @@ class EntityRegistry:
|
||||||
|
|
||||||
data["entities"] = [
|
data["entities"] = [
|
||||||
{
|
{
|
||||||
"entity_id": entry.entity_id,
|
|
||||||
"config_entry_id": entry.config_entry_id,
|
|
||||||
"device_id": entry.device_id,
|
|
||||||
"area_id": entry.area_id,
|
"area_id": entry.area_id,
|
||||||
"unique_id": entry.unique_id,
|
|
||||||
"platform": entry.platform,
|
|
||||||
"name": entry.name,
|
|
||||||
"icon": entry.icon,
|
|
||||||
"disabled_by": entry.disabled_by,
|
|
||||||
"capabilities": entry.capabilities,
|
"capabilities": entry.capabilities,
|
||||||
"supported_features": entry.supported_features,
|
"config_entry_id": entry.config_entry_id,
|
||||||
"device_class": entry.device_class,
|
"device_class": entry.device_class,
|
||||||
"unit_of_measurement": entry.unit_of_measurement,
|
"device_id": entry.device_id,
|
||||||
"original_name": entry.original_name,
|
"disabled_by": entry.disabled_by,
|
||||||
"original_icon": entry.original_icon,
|
|
||||||
"entity_category": entry.entity_category,
|
"entity_category": entry.entity_category,
|
||||||
|
"entity_id": entry.entity_id,
|
||||||
|
"icon": entry.icon,
|
||||||
|
"name": entry.name,
|
||||||
|
"original_icon": entry.original_icon,
|
||||||
|
"original_name": entry.original_name,
|
||||||
|
"platform": entry.platform,
|
||||||
|
"supported_features": entry.supported_features,
|
||||||
|
"unique_id": entry.unique_id,
|
||||||
|
"unit_of_measurement": entry.unit_of_measurement,
|
||||||
}
|
}
|
||||||
for entry in self.entities.values()
|
for entry in self.entities.values()
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Reference in a new issue