mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Tweak list_statistic_ids (#55845)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
c48527858d
commit
552485bb05
@ -517,7 +517,8 @@ def list_statistic_ids(
|
|||||||
unit = _configured_unit(unit, units)
|
unit = _configured_unit(unit, units)
|
||||||
platform_statistic_ids[statistic_id] = unit
|
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 a map of statistic_id to unit_of_measurement
|
||||||
return [
|
return [
|
||||||
|
@ -125,7 +125,7 @@ WARN_UNSUPPORTED_UNIT = "sensor_warn_unsupported_unit"
|
|||||||
WARN_UNSTABLE_UNIT = "sensor_warn_unstable_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."""
|
"""Get (entity_id, state_class, device_class) of all sensors for which to compile statistics."""
|
||||||
all_sensors = hass.states.all(DOMAIN)
|
all_sensors = hass.states.all(DOMAIN)
|
||||||
entity_ids = []
|
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:
|
if (state_class := state.attributes.get(ATTR_STATE_CLASS)) not in STATE_CLASSES:
|
||||||
continue
|
continue
|
||||||
device_class = state.attributes.get(ATTR_DEVICE_CLASS)
|
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
|
return entity_ids
|
||||||
|
|
||||||
@ -298,11 +299,11 @@ def reset_detected(
|
|||||||
|
|
||||||
|
|
||||||
def _wanted_statistics(
|
def _wanted_statistics(
|
||||||
entities: list[tuple[str, str, str | None]]
|
entities: list[tuple[str, str, str | None, str | None]]
|
||||||
) -> dict[str, set[str]]:
|
) -> dict[str, set[str]]:
|
||||||
"""Prepare a dict with wanted statistics for entities."""
|
"""Prepare a dict with wanted statistics for entities."""
|
||||||
wanted_statistics = {}
|
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]:
|
if device_class in DEVICE_CLASS_STATISTICS[state_class]:
|
||||||
wanted_statistics[entity_id] = DEVICE_CLASS_STATISTICS[state_class][
|
wanted_statistics[entity_id] = DEVICE_CLASS_STATISTICS[state_class][
|
||||||
device_class
|
device_class
|
||||||
@ -367,6 +368,7 @@ def compile_statistics( # noqa: C901
|
|||||||
entity_id,
|
entity_id,
|
||||||
state_class,
|
state_class,
|
||||||
device_class,
|
device_class,
|
||||||
|
_,
|
||||||
) in entities:
|
) in entities:
|
||||||
if entity_id not in history_list:
|
if entity_id not in history_list:
|
||||||
continue
|
continue
|
||||||
@ -530,7 +532,7 @@ def list_statistic_ids(hass: HomeAssistant, statistic_type: str | None = None) -
|
|||||||
|
|
||||||
statistic_ids = {}
|
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]:
|
if device_class in DEVICE_CLASS_STATISTICS[state_class]:
|
||||||
provided_statistics = DEVICE_CLASS_STATISTICS[state_class][device_class]
|
provided_statistics = DEVICE_CLASS_STATISTICS[state_class][device_class]
|
||||||
else:
|
else:
|
||||||
@ -549,12 +551,6 @@ def list_statistic_ids(hass: HomeAssistant, statistic_type: str | None = None) -
|
|||||||
):
|
):
|
||||||
continue
|
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:
|
if device_class not in UNIT_CONVERSIONS:
|
||||||
statistic_ids[entity_id] = native_unit
|
statistic_ids[entity_id] = native_unit
|
||||||
continue
|
continue
|
||||||
@ -580,6 +576,7 @@ def validate_statistics(
|
|||||||
entity_id,
|
entity_id,
|
||||||
_state_class,
|
_state_class,
|
||||||
device_class,
|
device_class,
|
||||||
|
_unit,
|
||||||
) in entities:
|
) in entities:
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert state is not None
|
assert state is not None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user