Reduce ref counting in _async_write_ha_state (#143634)

* Reduce ref counting in _async_write_ha_state

It no longer makes sense to keep a temp reference
to entity_id and hass since the function was
refactored and there are very few accesses now.

* one more place we can reduce ref counts
This commit is contained in:
J. Nick Koston 2025-04-25 06:25:16 -10:00 committed by GitHub
parent ea90df434b
commit d61e39743b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1124,9 +1124,6 @@ class Entity(
# Polling returned after the entity has already been removed
return
hass = self.hass
entity_id = self.entity_id
if (entry := self.registry_entry) and entry.disabled_by:
if not self._disabled_reported:
self._disabled_reported = True
@ -1135,7 +1132,7 @@ class Entity(
"Entity %s is incorrectly being triggered for updates while it"
" is disabled. This is a bug in the %s integration"
),
entity_id,
self.entity_id,
self.platform.platform_name,
)
return
@ -1177,7 +1174,7 @@ class Entity(
"Entity %s (%s) is updating its capabilities too often,"
" please %s"
),
entity_id,
self.entity_id,
type(self),
report_issue,
)
@ -1194,7 +1191,7 @@ class Entity(
report_issue = self._suggest_report_issue()
_LOGGER.warning(
"Updating state for %s (%s) took %.3f seconds. Please %s",
entity_id,
self.entity_id,
type(self),
time_now - state_calculate_start,
report_issue,
@ -1205,12 +1202,12 @@ class Entity(
# set and since try is near zero cost
# on py3.11+ its faster to assume it is
# set and catch the exception if it is not.
customize = hass.data[DATA_CUSTOMIZE]
custom = self.hass.data[DATA_CUSTOMIZE].get(self.entity_id)
except KeyError:
pass
else:
# Overwrite properties that have been set in the config file.
if custom := customize.get(entity_id):
if custom:
attr |= custom
if (
@ -1224,15 +1221,15 @@ class Entity(
_LOGGER.error(
"State %s for %s is longer than %s, falling back to %s",
state,
entity_id,
self.entity_id,
MAX_LENGTH_STATE_STATE,
STATE_UNKNOWN,
)
state = STATE_UNKNOWN
# Intentionally called with positional args for performance reasons
hass.states.async_set_internal(
entity_id,
self.hass.states.async_set_internal(
self.entity_id,
state,
attr,
self.force_update,