Small cleanups to list_statistic_ids (#124451)

pull/124582/head
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
1 changed files with 12 additions and 13 deletions

View File

@ -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