Refactor sensor recorder _get_sensor_states to check for state class first (#107046)

The state class check is cheap and the entity filter check is much
more expensive, so do the state class check first
pull/105955/head
J. Nick Koston 2024-01-07 17:36:03 -10:00 committed by GitHub
parent 0b9992260a
commit 50edc334de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 4 deletions

View File

@ -68,13 +68,19 @@ LINK_DEV_STATISTICS = "https://my.home-assistant.io/redirect/developer_statistic
def _get_sensor_states(hass: HomeAssistant) -> list[State]:
"""Get the current state of all sensors for which to compile statistics."""
all_sensors = hass.states.all(DOMAIN)
instance = get_instance(hass)
# We check for state class first before calling the filter
# function as the filter function is much more expensive
# than checking the state class
return [
state
for state in all_sensors
if instance.entity_filter(state.entity_id)
and try_parse_enum(SensorStateClass, state.attributes.get(ATTR_STATE_CLASS))
for state in hass.states.all(DOMAIN)
if (state_class := state.attributes.get(ATTR_STATE_CLASS))
and (
type(state_class) is SensorStateClass
or try_parse_enum(SensorStateClass, state_class)
)
and instance.entity_filter(state.entity_id)
]