mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Small cleanups to writing entity state (#89890)
* Small cleanups to writing entity state * reduce one prop access * small cleanups * small cleanups * name conflict
This commit is contained in:
parent
8ecd73cac7
commit
30e7ab247d
@ -579,6 +579,25 @@ class Entity(ABC):
|
||||
return f"{state:.{FLOAT_PRECISION}}"
|
||||
return str(state)
|
||||
|
||||
def _friendly_name_internal(self) -> str | None:
|
||||
"""Return the friendly name.
|
||||
|
||||
If has_entity_name is False, this returns self.name
|
||||
If has_entity_name is True, this returns device.name + self.name
|
||||
"""
|
||||
if not self.has_entity_name or not self.registry_entry:
|
||||
return self.name
|
||||
|
||||
device_registry = dr.async_get(self.hass)
|
||||
if not (device_id := self.registry_entry.device_id) or not (
|
||||
device_entry := device_registry.async_get(device_id)
|
||||
):
|
||||
return self.name
|
||||
|
||||
if not (name := self.name):
|
||||
return device_entry.name_by_user or device_entry.name
|
||||
return f"{device_entry.name_by_user or device_entry.name} {name}"
|
||||
|
||||
@callback
|
||||
def _async_write_ha_state(self) -> None:
|
||||
"""Write the state to the state machine."""
|
||||
@ -586,7 +605,11 @@ class Entity(ABC):
|
||||
# Polling returned after the entity has already been removed
|
||||
return
|
||||
|
||||
if self.registry_entry and self.registry_entry.disabled_by:
|
||||
hass = self.hass
|
||||
entity_id = self.entity_id
|
||||
entry = self.registry_entry
|
||||
|
||||
if entry and entry.disabled_by:
|
||||
if not self._disabled_reported:
|
||||
self._disabled_reported = True
|
||||
assert self.platform is not None
|
||||
@ -595,7 +618,7 @@ class Entity(ABC):
|
||||
"Entity %s is incorrectly being triggered for updates while it"
|
||||
" is disabled. This is a bug in the %s integration"
|
||||
),
|
||||
self.entity_id,
|
||||
entity_id,
|
||||
self.platform.platform_name,
|
||||
)
|
||||
return
|
||||
@ -614,8 +637,6 @@ class Entity(ABC):
|
||||
if (unit_of_measurement := self.unit_of_measurement) is not None:
|
||||
attr[ATTR_UNIT_OF_MEASUREMENT] = unit_of_measurement
|
||||
|
||||
entry = self.registry_entry
|
||||
|
||||
if assumed_state := self.assumed_state:
|
||||
attr[ATTR_ASSUMED_STATE] = assumed_state
|
||||
|
||||
@ -633,26 +654,9 @@ class Entity(ABC):
|
||||
if (icon := (entry and entry.icon) or self.icon) is not None:
|
||||
attr[ATTR_ICON] = icon
|
||||
|
||||
def friendly_name() -> str | None:
|
||||
"""Return the friendly name.
|
||||
|
||||
If has_entity_name is False, this returns self.name
|
||||
If has_entity_name is True, this returns device.name + self.name
|
||||
"""
|
||||
if not self.has_entity_name or not self.registry_entry:
|
||||
return self.name
|
||||
|
||||
device_registry = dr.async_get(self.hass)
|
||||
if not (device_id := self.registry_entry.device_id) or not (
|
||||
device_entry := device_registry.async_get(device_id)
|
||||
):
|
||||
return self.name
|
||||
|
||||
if not self.name:
|
||||
return device_entry.name_by_user or device_entry.name
|
||||
return f"{device_entry.name_by_user or device_entry.name} {self.name}"
|
||||
|
||||
if (name := (entry and entry.name) or friendly_name()) is not None:
|
||||
if (
|
||||
name := (entry and entry.name) or self._friendly_name_internal()
|
||||
) is not None:
|
||||
attr[ATTR_FRIENDLY_NAME] = name
|
||||
|
||||
if (supported_features := self.supported_features) is not None:
|
||||
@ -665,15 +669,15 @@ class Entity(ABC):
|
||||
report_issue = self._suggest_report_issue()
|
||||
_LOGGER.warning(
|
||||
"Updating state for %s (%s) took %.3f seconds. Please %s",
|
||||
self.entity_id,
|
||||
entity_id,
|
||||
type(self),
|
||||
end - start,
|
||||
report_issue,
|
||||
)
|
||||
|
||||
# Overwrite properties that have been set in the config file.
|
||||
if DATA_CUSTOMIZE in self.hass.data:
|
||||
attr.update(self.hass.data[DATA_CUSTOMIZE].get(self.entity_id))
|
||||
if customize := hass.data.get(DATA_CUSTOMIZE):
|
||||
attr.update(customize.get(entity_id))
|
||||
|
||||
if (
|
||||
self._context_set is not None
|
||||
@ -682,9 +686,7 @@ class Entity(ABC):
|
||||
self._context = None
|
||||
self._context_set = None
|
||||
|
||||
self.hass.states.async_set(
|
||||
self.entity_id, state, attr, self.force_update, self._context
|
||||
)
|
||||
hass.states.async_set(entity_id, state, attr, self.force_update, self._context)
|
||||
|
||||
def schedule_update_ha_state(self, force_refresh: bool = False) -> None:
|
||||
"""Schedule an update ha state change task.
|
||||
|
Loading…
x
Reference in New Issue
Block a user