Skip to content

Commit

Permalink
Replace statistics _get_unit_class function with a dict lookup (#124452)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Aug 23, 2024
1 parent 61ac4c7 commit 7579ebc
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions homeassistant/components/recorder/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
**{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"


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"],
}

Expand Down

0 comments on commit 7579ebc

Please sign in to comment.