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 firstpull/105955/head
parent
0b9992260a
commit
50edc334de
|
@ -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)
|
||||
]
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue