Migrate met_eireann weather to native_* (#74391)

Co-authored-by: avee87 <6134677+avee87@users.noreply.github.com>
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2022-07-04 14:20:47 +02:00 committed by GitHub
parent 14e5001d0c
commit 1c0ece6ac1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 57 deletions

View file

@ -1,6 +1,4 @@
"""Constants for Met Éireann component.""" """Constants for Met Éireann component."""
import logging
from homeassistant.components.weather import ( from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT, ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY, ATTR_CONDITION_CLOUDY,
@ -12,13 +10,13 @@ from homeassistant.components.weather import (
ATTR_CONDITION_SNOWY_RAINY, ATTR_CONDITION_SNOWY_RAINY,
ATTR_CONDITION_SUNNY, ATTR_CONDITION_SUNNY,
ATTR_FORECAST_CONDITION, ATTR_FORECAST_CONDITION,
ATTR_FORECAST_NATIVE_PRESSURE,
ATTR_FORECAST_NATIVE_TEMP,
ATTR_FORECAST_NATIVE_TEMP_LOW,
ATTR_FORECAST_NATIVE_WIND_SPEED,
ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_PRECIPITATION,
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,
DOMAIN as WEATHER_DOMAIN, DOMAIN as WEATHER_DOMAIN,
) )
@ -32,17 +30,15 @@ HOME_LOCATION_NAME = "Home"
ENTITY_ID_SENSOR_FORMAT_HOME = f"{WEATHER_DOMAIN}.met_eireann_{HOME_LOCATION_NAME}" ENTITY_ID_SENSOR_FORMAT_HOME = f"{WEATHER_DOMAIN}.met_eireann_{HOME_LOCATION_NAME}"
_LOGGER = logging.getLogger(".")
FORECAST_MAP = { FORECAST_MAP = {
ATTR_FORECAST_CONDITION: "condition", ATTR_FORECAST_CONDITION: "condition",
ATTR_FORECAST_PRESSURE: "pressure", ATTR_FORECAST_NATIVE_PRESSURE: "pressure",
ATTR_FORECAST_PRECIPITATION: "precipitation", ATTR_FORECAST_PRECIPITATION: "precipitation",
ATTR_FORECAST_TEMP: "temperature", ATTR_FORECAST_NATIVE_TEMP: "temperature",
ATTR_FORECAST_TEMP_LOW: "templow", ATTR_FORECAST_NATIVE_TEMP_LOW: "templow",
ATTR_FORECAST_TIME: "datetime", ATTR_FORECAST_TIME: "datetime",
ATTR_FORECAST_WIND_BEARING: "wind_bearing", ATTR_FORECAST_WIND_BEARING: "wind_bearing",
ATTR_FORECAST_WIND_SPEED: "wind_speed", ATTR_FORECAST_NATIVE_WIND_SPEED: "wind_speed",
} }
CONDITION_MAP = { CONDITION_MAP = {

View file

@ -3,8 +3,6 @@ import logging
from homeassistant.components.weather import ( from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION, ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TIME, ATTR_FORECAST_TIME,
WeatherEntity, WeatherEntity,
) )
@ -13,12 +11,9 @@ from homeassistant.const import (
CONF_LATITUDE, CONF_LATITUDE,
CONF_LONGITUDE, CONF_LONGITUDE,
CONF_NAME, CONF_NAME,
LENGTH_INCHES,
LENGTH_MILLIMETERS, LENGTH_MILLIMETERS,
PRESSURE_HPA, PRESSURE_HPA,
PRESSURE_INHG,
SPEED_METERS_PER_SECOND, SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -27,9 +22,6 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from homeassistant.util.distance import convert as convert_distance
from homeassistant.util.pressure import convert as convert_pressure
from homeassistant.util.speed import convert as convert_speed
from .const import ATTRIBUTION, CONDITION_MAP, DEFAULT_NAME, DOMAIN, FORECAST_MAP from .const import ATTRIBUTION, CONDITION_MAP, DEFAULT_NAME, DOMAIN, FORECAST_MAP
@ -54,12 +46,8 @@ async def async_setup_entry(
coordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities( async_add_entities(
[ [
MetEireannWeather( MetEireannWeather(coordinator, config_entry.data, False),
coordinator, config_entry.data, hass.config.units.is_metric, False MetEireannWeather(coordinator, config_entry.data, True),
),
MetEireannWeather(
coordinator, config_entry.data, hass.config.units.is_metric, True
),
] ]
) )
@ -67,11 +55,15 @@ async def async_setup_entry(
class MetEireannWeather(CoordinatorEntity, WeatherEntity): class MetEireannWeather(CoordinatorEntity, WeatherEntity):
"""Implementation of a Met Éireann weather condition.""" """Implementation of a Met Éireann weather condition."""
def __init__(self, coordinator, config, is_metric, hourly): _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__(self, coordinator, config, hourly):
"""Initialise the platform with a data instance and site.""" """Initialise the platform with a data instance and site."""
super().__init__(coordinator) super().__init__(coordinator)
self._config = config self._config = config
self._is_metric = is_metric
self._hourly = hourly self._hourly = hourly
@property @property
@ -109,23 +101,14 @@ class MetEireannWeather(CoordinatorEntity, WeatherEntity):
) )
@property @property
def temperature(self): def native_temperature(self):
"""Return the temperature.""" """Return the temperature."""
return self.coordinator.data.current_weather_data.get("temperature") return self.coordinator.data.current_weather_data.get("temperature")
@property @property
def temperature_unit(self): def native_pressure(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def pressure(self):
"""Return the pressure.""" """Return the pressure."""
pressure_hpa = self.coordinator.data.current_weather_data.get("pressure") return self.coordinator.data.current_weather_data.get("pressure")
if self._is_metric or pressure_hpa is None:
return pressure_hpa
return round(convert_pressure(pressure_hpa, PRESSURE_HPA, PRESSURE_INHG), 2)
@property @property
def humidity(self): def humidity(self):
@ -133,16 +116,9 @@ class MetEireannWeather(CoordinatorEntity, WeatherEntity):
return self.coordinator.data.current_weather_data.get("humidity") return self.coordinator.data.current_weather_data.get("humidity")
@property @property
def wind_speed(self): def native_wind_speed(self):
"""Return the wind speed.""" """Return the wind speed."""
speed_m_s = self.coordinator.data.current_weather_data.get("wind_speed") return self.coordinator.data.current_weather_data.get("wind_speed")
if self._is_metric or speed_m_s is None:
return speed_m_s
speed_mi_h = convert_speed(
speed_m_s, SPEED_METERS_PER_SECOND, SPEED_MILES_PER_HOUR
)
return int(round(speed_mi_h))
@property @property
def wind_bearing(self): def wind_bearing(self):
@ -161,7 +137,7 @@ class MetEireannWeather(CoordinatorEntity, WeatherEntity):
me_forecast = self.coordinator.data.hourly_forecast me_forecast = self.coordinator.data.hourly_forecast
else: else:
me_forecast = self.coordinator.data.daily_forecast me_forecast = self.coordinator.data.daily_forecast
required_keys = {ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME} required_keys = {"temperature", "datetime"}
ha_forecast = [] ha_forecast = []
@ -171,13 +147,6 @@ class MetEireannWeather(CoordinatorEntity, WeatherEntity):
ha_item = { ha_item = {
k: item[v] for k, v in FORECAST_MAP.items() if item.get(v) is not None k: item[v] for k, v in FORECAST_MAP.items() if item.get(v) is not None
} }
if not self._is_metric and ATTR_FORECAST_PRECIPITATION in ha_item:
precip_inches = convert_distance(
ha_item[ATTR_FORECAST_PRECIPITATION],
LENGTH_MILLIMETERS,
LENGTH_INCHES,
)
ha_item[ATTR_FORECAST_PRECIPITATION] = round(precip_inches, 2)
if ha_item.get(ATTR_FORECAST_CONDITION): if ha_item.get(ATTR_FORECAST_CONDITION):
ha_item[ATTR_FORECAST_CONDITION] = format_condition( ha_item[ATTR_FORECAST_CONDITION] = format_condition(
ha_item[ATTR_FORECAST_CONDITION] ha_item[ATTR_FORECAST_CONDITION]