Tweak list_statistic_ids (#55845)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
pull/56731/head
Erik Montnemery 2021-09-28 09:05:26 +02:00 committed by GitHub
parent c48527858d
commit 552485bb05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 12 deletions

View File

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

View File

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