Replace statistics _get_unit_class function with a dict lookup (#124452)

This commit is contained in:
J. Nick Koston 2024-08-22 21:14:31 -05:00 committed by GitHub
parent 61ac4c7af7
commit 7579ebc02a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -148,6 +148,12 @@ STATISTIC_UNIT_TO_UNIT_CONVERTER: dict[str | None, type[BaseUnitConverter]] = {
**{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"
@ -211,13 +217,6 @@ class StatisticsRow(BaseStatisticsRow, total=False):
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(
hass: HomeAssistant,
statistic_id: str,
@ -807,7 +806,7 @@ def _statistic_by_id_from_metadata(
"has_sum": meta["has_sum"],
"name": meta["name"],
"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"],
}
for _, meta in metadata.values()
@ -881,7 +880,7 @@ def list_statistic_ids(
"has_sum": meta["has_sum"],
"name": meta["name"],
"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"],
}