Revert new unit types (#53226)

This commit is contained in:
Marc Mueller
2021-07-20 14:13:51 +02:00
committed by GitHub
parent b4a50f5459
commit a56485a8c5
16 changed files with 153 additions and 235 deletions

View File

@@ -24,13 +24,6 @@ from homeassistant.const import (
VOLUME,
VOLUME_GALLONS,
VOLUME_LITERS,
UnitLengthT,
UnitMassT,
UnitPressureT,
UnitT,
UnitTemperatureT,
UnitTypeT,
UnitVolumeT,
)
from homeassistant.util import (
distance as distance_util,
@@ -43,23 +36,17 @@ from homeassistant.util import (
LENGTH_UNITS = distance_util.VALID_UNITS
MASS_UNITS: tuple[UnitMassT, ...] = (
MASS_POUNDS,
MASS_OUNCES,
MASS_KILOGRAMS,
MASS_GRAMS,
)
MASS_UNITS: tuple[str, ...] = (MASS_POUNDS, MASS_OUNCES, MASS_KILOGRAMS, MASS_GRAMS)
PRESSURE_UNITS = pressure_util.VALID_UNITS
VOLUME_UNITS = volume_util.VALID_UNITS
TEMPERATURE_UNITS: tuple[UnitTemperatureT, ...] = (TEMP_FAHRENHEIT, TEMP_CELSIUS)
TEMPERATURE_UNITS: tuple[str, ...] = (TEMP_FAHRENHEIT, TEMP_CELSIUS)
def is_valid_unit(unit: UnitT, unit_type: UnitTypeT) -> bool:
def is_valid_unit(unit: str, unit_type: str) -> 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:
@@ -82,11 +69,11 @@ class UnitSystem:
def __init__(
self,
name: str,
temperature: UnitTemperatureT,
length: UnitLengthT,
volume: UnitVolumeT,
mass: UnitMassT,
pressure: UnitPressureT,
temperature: str,
length: str,
volume: str,
mass: str,
pressure: str,
) -> None:
"""Initialize the unit system object."""
errors: str = ", ".join(
@@ -116,14 +103,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: UnitTemperatureT) -> float:
def temperature(self, temperature: float, from_unit: str) -> 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: UnitLengthT) -> float:
def length(self, length: float | None, from_unit: str) -> float:
"""Convert the given length to this unit system."""
if not isinstance(length, Number):
raise TypeError(f"{length!s} is not a numeric value.")
@@ -133,7 +120,7 @@ class UnitSystem:
length, from_unit, self.length_unit
)
def pressure(self, pressure: float | None, from_unit: UnitPressureT) -> float:
def pressure(self, pressure: float | None, from_unit: str) -> float:
"""Convert the given pressure to this unit system."""
if not isinstance(pressure, Number):
raise TypeError(f"{pressure!s} is not a numeric value.")
@@ -143,7 +130,7 @@ class UnitSystem:
pressure, from_unit, self.pressure_unit
)
def volume(self, volume: float | None, from_unit: UnitVolumeT) -> float:
def volume(self, volume: float | None, from_unit: str) -> float:
"""Convert the given volume to this unit system."""
if not isinstance(volume, Number):
raise TypeError(f"{volume!s} is not a numeric value.")
@@ -151,7 +138,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, UnitT]:
def as_dict(self) -> dict[str, str]:
"""Convert the unit system to a dictionary."""
return {
LENGTH: self.length_unit,