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