Small cleanups to list_statistic_ids (#124451)

This commit is contained in:
J. Nick Koston 2024-08-25 06:32:32 -10:00 committed by GitHub
parent d94b1e6e8a
commit 242aae514e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -640,32 +640,31 @@ def list_statistic_ids(
result: dict[str, StatisticMetaData] = {} result: dict[str, StatisticMetaData] = {}
for state in entities: for state in entities:
state_class = state.attributes[ATTR_STATE_CLASS] entity_id = state.entity_id
state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) 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] provided_statistics = DEFAULT_STATISTICS[state_class]
if statistic_type is not None and statistic_type not in provided_statistics: if statistic_type is not None and statistic_type not in provided_statistics:
continue continue
if statistic_ids is not None and state.entity_id not in statistic_ids:
continue
if ( if (
"sum" in provided_statistics (has_sum := "sum" in provided_statistics)
and ATTR_LAST_RESET not in state.attributes and ATTR_LAST_RESET not in attributes
and state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT and state_class == SensorStateClass.MEASUREMENT
): ):
continue continue
result[state.entity_id] = { result[entity_id] = {
"has_mean": "mean" in provided_statistics, "has_mean": "mean" in provided_statistics,
"has_sum": "sum" in provided_statistics, "has_sum": has_sum,
"name": None, "name": None,
"source": RECORDER_DOMAIN, "source": RECORDER_DOMAIN,
"statistic_id": state.entity_id, "statistic_id": entity_id,
"unit_of_measurement": state_unit, "unit_of_measurement": attributes.get(ATTR_UNIT_OF_MEASUREMENT),
} }
continue
return result return result