mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
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
This commit is contained in:
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]:
|
def _get_sensor_states(hass: HomeAssistant) -> list[State]:
|
||||||
"""Get the current state of all sensors for which to compile statistics."""
|
"""Get the current state of all sensors for which to compile statistics."""
|
||||||
all_sensors = hass.states.all(DOMAIN)
|
|
||||||
instance = get_instance(hass)
|
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 [
|
return [
|
||||||
state
|
state
|
||||||
for state in all_sensors
|
for state in hass.states.all(DOMAIN)
|
||||||
if instance.entity_filter(state.entity_id)
|
if (state_class := state.attributes.get(ATTR_STATE_CLASS))
|
||||||
and try_parse_enum(SensorStateClass, 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…
x
Reference in New Issue
Block a user