diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index d08a20636ab..1aba934aba4 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -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) ]