Skip to content

Commit

Permalink
Use new unit enums in weather entity (#82937)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Nov 29, 2022
1 parent 0561c14 commit 4ad9633
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 56 deletions.
21 changes: 7 additions & 14 deletions homeassistant/components/demo/weather.py
Expand Up @@ -22,14 +22,7 @@
WeatherEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PRESSURE_HPA,
PRESSURE_INHG,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.const import UnitOfPressure, UnitOfSpeed, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
Expand Down Expand Up @@ -78,9 +71,9 @@ def setup_platform(
92,
1099,
0.5,
TEMP_CELSIUS,
PRESSURE_HPA,
SPEED_METERS_PER_SECOND,
UnitOfTemperature.CELSIUS,
UnitOfPressure.HPA,
UnitOfSpeed.METERS_PER_SECOND,
[
[ATTR_CONDITION_RAINY, 1, 22, 15, 60],
[ATTR_CONDITION_RAINY, 5, 19, 8, 30],
Expand All @@ -98,9 +91,9 @@ def setup_platform(
54,
987,
4.8,
TEMP_FAHRENHEIT,
PRESSURE_INHG,
SPEED_MILES_PER_HOUR,
UnitOfTemperature.FAHRENHEIT,
UnitOfPressure.INHG,
UnitOfSpeed.MILES_PER_HOUR,
[
[ATTR_CONDITION_SNOWY, 2, -10, -15, 60],
[ATTR_CONDITION_PARTLYCLOUDY, 1, -13, -14, 25],
Expand Down
68 changes: 28 additions & 40 deletions homeassistant/components/weather/__init__.py
Expand Up @@ -11,24 +11,14 @@

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
LENGTH_INCHES,
LENGTH_KILOMETERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
PRECISION_HALVES,
PRECISION_TENTHS,
PRECISION_WHOLE,
PRESSURE_HPA,
PRESSURE_INHG,
PRESSURE_MBAR,
PRESSURE_MMHG,
SPEED_FEET_PER_SECOND,
SPEED_KILOMETERS_PER_HOUR,
SPEED_KNOTS,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfLength,
UnitOfPrecipitationDepth,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.config_validation import ( # noqa: F401
Expand All @@ -44,7 +34,7 @@
SpeedConverter,
TemperatureConverter,
)
from homeassistant.util.unit_system import METRIC_SYSTEM
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -101,29 +91,29 @@
ROUNDING_PRECISION = 2

VALID_UNITS_PRESSURE: set[str] = {
PRESSURE_HPA,
PRESSURE_MBAR,
PRESSURE_INHG,
PRESSURE_MMHG,
UnitOfPressure.HPA,
UnitOfPressure.MBAR,
UnitOfPressure.INHG,
UnitOfPressure.MMHG,
}
VALID_UNITS_TEMPERATURE: set[str] = {
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
UnitOfTemperature.CELSIUS,
UnitOfTemperature.FAHRENHEIT,
}
VALID_UNITS_PRECIPITATION: set[str] = {
LENGTH_MILLIMETERS,
LENGTH_INCHES,
UnitOfPrecipitationDepth.MILLIMETERS,
UnitOfPrecipitationDepth.INCHES,
}
VALID_UNITS_VISIBILITY: set[str] = {
LENGTH_KILOMETERS,
LENGTH_MILES,
UnitOfLength.KILOMETERS,
UnitOfLength.MILES,
}
VALID_UNITS_WIND_SPEED: set[str] = {
SPEED_FEET_PER_SECOND,
SPEED_KILOMETERS_PER_HOUR,
SPEED_KNOTS,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
UnitOfSpeed.FEET_PER_SECOND,
UnitOfSpeed.KILOMETERS_PER_HOUR,
UnitOfSpeed.KNOTS,
UnitOfSpeed.METERS_PER_SECOND,
UnitOfSpeed.MILES_PER_HOUR,
}

UNIT_CONVERSIONS: dict[str, Callable[[float, str, str], float]] = {
Expand Down Expand Up @@ -420,9 +410,9 @@ def _default_pressure_unit(self) -> str:
Should not be set by integrations.
"""
return (
PRESSURE_HPA if self.hass.config.units is METRIC_SYSTEM else PRESSURE_INHG
)
if self.hass.config.units is US_CUSTOMARY_SYSTEM:
return UnitOfPressure.INHG
return UnitOfPressure.HPA

@final
@property
Expand Down Expand Up @@ -484,11 +474,9 @@ def _default_wind_speed_unit(self) -> str:
Should not be set by integrations.
"""
return (
SPEED_KILOMETERS_PER_HOUR
if self.hass.config.units is METRIC_SYSTEM
else SPEED_MILES_PER_HOUR
)
if self.hass.config.units is US_CUSTOMARY_SYSTEM:
return UnitOfSpeed.MILES_PER_HOUR
return UnitOfSpeed.KILOMETERS_PER_HOUR

@final
@property
Expand Down Expand Up @@ -623,7 +611,7 @@ def precision(self) -> float:
return self._attr_precision
return (
PRECISION_TENTHS
if self._temperature_unit == TEMP_CELSIUS
if self._temperature_unit == UnitOfTemperature.CELSIUS
else PRECISION_WHOLE
)

Expand Down
5 changes: 3 additions & 2 deletions homeassistant/util/unit_system.py
Expand Up @@ -17,6 +17,7 @@
WIND_SPEED,
UnitOfLength,
UnitOfMass,
UnitOfPrecipitationDepth,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
Expand Down Expand Up @@ -247,7 +248,7 @@ def _deprecated_unit_system(value: str) -> str:

METRIC_SYSTEM = UnitSystem(
_CONF_UNIT_SYSTEM_METRIC,
accumulated_precipitation=UnitOfLength.MILLIMETERS,
accumulated_precipitation=UnitOfPrecipitationDepth.MILLIMETERS,
conversions={
# Convert non-metric distances
("distance", UnitOfLength.FEET): UnitOfLength.METERS,
Expand Down Expand Up @@ -279,7 +280,7 @@ def _deprecated_unit_system(value: str) -> str:

US_CUSTOMARY_SYSTEM = UnitSystem(
_CONF_UNIT_SYSTEM_US_CUSTOMARY,
accumulated_precipitation=UnitOfLength.INCHES,
accumulated_precipitation=UnitOfPrecipitationDepth.INCHES,
conversions={
# Convert non-USCS distances
("distance", UnitOfLength.CENTIMETERS): UnitOfLength.INCHES,
Expand Down

0 comments on commit 4ad9633

Please sign in to comment.