Use new unit enums in weather entity (#82937)

This commit is contained in:
epenet 2022-11-29 18:10:31 +01:00 committed by GitHub
parent 0561c14d53
commit 4ad9633dfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 56 deletions

View File

@ -22,14 +22,7 @@ from homeassistant.components.weather import (
WeatherEntity, WeatherEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import UnitOfPressure, UnitOfSpeed, UnitOfTemperature
PRESSURE_HPA,
PRESSURE_INHG,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -78,9 +71,9 @@ def setup_platform(
92, 92,
1099, 1099,
0.5, 0.5,
TEMP_CELSIUS, UnitOfTemperature.CELSIUS,
PRESSURE_HPA, UnitOfPressure.HPA,
SPEED_METERS_PER_SECOND, UnitOfSpeed.METERS_PER_SECOND,
[ [
[ATTR_CONDITION_RAINY, 1, 22, 15, 60], [ATTR_CONDITION_RAINY, 1, 22, 15, 60],
[ATTR_CONDITION_RAINY, 5, 19, 8, 30], [ATTR_CONDITION_RAINY, 5, 19, 8, 30],
@ -98,9 +91,9 @@ def setup_platform(
54, 54,
987, 987,
4.8, 4.8,
TEMP_FAHRENHEIT, UnitOfTemperature.FAHRENHEIT,
PRESSURE_INHG, UnitOfPressure.INHG,
SPEED_MILES_PER_HOUR, UnitOfSpeed.MILES_PER_HOUR,
[ [
[ATTR_CONDITION_SNOWY, 2, -10, -15, 60], [ATTR_CONDITION_SNOWY, 2, -10, -15, 60],
[ATTR_CONDITION_PARTLYCLOUDY, 1, -13, -14, 25], [ATTR_CONDITION_PARTLYCLOUDY, 1, -13, -14, 25],

View File

@ -11,24 +11,14 @@ from typing import Any, Final, TypedDict, final
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
LENGTH_INCHES,
LENGTH_KILOMETERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
PRECISION_HALVES, PRECISION_HALVES,
PRECISION_TENTHS, PRECISION_TENTHS,
PRECISION_WHOLE, PRECISION_WHOLE,
PRESSURE_HPA, UnitOfLength,
PRESSURE_INHG, UnitOfPrecipitationDepth,
PRESSURE_MBAR, UnitOfPressure,
PRESSURE_MMHG, UnitOfSpeed,
SPEED_FEET_PER_SECOND, UnitOfTemperature,
SPEED_KILOMETERS_PER_HOUR,
SPEED_KNOTS,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.config_validation import ( # noqa: F401 from homeassistant.helpers.config_validation import ( # noqa: F401
@ -44,7 +34,7 @@ from homeassistant.util.unit_conversion import (
SpeedConverter, SpeedConverter,
TemperatureConverter, TemperatureConverter,
) )
from homeassistant.util.unit_system import METRIC_SYSTEM from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -101,29 +91,29 @@ SCAN_INTERVAL = timedelta(seconds=30)
ROUNDING_PRECISION = 2 ROUNDING_PRECISION = 2
VALID_UNITS_PRESSURE: set[str] = { VALID_UNITS_PRESSURE: set[str] = {
PRESSURE_HPA, UnitOfPressure.HPA,
PRESSURE_MBAR, UnitOfPressure.MBAR,
PRESSURE_INHG, UnitOfPressure.INHG,
PRESSURE_MMHG, UnitOfPressure.MMHG,
} }
VALID_UNITS_TEMPERATURE: set[str] = { VALID_UNITS_TEMPERATURE: set[str] = {
TEMP_CELSIUS, UnitOfTemperature.CELSIUS,
TEMP_FAHRENHEIT, UnitOfTemperature.FAHRENHEIT,
} }
VALID_UNITS_PRECIPITATION: set[str] = { VALID_UNITS_PRECIPITATION: set[str] = {
LENGTH_MILLIMETERS, UnitOfPrecipitationDepth.MILLIMETERS,
LENGTH_INCHES, UnitOfPrecipitationDepth.INCHES,
} }
VALID_UNITS_VISIBILITY: set[str] = { VALID_UNITS_VISIBILITY: set[str] = {
LENGTH_KILOMETERS, UnitOfLength.KILOMETERS,
LENGTH_MILES, UnitOfLength.MILES,
} }
VALID_UNITS_WIND_SPEED: set[str] = { VALID_UNITS_WIND_SPEED: set[str] = {
SPEED_FEET_PER_SECOND, UnitOfSpeed.FEET_PER_SECOND,
SPEED_KILOMETERS_PER_HOUR, UnitOfSpeed.KILOMETERS_PER_HOUR,
SPEED_KNOTS, UnitOfSpeed.KNOTS,
SPEED_METERS_PER_SECOND, UnitOfSpeed.METERS_PER_SECOND,
SPEED_MILES_PER_HOUR, UnitOfSpeed.MILES_PER_HOUR,
} }
UNIT_CONVERSIONS: dict[str, Callable[[float, str, str], float]] = { UNIT_CONVERSIONS: dict[str, Callable[[float, str, str], float]] = {
@ -420,9 +410,9 @@ class WeatherEntity(Entity):
Should not be set by integrations. Should not be set by integrations.
""" """
return ( if self.hass.config.units is US_CUSTOMARY_SYSTEM:
PRESSURE_HPA if self.hass.config.units is METRIC_SYSTEM else PRESSURE_INHG return UnitOfPressure.INHG
) return UnitOfPressure.HPA
@final @final
@property @property
@ -484,11 +474,9 @@ class WeatherEntity(Entity):
Should not be set by integrations. Should not be set by integrations.
""" """
return ( if self.hass.config.units is US_CUSTOMARY_SYSTEM:
SPEED_KILOMETERS_PER_HOUR return UnitOfSpeed.MILES_PER_HOUR
if self.hass.config.units is METRIC_SYSTEM return UnitOfSpeed.KILOMETERS_PER_HOUR
else SPEED_MILES_PER_HOUR
)
@final @final
@property @property
@ -623,7 +611,7 @@ class WeatherEntity(Entity):
return self._attr_precision return self._attr_precision
return ( return (
PRECISION_TENTHS PRECISION_TENTHS
if self._temperature_unit == TEMP_CELSIUS if self._temperature_unit == UnitOfTemperature.CELSIUS
else PRECISION_WHOLE else PRECISION_WHOLE
) )

View File

@ -17,6 +17,7 @@ from homeassistant.const import (
WIND_SPEED, WIND_SPEED,
UnitOfLength, UnitOfLength,
UnitOfMass, UnitOfMass,
UnitOfPrecipitationDepth,
UnitOfPressure, UnitOfPressure,
UnitOfSpeed, UnitOfSpeed,
UnitOfTemperature, UnitOfTemperature,
@ -247,7 +248,7 @@ validate_unit_system = vol.All(
METRIC_SYSTEM = UnitSystem( METRIC_SYSTEM = UnitSystem(
_CONF_UNIT_SYSTEM_METRIC, _CONF_UNIT_SYSTEM_METRIC,
accumulated_precipitation=UnitOfLength.MILLIMETERS, accumulated_precipitation=UnitOfPrecipitationDepth.MILLIMETERS,
conversions={ conversions={
# Convert non-metric distances # Convert non-metric distances
("distance", UnitOfLength.FEET): UnitOfLength.METERS, ("distance", UnitOfLength.FEET): UnitOfLength.METERS,
@ -279,7 +280,7 @@ METRIC_SYSTEM = UnitSystem(
US_CUSTOMARY_SYSTEM = UnitSystem( US_CUSTOMARY_SYSTEM = UnitSystem(
_CONF_UNIT_SYSTEM_US_CUSTOMARY, _CONF_UNIT_SYSTEM_US_CUSTOMARY,
accumulated_precipitation=UnitOfLength.INCHES, accumulated_precipitation=UnitOfPrecipitationDepth.INCHES,
conversions={ conversions={
# Convert non-USCS distances # Convert non-USCS distances
("distance", UnitOfLength.CENTIMETERS): UnitOfLength.INCHES, ("distance", UnitOfLength.CENTIMETERS): UnitOfLength.INCHES,