mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 18:28:14 +02:00
Add new unit types for better type checking (#53124)
* Add new unit types * Update helper functions * Update components * Update lcn climate
This commit is contained in:
@ -24,6 +24,13 @@ from homeassistant.const import (
|
||||
VOLUME,
|
||||
VOLUME_GALLONS,
|
||||
VOLUME_LITERS,
|
||||
UnitLengthT,
|
||||
UnitMassT,
|
||||
UnitPressureT,
|
||||
UnitT,
|
||||
UnitTemperatureT,
|
||||
UnitTypeT,
|
||||
UnitVolumeT,
|
||||
)
|
||||
from homeassistant.util import (
|
||||
distance as distance_util,
|
||||
@ -36,17 +43,23 @@ from homeassistant.util import (
|
||||
|
||||
LENGTH_UNITS = distance_util.VALID_UNITS
|
||||
|
||||
MASS_UNITS = [MASS_POUNDS, MASS_OUNCES, MASS_KILOGRAMS, MASS_GRAMS]
|
||||
MASS_UNITS: tuple[UnitMassT, ...] = (
|
||||
MASS_POUNDS,
|
||||
MASS_OUNCES,
|
||||
MASS_KILOGRAMS,
|
||||
MASS_GRAMS,
|
||||
)
|
||||
|
||||
PRESSURE_UNITS = pressure_util.VALID_UNITS
|
||||
|
||||
VOLUME_UNITS = volume_util.VALID_UNITS
|
||||
|
||||
TEMPERATURE_UNITS = [TEMP_FAHRENHEIT, TEMP_CELSIUS]
|
||||
TEMPERATURE_UNITS: tuple[UnitTemperatureT, ...] = (TEMP_FAHRENHEIT, TEMP_CELSIUS)
|
||||
|
||||
|
||||
def is_valid_unit(unit: str, unit_type: str) -> bool:
|
||||
def is_valid_unit(unit: UnitT, unit_type: UnitTypeT) -> bool:
|
||||
"""Check if the unit is valid for it's type."""
|
||||
units: tuple[UnitT, ...]
|
||||
if unit_type == LENGTH:
|
||||
units = LENGTH_UNITS
|
||||
elif unit_type == TEMPERATURE:
|
||||
@ -69,11 +82,11 @@ class UnitSystem:
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
temperature: str,
|
||||
length: str,
|
||||
volume: str,
|
||||
mass: str,
|
||||
pressure: str,
|
||||
temperature: UnitTemperatureT,
|
||||
length: UnitLengthT,
|
||||
volume: UnitVolumeT,
|
||||
mass: UnitMassT,
|
||||
pressure: UnitPressureT,
|
||||
) -> None:
|
||||
"""Initialize the unit system object."""
|
||||
errors: str = ", ".join(
|
||||
@ -103,14 +116,14 @@ class UnitSystem:
|
||||
"""Determine if this is the metric unit system."""
|
||||
return self.name == CONF_UNIT_SYSTEM_METRIC
|
||||
|
||||
def temperature(self, temperature: float, from_unit: str) -> float:
|
||||
def temperature(self, temperature: float, from_unit: UnitTemperatureT) -> float:
|
||||
"""Convert the given temperature to this unit system."""
|
||||
if not isinstance(temperature, Number):
|
||||
raise TypeError(f"{temperature!s} is not a numeric value.")
|
||||
|
||||
return temperature_util.convert(temperature, from_unit, self.temperature_unit)
|
||||
|
||||
def length(self, length: float | None, from_unit: str) -> float:
|
||||
def length(self, length: float | None, from_unit: UnitLengthT) -> float:
|
||||
"""Convert the given length to this unit system."""
|
||||
if not isinstance(length, Number):
|
||||
raise TypeError(f"{length!s} is not a numeric value.")
|
||||
@ -120,7 +133,7 @@ class UnitSystem:
|
||||
length, from_unit, self.length_unit
|
||||
)
|
||||
|
||||
def pressure(self, pressure: float | None, from_unit: str) -> float:
|
||||
def pressure(self, pressure: float | None, from_unit: UnitPressureT) -> float:
|
||||
"""Convert the given pressure to this unit system."""
|
||||
if not isinstance(pressure, Number):
|
||||
raise TypeError(f"{pressure!s} is not a numeric value.")
|
||||
@ -130,7 +143,7 @@ class UnitSystem:
|
||||
pressure, from_unit, self.pressure_unit
|
||||
)
|
||||
|
||||
def volume(self, volume: float | None, from_unit: str) -> float:
|
||||
def volume(self, volume: float | None, from_unit: UnitVolumeT) -> float:
|
||||
"""Convert the given volume to this unit system."""
|
||||
if not isinstance(volume, Number):
|
||||
raise TypeError(f"{volume!s} is not a numeric value.")
|
||||
@ -138,7 +151,7 @@ class UnitSystem:
|
||||
# type ignore: https://github.com/python/mypy/issues/7207
|
||||
return volume_util.convert(volume, from_unit, self.volume_unit) # type: ignore
|
||||
|
||||
def as_dict(self) -> dict[str, str]:
|
||||
def as_dict(self) -> dict[str, UnitT]:
|
||||
"""Convert the unit system to a dictionary."""
|
||||
return {
|
||||
LENGTH: self.length_unit,
|
||||
|
Reference in New Issue
Block a user