Migrate openweathermap to native_* (#73913)

This commit is contained in:
Erik Montnemery 2022-06-23 21:01:08 +02:00 committed by GitHub
parent 00a79635c1
commit e57f34f0f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 35 deletions

View File

@ -22,11 +22,11 @@ from homeassistant.components.weather import (
ATTR_CONDITION_WINDY, ATTR_CONDITION_WINDY,
ATTR_CONDITION_WINDY_VARIANT, ATTR_CONDITION_WINDY_VARIANT,
ATTR_FORECAST_CONDITION, ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_NATIVE_PRECIPITATION,
ATTR_FORECAST_NATIVE_PRESSURE,
ATTR_FORECAST_NATIVE_TEMP,
ATTR_FORECAST_NATIVE_TEMP_LOW,
ATTR_FORECAST_PRECIPITATION_PROBABILITY, ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_PRESSURE,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME, ATTR_FORECAST_TIME,
) )
from homeassistant.const import ( from homeassistant.const import (
@ -266,7 +266,7 @@ FORECAST_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
name="Condition", name="Condition",
), ),
SensorEntityDescription( SensorEntityDescription(
key=ATTR_FORECAST_PRECIPITATION, key=ATTR_FORECAST_NATIVE_PRECIPITATION,
name="Precipitation", name="Precipitation",
native_unit_of_measurement=LENGTH_MILLIMETERS, native_unit_of_measurement=LENGTH_MILLIMETERS,
), ),
@ -276,19 +276,19 @@ FORECAST_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
), ),
SensorEntityDescription( SensorEntityDescription(
key=ATTR_FORECAST_PRESSURE, key=ATTR_FORECAST_NATIVE_PRESSURE,
name="Pressure", name="Pressure",
native_unit_of_measurement=PRESSURE_HPA, native_unit_of_measurement=PRESSURE_HPA,
device_class=SensorDeviceClass.PRESSURE, device_class=SensorDeviceClass.PRESSURE,
), ),
SensorEntityDescription( SensorEntityDescription(
key=ATTR_FORECAST_TEMP, key=ATTR_FORECAST_NATIVE_TEMP,
name="Temperature", name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
), ),
SensorEntityDescription( SensorEntityDescription(
key=ATTR_FORECAST_TEMP_LOW, key=ATTR_FORECAST_NATIVE_TEMP_LOW,
name="Temperature Low", name="Temperature Low",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,

View File

@ -3,12 +3,16 @@ from __future__ import annotations
from homeassistant.components.weather import Forecast, WeatherEntity from homeassistant.components.weather import Forecast, WeatherEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PRESSURE_HPA, PRESSURE_INHG, TEMP_CELSIUS from homeassistant.const import (
LENGTH_MILLIMETERS,
PRESSURE_HPA,
SPEED_METERS_PER_SECOND,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.pressure import convert as pressure_convert
from .const import ( from .const import (
ATTR_API_CONDITION, ATTR_API_CONDITION,
@ -49,7 +53,11 @@ class OpenWeatherMapWeather(WeatherEntity):
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION
_attr_should_poll = False _attr_should_poll = False
_attr_temperature_unit = TEMP_CELSIUS
_attr_native_precipitation_unit = LENGTH_MILLIMETERS
_attr_native_pressure_unit = PRESSURE_HPA
_attr_native_temperature_unit = TEMP_CELSIUS
_attr_native_wind_speed_unit = SPEED_METERS_PER_SECOND
def __init__( def __init__(
self, self,
@ -74,19 +82,14 @@ class OpenWeatherMapWeather(WeatherEntity):
return self._weather_coordinator.data[ATTR_API_CONDITION] return self._weather_coordinator.data[ATTR_API_CONDITION]
@property @property
def temperature(self) -> float | None: def native_temperature(self) -> float | None:
"""Return the temperature.""" """Return the temperature."""
return self._weather_coordinator.data[ATTR_API_TEMPERATURE] return self._weather_coordinator.data[ATTR_API_TEMPERATURE]
@property @property
def pressure(self) -> float | None: def native_pressure(self) -> float | None:
"""Return the pressure.""" """Return the pressure."""
pressure = self._weather_coordinator.data[ATTR_API_PRESSURE] return self._weather_coordinator.data[ATTR_API_PRESSURE]
# OpenWeatherMap returns pressure in hPA, so convert to
# inHg if we aren't using metric.
if not self.hass.config.units.is_metric and pressure:
return pressure_convert(pressure, PRESSURE_HPA, PRESSURE_INHG)
return pressure
@property @property
def humidity(self) -> float | None: def humidity(self) -> float | None:
@ -94,12 +97,9 @@ class OpenWeatherMapWeather(WeatherEntity):
return self._weather_coordinator.data[ATTR_API_HUMIDITY] return self._weather_coordinator.data[ATTR_API_HUMIDITY]
@property @property
def wind_speed(self) -> float | None: def native_wind_speed(self) -> float | None:
"""Return the wind speed.""" """Return the wind speed."""
wind_speed = self._weather_coordinator.data[ATTR_API_WIND_SPEED] return self._weather_coordinator.data[ATTR_API_WIND_SPEED]
if self.hass.config.units.name == "imperial":
return round(wind_speed * 2.24, 2)
return round(wind_speed * 3.6, 2)
@property @property
def wind_bearing(self) -> float | str | None: def wind_bearing(self) -> float | str | None:

View File

@ -9,14 +9,14 @@ from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT, ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_SUNNY, ATTR_CONDITION_SUNNY,
ATTR_FORECAST_CONDITION, ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_NATIVE_PRECIPITATION,
ATTR_FORECAST_NATIVE_PRESSURE,
ATTR_FORECAST_NATIVE_TEMP,
ATTR_FORECAST_NATIVE_TEMP_LOW,
ATTR_FORECAST_NATIVE_WIND_SPEED,
ATTR_FORECAST_PRECIPITATION_PROBABILITY, ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_PRESSURE,
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,
) )
from homeassistant.helpers import sun from homeassistant.helpers import sun
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@ -161,14 +161,14 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
ATTR_FORECAST_TIME: dt.utc_from_timestamp( ATTR_FORECAST_TIME: dt.utc_from_timestamp(
entry.reference_time("unix") entry.reference_time("unix")
).isoformat(), ).isoformat(),
ATTR_FORECAST_PRECIPITATION: self._calc_precipitation( ATTR_FORECAST_NATIVE_PRECIPITATION: self._calc_precipitation(
entry.rain, entry.snow entry.rain, entry.snow
), ),
ATTR_FORECAST_PRECIPITATION_PROBABILITY: ( ATTR_FORECAST_PRECIPITATION_PROBABILITY: (
round(entry.precipitation_probability * 100) round(entry.precipitation_probability * 100)
), ),
ATTR_FORECAST_PRESSURE: entry.pressure.get("press"), ATTR_FORECAST_NATIVE_PRESSURE: entry.pressure.get("press"),
ATTR_FORECAST_WIND_SPEED: entry.wind().get("speed"), ATTR_FORECAST_NATIVE_WIND_SPEED: entry.wind().get("speed"),
ATTR_FORECAST_WIND_BEARING: entry.wind().get("deg"), ATTR_FORECAST_WIND_BEARING: entry.wind().get("deg"),
ATTR_FORECAST_CONDITION: self._get_condition( ATTR_FORECAST_CONDITION: self._get_condition(
entry.weather_code, entry.reference_time("unix") entry.weather_code, entry.reference_time("unix")
@ -178,10 +178,16 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
temperature_dict = entry.temperature("celsius") temperature_dict = entry.temperature("celsius")
if "max" in temperature_dict and "min" in temperature_dict: if "max" in temperature_dict and "min" in temperature_dict:
forecast[ATTR_FORECAST_TEMP] = entry.temperature("celsius").get("max") forecast[ATTR_FORECAST_NATIVE_TEMP] = entry.temperature("celsius").get(
forecast[ATTR_FORECAST_TEMP_LOW] = entry.temperature("celsius").get("min") "max"
)
forecast[ATTR_FORECAST_NATIVE_TEMP_LOW] = entry.temperature("celsius").get(
"min"
)
else: else:
forecast[ATTR_FORECAST_TEMP] = entry.temperature("celsius").get("temp") forecast[ATTR_FORECAST_NATIVE_TEMP] = entry.temperature("celsius").get(
"temp"
)
return forecast return forecast