Migrate accuweather weather to native_* (#74407)

This commit is contained in:
Erik Montnemery 2022-07-04 15:12:45 +02:00 committed by GitHub
parent 402a40c108
commit 5f5f1343cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,19 +6,26 @@ from typing import Any, cast
from homeassistant.components.weather import ( from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION, ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_NATIVE_PRECIPITATION,
ATTR_FORECAST_NATIVE_TEMP,
ATTR_FORECAST_NATIVE_TEMP_LOW,
ATTR_FORECAST_NATIVE_WIND_SPEED,
ATTR_FORECAST_PRECIPITATION_PROBABILITY, ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME, ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING, ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
Forecast, Forecast,
WeatherEntity, WeatherEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_NAME, CONF_NAME,
LENGTH_INCHES,
LENGTH_KILOMETERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
PRESSURE_HPA,
PRESSURE_INHG,
SPEED_KILOMETERS_PER_HOUR,
SPEED_MILES_PER_HOUR, SPEED_MILES_PER_HOUR,
TEMP_CELSIUS, TEMP_CELSIUS,
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
@ -66,19 +73,25 @@ class AccuWeatherEntity(
) -> None: ) -> None:
"""Initialize.""" """Initialize."""
super().__init__(coordinator) super().__init__(coordinator)
self._unit_system = API_METRIC if coordinator.is_metric else API_IMPERIAL # Coordinator data is used also for sensors which don't have units automatically
wind_speed_unit = self.coordinator.data["Wind"]["Speed"][self._unit_system][ # converted, hence the weather entity's native units follow the configured unit
"Unit" # system
] if coordinator.is_metric:
if wind_speed_unit == "mi/h": self._attr_native_precipitation_unit = LENGTH_MILLIMETERS
self._attr_wind_speed_unit = SPEED_MILES_PER_HOUR self._attr_native_pressure_unit = PRESSURE_HPA
self._attr_native_temperature_unit = TEMP_CELSIUS
self._attr_native_visibility_unit = LENGTH_KILOMETERS
self._attr_native_wind_speed_unit = SPEED_KILOMETERS_PER_HOUR
self._unit_system = API_METRIC
else: else:
self._attr_wind_speed_unit = wind_speed_unit self._unit_system = API_IMPERIAL
self._attr_native_precipitation_unit = LENGTH_INCHES
self._attr_native_pressure_unit = PRESSURE_INHG
self._attr_native_temperature_unit = TEMP_FAHRENHEIT
self._attr_native_visibility_unit = LENGTH_MILES
self._attr_native_wind_speed_unit = SPEED_MILES_PER_HOUR
self._attr_name = name self._attr_name = name
self._attr_unique_id = coordinator.location_key self._attr_unique_id = coordinator.location_key
self._attr_temperature_unit = (
TEMP_CELSIUS if coordinator.is_metric else TEMP_FAHRENHEIT
)
self._attr_attribution = ATTRIBUTION self._attr_attribution = ATTRIBUTION
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE, entry_type=DeviceEntryType.SERVICE,
@ -106,14 +119,14 @@ class AccuWeatherEntity(
return None return None
@property @property
def temperature(self) -> float: def native_temperature(self) -> float:
"""Return the temperature.""" """Return the temperature."""
return cast( return cast(
float, self.coordinator.data["Temperature"][self._unit_system]["Value"] float, self.coordinator.data["Temperature"][self._unit_system]["Value"]
) )
@property @property
def pressure(self) -> float: def native_pressure(self) -> float:
"""Return the pressure.""" """Return the pressure."""
return cast( return cast(
float, self.coordinator.data["Pressure"][self._unit_system]["Value"] float, self.coordinator.data["Pressure"][self._unit_system]["Value"]
@ -125,7 +138,7 @@ class AccuWeatherEntity(
return cast(int, self.coordinator.data["RelativeHumidity"]) return cast(int, self.coordinator.data["RelativeHumidity"])
@property @property
def wind_speed(self) -> float: def native_wind_speed(self) -> float:
"""Return the wind speed.""" """Return the wind speed."""
return cast( return cast(
float, self.coordinator.data["Wind"]["Speed"][self._unit_system]["Value"] float, self.coordinator.data["Wind"]["Speed"][self._unit_system]["Value"]
@ -137,7 +150,7 @@ class AccuWeatherEntity(
return cast(int, self.coordinator.data["Wind"]["Direction"]["Degrees"]) return cast(int, self.coordinator.data["Wind"]["Direction"]["Degrees"])
@property @property
def visibility(self) -> float: def native_visibility(self) -> float:
"""Return the visibility.""" """Return the visibility."""
return cast( return cast(
float, self.coordinator.data["Visibility"][self._unit_system]["Value"] float, self.coordinator.data["Visibility"][self._unit_system]["Value"]
@ -162,9 +175,9 @@ class AccuWeatherEntity(
return [ return [
{ {
ATTR_FORECAST_TIME: utc_from_timestamp(item["EpochDate"]).isoformat(), ATTR_FORECAST_TIME: utc_from_timestamp(item["EpochDate"]).isoformat(),
ATTR_FORECAST_TEMP: item["TemperatureMax"]["Value"], ATTR_FORECAST_NATIVE_TEMP: item["TemperatureMax"]["Value"],
ATTR_FORECAST_TEMP_LOW: item["TemperatureMin"]["Value"], ATTR_FORECAST_NATIVE_TEMP_LOW: item["TemperatureMin"]["Value"],
ATTR_FORECAST_PRECIPITATION: self._calc_precipitation(item), ATTR_FORECAST_NATIVE_PRECIPITATION: self._calc_precipitation(item),
ATTR_FORECAST_PRECIPITATION_PROBABILITY: round( ATTR_FORECAST_PRECIPITATION_PROBABILITY: round(
mean( mean(
[ [
@ -173,7 +186,7 @@ class AccuWeatherEntity(
] ]
) )
), ),
ATTR_FORECAST_WIND_SPEED: item["WindDay"]["Speed"]["Value"], ATTR_FORECAST_NATIVE_WIND_SPEED: item["WindDay"]["Speed"]["Value"],
ATTR_FORECAST_WIND_BEARING: item["WindDay"]["Direction"]["Degrees"], ATTR_FORECAST_WIND_BEARING: item["WindDay"]["Direction"]["Degrees"],
ATTR_FORECAST_CONDITION: [ ATTR_FORECAST_CONDITION: [
k for k, v in CONDITION_CLASSES.items() if item["IconDay"] in v k for k, v in CONDITION_CLASSES.items() if item["IconDay"] in v