Speed up selecting which statistics to compile (#87938)

This commit is contained in:
J. Nick Koston 2023-02-12 11:15:27 -06:00 committed by GitHub
parent 10b7c273b1
commit b054296c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -75,17 +75,13 @@ 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) all_sensors = hass.states.all(DOMAIN)
statistics_sensors = []
instance = get_instance(hass) instance = get_instance(hass)
return [
for state in all_sensors: state
if not instance.entity_filter(state.entity_id): for state in all_sensors
continue if instance.entity_filter(state.entity_id)
if not try_parse_enum(SensorStateClass, state.attributes.get(ATTR_STATE_CLASS)): and try_parse_enum(SensorStateClass, state.attributes.get(ATTR_STATE_CLASS))
continue ]
statistics_sensors.append(state)
return statistics_sensors
def _time_weighted_average( def _time_weighted_average(
@ -347,11 +343,10 @@ def reset_detected(
def _wanted_statistics(sensor_states: list[State]) -> dict[str, set[str]]: def _wanted_statistics(sensor_states: list[State]) -> dict[str, set[str]]:
"""Prepare a dict with wanted statistics for entities.""" """Prepare a dict with wanted statistics for entities."""
wanted_statistics = {} return {
for state in sensor_states: state.entity_id: DEFAULT_STATISTICS[state.attributes[ATTR_STATE_CLASS]]
state_class = state.attributes[ATTR_STATE_CLASS] for state in sensor_states
wanted_statistics[state.entity_id] = DEFAULT_STATISTICS[state_class] }
return wanted_statistics
def _last_reset_as_utc_isoformat(last_reset_s: Any, entity_id: str) -> str | None: def _last_reset_as_utc_isoformat(last_reset_s: Any, entity_id: str) -> str | None: