diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index fece82b4ff6..294c5217623 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -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 [