1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Fix pressure in dark sky and openweathermap and add pressure utility (#21210)

This commit is contained in:
MatthewFlamm
2019-03-24 13:37:31 -04:00
committed by Sebastian Muszynski
parent 6988fe783c
commit ed93c3b2c1
8 changed files with 212 additions and 36 deletions

View File

@@ -5,27 +5,19 @@ from typing import Optional
from numbers import Number
from homeassistant.const import (
TEMP_CELSIUS, TEMP_FAHRENHEIT, LENGTH_CENTIMETERS, LENGTH_METERS,
LENGTH_KILOMETERS, LENGTH_INCHES, LENGTH_FEET, LENGTH_YARD, LENGTH_MILES,
VOLUME_LITERS, VOLUME_MILLILITERS, VOLUME_GALLONS, VOLUME_FLUID_OUNCE,
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, VOLUME,
TEMPERATURE, UNIT_NOT_RECOGNIZED_TEMPLATE)
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
from homeassistant.util import volume as volume_util
_LOGGER = logging.getLogger(__name__)
LENGTH_UNITS = [
LENGTH_MILES,
LENGTH_YARD,
LENGTH_FEET,
LENGTH_INCHES,
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_CENTIMETERS,
]
LENGTH_UNITS = distance_util.VALID_UNITS
MASS_UNITS = [
MASS_POUNDS,
@@ -34,12 +26,9 @@ MASS_UNITS = [
MASS_GRAMS,
]
VOLUME_UNITS = [
VOLUME_GALLONS,
VOLUME_FLUID_OUNCE,
VOLUME_LITERS,
VOLUME_MILLILITERS,
]
PRESSURE_UNITS = pressure_util.VALID_UNITS
VOLUME_UNITS = volume_util.VALID_UNITS
TEMPERATURE_UNITS = [
TEMP_FAHRENHEIT,
@@ -57,6 +46,8 @@ def is_valid_unit(unit: str, unit_type: str) -> bool:
units = MASS_UNITS
elif unit_type == VOLUME:
units = VOLUME_UNITS
elif unit_type == PRESSURE:
units = PRESSURE_UNITS
else:
return False
@@ -67,7 +58,7 @@ class UnitSystem:
"""A container for units of measure."""
def __init__(self, name: str, temperature: str, length: str,
volume: str, mass: str) -> None:
volume: str, mass: str, pressure: str) -> None:
"""Initialize the unit system object."""
errors = \
', '.join(UNIT_NOT_RECOGNIZED_TEMPLATE.format(unit, unit_type)
@@ -75,7 +66,8 @@ class UnitSystem:
(temperature, TEMPERATURE),
(length, LENGTH),
(volume, VOLUME),
(mass, MASS), ]
(mass, MASS),
(pressure, PRESSURE), ]
if not is_valid_unit(unit, unit_type)) # type: str
if errors:
@@ -85,6 +77,7 @@ class UnitSystem:
self.temperature_unit = temperature
self.length_unit = length
self.mass_unit = mass
self.pressure_unit = pressure
self.volume_unit = volume
@property
@@ -109,6 +102,14 @@ class UnitSystem:
return distance_util.convert(length, from_unit,
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)))
return pressure_util.convert(pressure, from_unit,
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):
@@ -121,13 +122,16 @@ class UnitSystem:
return {
LENGTH: self.length_unit,
MASS: self.mass_unit,
PRESSURE: self.pressure_unit,
TEMPERATURE: self.temperature_unit,
VOLUME: self.volume_unit
}
METRIC_SYSTEM = UnitSystem(CONF_UNIT_SYSTEM_METRIC, TEMP_CELSIUS,
LENGTH_KILOMETERS, VOLUME_LITERS, MASS_GRAMS)
LENGTH_KILOMETERS, VOLUME_LITERS, MASS_GRAMS,
PRESSURE_PA)
IMPERIAL_SYSTEM = UnitSystem(CONF_UNIT_SYSTEM_IMPERIAL, TEMP_FAHRENHEIT,
LENGTH_MILES, VOLUME_GALLONS, MASS_POUNDS)
LENGTH_MILES, VOLUME_GALLONS, MASS_POUNDS,
PRESSURE_PSI)