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,6 +1010,10 @@ def list_statistic_ids(
for _, meta in metadata.values()
}
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"):
@ -1019,6 +1024,7 @@ def list_statistic_ids(
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"],