Use new unit enums in weather entity (#82937)

pull/82946/head
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,
)
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],

View File

@ -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
)

View File

@ -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,