From 552485bb05a4ca599fe2bfd25806fdb60436454e Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 28 Sep 2021 09:05:26 +0200 Subject: [PATCH] Tweak list_statistic_ids (#55845) Co-authored-by: Paulus Schoutsen --- .../components/recorder/statistics.py | 3 ++- homeassistant/components/sensor/recorder.py | 19 ++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index 2b4775e2412..845e13d40b3 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -517,7 +517,8 @@ def list_statistic_ids( unit = _configured_unit(unit, units) platform_statistic_ids[statistic_id] = unit - statistic_ids = {**statistic_ids, **platform_statistic_ids} + for key, value in platform_statistic_ids.items(): + statistic_ids.setdefault(key, value) # Return a map of statistic_id to unit_of_measurement return [ diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 69db5be912f..806981d51eb 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -125,7 +125,7 @@ WARN_UNSUPPORTED_UNIT = "sensor_warn_unsupported_unit" WARN_UNSTABLE_UNIT = "sensor_warn_unstable_unit" -def _get_entities(hass: HomeAssistant) -> list[tuple[str, str, str | None]]: +def _get_entities(hass: HomeAssistant) -> list[tuple[str, str, str | None, str | None]]: """Get (entity_id, state_class, device_class) of all sensors for which to compile statistics.""" all_sensors = hass.states.all(DOMAIN) entity_ids = [] @@ -134,7 +134,8 @@ def _get_entities(hass: HomeAssistant) -> list[tuple[str, str, str | None]]: if (state_class := state.attributes.get(ATTR_STATE_CLASS)) not in STATE_CLASSES: continue device_class = state.attributes.get(ATTR_DEVICE_CLASS) - entity_ids.append((state.entity_id, state_class, device_class)) + unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + entity_ids.append((state.entity_id, state_class, device_class, unit)) return entity_ids @@ -298,11 +299,11 @@ def reset_detected( def _wanted_statistics( - entities: list[tuple[str, str, str | None]] + entities: list[tuple[str, str, str | None, str | None]] ) -> dict[str, set[str]]: """Prepare a dict with wanted statistics for entities.""" wanted_statistics = {} - for entity_id, state_class, device_class in entities: + for entity_id, state_class, device_class, _ in entities: if device_class in DEVICE_CLASS_STATISTICS[state_class]: wanted_statistics[entity_id] = DEVICE_CLASS_STATISTICS[state_class][ device_class @@ -367,6 +368,7 @@ def compile_statistics( # noqa: C901 entity_id, state_class, device_class, + _, ) in entities: if entity_id not in history_list: continue @@ -530,7 +532,7 @@ def list_statistic_ids(hass: HomeAssistant, statistic_type: str | None = None) - statistic_ids = {} - for entity_id, state_class, device_class in entities: + for entity_id, state_class, device_class, native_unit in entities: if device_class in DEVICE_CLASS_STATISTICS[state_class]: provided_statistics = DEVICE_CLASS_STATISTICS[state_class][device_class] else: @@ -549,12 +551,6 @@ def list_statistic_ids(hass: HomeAssistant, statistic_type: str | None = None) - ): continue - metadata = statistics.get_metadata(hass, entity_id) - if metadata: - native_unit: str | None = metadata["unit_of_measurement"] - else: - native_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - if device_class not in UNIT_CONVERSIONS: statistic_ids[entity_id] = native_unit continue @@ -580,6 +576,7 @@ def validate_statistics( entity_id, _state_class, device_class, + _unit, ) in entities: state = hass.states.get(entity_id) assert state is not None