mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Replace linear search in unit_system with dict lookup (#125485)
This commit is contained in:
parent
ab29718a45
commit
0a11acf7ae
@ -58,23 +58,21 @@ WIND_SPEED_UNITS = SpeedConverter.VALID_UNITS
|
||||
|
||||
TEMPERATURE_UNITS: set[str] = {UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS}
|
||||
|
||||
_VALID_BY_TYPE: dict[str, set[str] | set[str | None]] = {
|
||||
LENGTH: LENGTH_UNITS,
|
||||
ACCUMULATED_PRECIPITATION: LENGTH_UNITS,
|
||||
WIND_SPEED: WIND_SPEED_UNITS,
|
||||
TEMPERATURE: TEMPERATURE_UNITS,
|
||||
MASS: MASS_UNITS,
|
||||
VOLUME: VOLUME_UNITS,
|
||||
PRESSURE: PRESSURE_UNITS,
|
||||
}
|
||||
|
||||
|
||||
def _is_valid_unit(unit: str, unit_type: str) -> bool:
|
||||
"""Check if the unit is valid for it's type."""
|
||||
if unit_type == LENGTH:
|
||||
return unit in LENGTH_UNITS
|
||||
if unit_type == ACCUMULATED_PRECIPITATION:
|
||||
return unit in LENGTH_UNITS
|
||||
if unit_type == WIND_SPEED:
|
||||
return unit in WIND_SPEED_UNITS
|
||||
if unit_type == TEMPERATURE:
|
||||
return unit in TEMPERATURE_UNITS
|
||||
if unit_type == MASS:
|
||||
return unit in MASS_UNITS
|
||||
if unit_type == VOLUME:
|
||||
return unit in VOLUME_UNITS
|
||||
if unit_type == PRESSURE:
|
||||
return unit in PRESSURE_UNITS
|
||||
if units := _VALID_BY_TYPE.get(unit_type):
|
||||
return unit in units
|
||||
return False
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user