mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Replace statistics _get_unit_class function with a dict lookup (#124452)
This commit is contained in:
parent
61ac4c7af7
commit
7579ebc02a
@ -148,6 +148,12 @@ STATISTIC_UNIT_TO_UNIT_CONVERTER: dict[str | None, type[BaseUnitConverter]] = {
|
|||||||
**{unit: VolumeFlowRateConverter for unit in VolumeFlowRateConverter.VALID_UNITS},
|
**{unit: VolumeFlowRateConverter for unit in VolumeFlowRateConverter.VALID_UNITS},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
UNIT_CLASSES = {
|
||||||
|
unit: converter.UNIT_CLASS
|
||||||
|
for unit, converter in STATISTIC_UNIT_TO_UNIT_CONVERTER.items()
|
||||||
|
}
|
||||||
|
|
||||||
DATA_SHORT_TERM_STATISTICS_RUN_CACHE = "recorder_short_term_statistics_run_cache"
|
DATA_SHORT_TERM_STATISTICS_RUN_CACHE = "recorder_short_term_statistics_run_cache"
|
||||||
|
|
||||||
|
|
||||||
@ -211,13 +217,6 @@ class StatisticsRow(BaseStatisticsRow, total=False):
|
|||||||
change: float | None
|
change: float | None
|
||||||
|
|
||||||
|
|
||||||
def _get_unit_class(unit: str | None) -> str | None:
|
|
||||||
"""Get corresponding unit class from from the statistics unit."""
|
|
||||||
if converter := STATISTIC_UNIT_TO_UNIT_CONVERTER.get(unit):
|
|
||||||
return converter.UNIT_CLASS
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def get_display_unit(
|
def get_display_unit(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
statistic_id: str,
|
statistic_id: str,
|
||||||
@ -807,7 +806,7 @@ def _statistic_by_id_from_metadata(
|
|||||||
"has_sum": meta["has_sum"],
|
"has_sum": meta["has_sum"],
|
||||||
"name": meta["name"],
|
"name": meta["name"],
|
||||||
"source": meta["source"],
|
"source": meta["source"],
|
||||||
"unit_class": _get_unit_class(meta["unit_of_measurement"]),
|
"unit_class": UNIT_CLASSES.get(meta["unit_of_measurement"]),
|
||||||
"unit_of_measurement": meta["unit_of_measurement"],
|
"unit_of_measurement": meta["unit_of_measurement"],
|
||||||
}
|
}
|
||||||
for _, meta in metadata.values()
|
for _, meta in metadata.values()
|
||||||
@ -881,7 +880,7 @@ def list_statistic_ids(
|
|||||||
"has_sum": meta["has_sum"],
|
"has_sum": meta["has_sum"],
|
||||||
"name": meta["name"],
|
"name": meta["name"],
|
||||||
"source": meta["source"],
|
"source": meta["source"],
|
||||||
"unit_class": _get_unit_class(meta["unit_of_measurement"]),
|
"unit_class": UNIT_CLASSES.get(meta["unit_of_measurement"]),
|
||||||
"unit_of_measurement": meta["unit_of_measurement"],
|
"unit_of_measurement": meta["unit_of_measurement"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user