From 4ad9633dfb3979489d0a222753feb467b648ac42 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 29 Nov 2022 18:10:31 +0100 Subject: [PATCH] Use new unit enums in weather entity (#82937) --- homeassistant/components/demo/weather.py | 21 ++---- homeassistant/components/weather/__init__.py | 68 ++++++++------------ homeassistant/util/unit_system.py | 5 +- 3 files changed, 38 insertions(+), 56 deletions(-) diff --git a/homeassistant/components/demo/weather.py b/homeassistant/components/demo/weather.py index cd1a3a6258c..e64d0bcc28d 100644 --- a/homeassistant/components/demo/weather.py +++ b/homeassistant/components/demo/weather.py @@ -22,14 +22,7 @@ from homeassistant.components.weather import ( 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 @@ -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], @@ -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], diff --git a/homeassistant/components/weather/__init__.py b/homeassistant/components/weather/__init__.py index 8ffced6d5d2..610b8f7c355 100644 --- a/homeassistant/components/weather/__init__.py +++ b/homeassistant/components/weather/__init__.py @@ -11,24 +11,14 @@ from typing import Any, Final, TypedDict, final 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 @@ -44,7 +34,7 @@ from homeassistant.util.unit_conversion import ( SpeedConverter, TemperatureConverter, ) -from homeassistant.util.unit_system import METRIC_SYSTEM +from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM _LOGGER = logging.getLogger(__name__) @@ -101,29 +91,29 @@ SCAN_INTERVAL = timedelta(seconds=30) 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]] = { @@ -420,9 +410,9 @@ class WeatherEntity(Entity): 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 @@ -484,11 +474,9 @@ class WeatherEntity(Entity): 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 @@ -623,7 +611,7 @@ class WeatherEntity(Entity): return self._attr_precision return ( PRECISION_TENTHS - if self._temperature_unit == TEMP_CELSIUS + if self._temperature_unit == UnitOfTemperature.CELSIUS else PRECISION_WHOLE ) diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 6350d80f3d7..9c6b6045cf9 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -17,6 +17,7 @@ from homeassistant.const import ( WIND_SPEED, UnitOfLength, UnitOfMass, + UnitOfPrecipitationDepth, UnitOfPressure, UnitOfSpeed, UnitOfTemperature, @@ -247,7 +248,7 @@ validate_unit_system = vol.All( 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, @@ -279,7 +280,7 @@ METRIC_SYSTEM = UnitSystem( 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,