From 242aae514ec29469befd6ba9e22ef03881bc821f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 25 Aug 2024 06:32:32 -1000 Subject: [PATCH] Small cleanups to list_statistic_ids (#124451) --- homeassistant/components/sensor/recorder.py | 25 ++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index c02c3ce7b7a..be4dce28546 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -640,32 +640,31 @@ def list_statistic_ids( result: dict[str, StatisticMetaData] = {} for state in entities: - state_class = state.attributes[ATTR_STATE_CLASS] - state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + entity_id = state.entity_id + if statistic_ids is not None and entity_id not in statistic_ids: + continue + attributes = state.attributes + state_class = attributes[ATTR_STATE_CLASS] provided_statistics = DEFAULT_STATISTICS[state_class] if statistic_type is not None and statistic_type not in provided_statistics: continue - if statistic_ids is not None and state.entity_id not in statistic_ids: - continue - if ( - "sum" in provided_statistics - and ATTR_LAST_RESET not in state.attributes - and state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT + (has_sum := "sum" in provided_statistics) + and ATTR_LAST_RESET not in attributes + and state_class == SensorStateClass.MEASUREMENT ): continue - result[state.entity_id] = { + result[entity_id] = { "has_mean": "mean" in provided_statistics, - "has_sum": "sum" in provided_statistics, + "has_sum": has_sum, "name": None, "source": RECORDER_DOMAIN, - "statistic_id": state.entity_id, - "unit_of_measurement": state_unit, + "statistic_id": entity_id, + "unit_of_measurement": attributes.get(ATTR_UNIT_OF_MEASUREMENT), } - continue return result