Avoid asking recorder platforms for list_statistic_ids when already complete (#88495)

* Avoid asking recorder platforms for list_statistic_ids when already complete

If we already had all the data needed for list_statistic_ids, we would
still query recorder platforms and throw away the results

* Update homeassistant/components/recorder/statistics.py
This commit is contained in:
J. Nick Koston 2023-02-20 11:39:42 -06:00 committed by GitHub
parent 5bf3a0b7af
commit 9c82cfecca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -987,6 +987,7 @@ def list_statistic_ids(
period.
"""
result = {}
statistic_ids_set = set(statistic_ids) if statistic_ids else None
# Query the database
with session_scope(hass=hass) as session:
@ -1009,26 +1010,31 @@ def list_statistic_ids(
for _, meta in metadata.values()
}
# Query all integrations with a registered recorder platform
for platform in hass.data[DOMAIN].recorder_platforms.values():
if not hasattr(platform, "list_statistic_ids"):
continue
platform_statistic_ids = platform.list_statistic_ids(
hass, statistic_ids=statistic_ids, statistic_type=statistic_type
)
for key, meta in platform_statistic_ids.items():
if key in result:
if not statistic_ids_set or statistic_ids_set.difference(result):
# If we want all statistic_ids, or some are missing, we need to query
# the integrations for the missing ones.
#
# Query all integrations with a registered recorder platform
for platform in hass.data[DOMAIN].recorder_platforms.values():
if not hasattr(platform, "list_statistic_ids"):
continue
result[key] = {
"display_unit_of_measurement": meta["unit_of_measurement"],
"has_mean": meta["has_mean"],
"has_sum": meta["has_sum"],
"name": meta["name"],
"source": meta["source"],
"unit_class": _get_unit_class(meta["unit_of_measurement"]),
"unit_of_measurement": meta["unit_of_measurement"],
}
platform_statistic_ids = platform.list_statistic_ids(
hass, statistic_ids=statistic_ids, statistic_type=statistic_type
)
for key, meta in platform_statistic_ids.items():
if key in result:
# The database has a higher priority than the integration
continue
result[key] = {
"display_unit_of_measurement": meta["unit_of_measurement"],
"has_mean": meta["has_mean"],
"has_sum": meta["has_sum"],
"name": meta["name"],
"source": meta["source"],
"unit_class": _get_unit_class(meta["unit_of_measurement"]),
"unit_of_measurement": meta["unit_of_measurement"],
}
# Return a list of statistic_id + metadata
return [