This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+84 -46
View File
@@ -5,11 +5,27 @@ from typing import Optional
from numbers import Number
from homeassistant.const import (
TEMP_CELSIUS, TEMP_FAHRENHEIT, LENGTH_MILES, LENGTH_KILOMETERS,
PRESSURE_PA, PRESSURE_PSI, VOLUME_LITERS, VOLUME_GALLONS,
MASS_GRAMS, MASS_KILOGRAMS, MASS_OUNCES, MASS_POUNDS,
CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL, LENGTH, MASS, PRESSURE,
VOLUME, TEMPERATURE, UNIT_NOT_RECOGNIZED_TEMPLATE)
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
LENGTH_MILES,
LENGTH_KILOMETERS,
PRESSURE_PA,
PRESSURE_PSI,
VOLUME_LITERS,
VOLUME_GALLONS,
MASS_GRAMS,
MASS_KILOGRAMS,
MASS_OUNCES,
MASS_POUNDS,
CONF_UNIT_SYSTEM_METRIC,
CONF_UNIT_SYSTEM_IMPERIAL,
LENGTH,
MASS,
PRESSURE,
VOLUME,
TEMPERATURE,
UNIT_NOT_RECOGNIZED_TEMPLATE,
)
from homeassistant.util import temperature as temperature_util
from homeassistant.util import distance as distance_util
from homeassistant.util import pressure as pressure_util
@@ -19,21 +35,13 @@ _LOGGER = logging.getLogger(__name__)
LENGTH_UNITS = distance_util.VALID_UNITS
MASS_UNITS = [
MASS_POUNDS,
MASS_OUNCES,
MASS_KILOGRAMS,
MASS_GRAMS,
]
MASS_UNITS = [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 = [TEMP_FAHRENHEIT, TEMP_CELSIUS]
def is_valid_unit(unit: str, unit_type: str) -> bool:
@@ -57,18 +65,27 @@ def is_valid_unit(unit: str, unit_type: str) -> bool:
class UnitSystem:
"""A container for units of measure."""
def __init__(self, name: str, temperature: str, length: str,
volume: str, mass: str, pressure: str) -> None:
def __init__(
self,
name: str,
temperature: str,
length: str,
volume: str,
mass: str,
pressure: str,
) -> None:
"""Initialize the unit system object."""
errors = \
', '.join(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit, unit_type)
for unit, unit_type in [
(temperature, TEMPERATURE),
(length, LENGTH),
(volume, VOLUME),
(mass, MASS),
(pressure, PRESSURE), ]
if not is_valid_unit(unit, unit_type)) # type: str
errors = ", ".join(
UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit, unit_type)
for unit, unit_type in [
(temperature, TEMPERATURE),
(length, LENGTH),
(volume, VOLUME),
(mass, MASS),
(pressure, PRESSURE),
]
if not is_valid_unit(unit, unit_type)
) # type: str
if errors:
raise ValueError(errors)
@@ -88,39 +105,50 @@ class UnitSystem:
def temperature(self, temperature: float, from_unit: str) -> float:
"""Convert the given temperature to this unit system."""
if not isinstance(temperature, Number):
raise TypeError(
'{} is not a numeric value.'.format(str(temperature)))
raise TypeError("{} is not a numeric value.".format(str(temperature)))
# type ignore: https://github.com/python/mypy/issues/7207
return temperature_util.convert(temperature, # type: ignore
from_unit, self.temperature_unit)
return temperature_util.convert(
temperature, # type: ignore
from_unit,
self.temperature_unit,
)
def length(self, length: Optional[float], from_unit: str) -> float:
"""Convert the given length to this unit system."""
if not isinstance(length, Number):
raise TypeError('{} is not a numeric value.'.format(str(length)))
raise TypeError("{} is not a numeric value.".format(str(length)))
# type ignore: https://github.com/python/mypy/issues/7207
return distance_util.convert(length, from_unit, # type: ignore
self.length_unit)
return distance_util.convert(
length,
from_unit, # type: ignore
self.length_unit,
)
def pressure(self, pressure: Optional[float], from_unit: str) -> float:
"""Convert the given pressure to this unit system."""
if not isinstance(pressure, Number):
raise TypeError('{} is not a numeric value.'.format(str(pressure)))
raise TypeError("{} is not a numeric value.".format(str(pressure)))
# type ignore: https://github.com/python/mypy/issues/7207
return pressure_util.convert(pressure, from_unit, # type: ignore
self.pressure_unit)
return pressure_util.convert(
pressure,
from_unit, # type: ignore
self.pressure_unit,
)
def volume(self, volume: Optional[float], from_unit: str) -> float:
"""Convert the given volume to this unit system."""
if not isinstance(volume, Number):
raise TypeError('{} is not a numeric value.'.format(str(volume)))
raise TypeError("{} is not a numeric value.".format(str(volume)))
# type ignore: https://github.com/python/mypy/issues/7207
return volume_util.convert(volume, from_unit, # type: ignore
self.volume_unit)
return volume_util.convert(
volume,
from_unit, # type: ignore
self.volume_unit,
)
def as_dict(self) -> dict:
"""Convert the unit system to a dictionary."""
@@ -129,14 +157,24 @@ class UnitSystem:
MASS: self.mass_unit,
PRESSURE: self.pressure_unit,
TEMPERATURE: self.temperature_unit,
VOLUME: self.volume_unit
VOLUME: self.volume_unit,
}
METRIC_SYSTEM = UnitSystem(CONF_UNIT_SYSTEM_METRIC, TEMP_CELSIUS,
LENGTH_KILOMETERS, VOLUME_LITERS, MASS_GRAMS,
PRESSURE_PA)
METRIC_SYSTEM = UnitSystem(
CONF_UNIT_SYSTEM_METRIC,
TEMP_CELSIUS,
LENGTH_KILOMETERS,
VOLUME_LITERS,
MASS_GRAMS,
PRESSURE_PA,
)
IMPERIAL_SYSTEM = UnitSystem(CONF_UNIT_SYSTEM_IMPERIAL, TEMP_FAHRENHEIT,
LENGTH_MILES, VOLUME_GALLONS, MASS_POUNDS,
PRESSURE_PSI)
IMPERIAL_SYSTEM = UnitSystem(
CONF_UNIT_SYSTEM_IMPERIAL,
TEMP_FAHRENHEIT,
LENGTH_MILES,
VOLUME_GALLONS,
MASS_POUNDS,
PRESSURE_PSI,
)