Add a new weather integration - Met Éireann (#39429)

* Added a new weather integration - Met Éireann

* Fix codespell error

* Update met_eireann to use CoordinatorEntity

* Remove deprecated platform setup

* Fix merge conflict

* Remove unnecessary onboarding/home tracking code

* Use common strings for config flow

* Remove unnecessary code

* Switch to using unique IDs in config flow

* Use constants where possible

* Fix failing tests

* Fix isort errors

* Remove unnecessary DataUpdateCoordinator class

* Add device info

* Explicitly define forecast data

* Disable hourly forecast entity by default

* Update config flow to reflect requested changes

* Cleanup code

* Update entity naming to match other similar components

* Convert forecast time to UTC

* Fix test coverage

* Update test coverage

* Remove elevation conversion

* Update translations for additional clarity

* Remove en-GB translation
This commit is contained in:
Dylan Gore
2021-04-05 23:23:57 +02:00
committed by GitHub
parent c28d4e8e01
commit f3399aa8aa
17 changed files with 696 additions and 0 deletions
@@ -0,0 +1,84 @@
"""The met_eireann component."""
from datetime import timedelta
import logging
import meteireann
from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import homeassistant.util.dt as dt_util
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
UPDATE_INTERVAL = timedelta(minutes=60)
async def async_setup_entry(hass, config_entry):
"""Set up Met Éireann as config entry."""
hass.data.setdefault(DOMAIN, {})
raw_weather_data = meteireann.WeatherData(
async_get_clientsession(hass),
latitude=config_entry.data[CONF_LATITUDE],
longitude=config_entry.data[CONF_LONGITUDE],
altitude=config_entry.data[CONF_ELEVATION],
)
weather_data = MetEireannWeatherData(hass, config_entry.data, raw_weather_data)
async def _async_update_data():
"""Fetch data from Met Éireann."""
try:
return await weather_data.fetch_data()
except Exception as err:
raise UpdateFailed(f"Update failed: {err}") from err
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name=DOMAIN,
update_method=_async_update_data,
update_interval=UPDATE_INTERVAL,
)
await coordinator.async_refresh()
hass.data[DOMAIN][config_entry.entry_id] = coordinator
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "weather")
)
return True
async def async_unload_entry(hass, config_entry):
"""Unload a config entry."""
await hass.config_entries.async_forward_entry_unload(config_entry, "weather")
hass.data[DOMAIN].pop(config_entry.entry_id)
return True
class MetEireannWeatherData:
"""Keep data for Met Éireann weather entities."""
def __init__(self, hass, config, weather_data):
"""Initialise the weather entity data."""
self.hass = hass
self._config = config
self._weather_data = weather_data
self.current_weather_data = {}
self.daily_forecast = None
self.hourly_forecast = None
async def fetch_data(self):
"""Fetch data from API - (current weather and forecast)."""
await self._weather_data.fetching_data()
self.current_weather_data = self._weather_data.get_current_weather()
time_zone = dt_util.DEFAULT_TIME_ZONE
self.daily_forecast = self._weather_data.get_forecast(time_zone, False)
self.hourly_forecast = self._weather_data.get_forecast(time_zone, True)
return self
@@ -0,0 +1,48 @@
"""Config flow to configure Met Éireann component."""
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
import homeassistant.helpers.config_validation as cv
# pylint:disable=unused-import
from .const import DOMAIN, HOME_LOCATION_NAME
class MetEireannFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Met Eireann component."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
errors = {}
if user_input is not None:
# Check if an identical entity is already configured
await self.async_set_unique_id(
f"{user_input.get(CONF_LATITUDE)},{user_input.get(CONF_LONGITUDE)}"
)
self._abort_if_unique_id_configured()
else:
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_NAME, default=HOME_LOCATION_NAME): str,
vol.Required(
CONF_LATITUDE, default=self.hass.config.latitude
): cv.latitude,
vol.Required(
CONF_LONGITUDE, default=self.hass.config.longitude
): cv.longitude,
vol.Required(
CONF_ELEVATION, default=self.hass.config.elevation
): int,
}
),
errors=errors,
)
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
@@ -0,0 +1,121 @@
"""Constants for Met Éireann component."""
import logging
from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY,
ATTR_CONDITION_FOG,
ATTR_CONDITION_LIGHTNING_RAINY,
ATTR_CONDITION_PARTLYCLOUDY,
ATTR_CONDITION_RAINY,
ATTR_CONDITION_SNOWY,
ATTR_CONDITION_SNOWY_RAINY,
ATTR_CONDITION_SUNNY,
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_PRESSURE,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
DOMAIN as WEATHER_DOMAIN,
)
ATTRIBUTION = "Data provided by Met Éireann"
DEFAULT_NAME = "Met Éireann"
DOMAIN = "met_eireann"
HOME_LOCATION_NAME = "Home"
ENTITY_ID_SENSOR_FORMAT_HOME = f"{WEATHER_DOMAIN}.met_eireann_{HOME_LOCATION_NAME}"
_LOGGER = logging.getLogger(".")
FORECAST_MAP = {
ATTR_FORECAST_CONDITION: "condition",
ATTR_FORECAST_PRESSURE: "pressure",
ATTR_FORECAST_PRECIPITATION: "precipitation",
ATTR_FORECAST_TEMP: "temperature",
ATTR_FORECAST_TEMP_LOW: "templow",
ATTR_FORECAST_TIME: "datetime",
ATTR_FORECAST_WIND_BEARING: "wind_bearing",
ATTR_FORECAST_WIND_SPEED: "wind_speed",
}
CONDITION_MAP = {
ATTR_CONDITION_CLEAR_NIGHT: ["Dark_Sun"],
ATTR_CONDITION_CLOUDY: ["Cloud"],
ATTR_CONDITION_FOG: ["Fog"],
ATTR_CONDITION_LIGHTNING_RAINY: [
"LightRainThunderSun",
"LightRainThunderSun",
"RainThunder",
"SnowThunder",
"SleetSunThunder",
"Dark_SleetSunThunder",
"SnowSunThunder",
"Dark_SnowSunThunder",
"LightRainThunder",
"SleetThunder",
"DrizzleThunderSun",
"Dark_DrizzleThunderSun",
"RainThunderSun",
"Dark_RainThunderSun",
"LightSleetThunderSun",
"Dark_LightSleetThunderSun",
"HeavySleetThunderSun",
"Dark_HeavySleetThunderSun",
"LightSnowThunderSun",
"Dark_LightSnowThunderSun",
"HeavySnowThunderSun",
"Dark_HeavySnowThunderSun",
"DrizzleThunder",
"LightSleetThunder",
"HeavySleetThunder",
"LightSnowThunder",
"HeavySnowThunder",
],
ATTR_CONDITION_PARTLYCLOUDY: [
"LightCloud",
"Dark_LightCloud",
"PartlyCloud",
"Dark_PartlyCloud",
],
ATTR_CONDITION_RAINY: [
"LightRainSun",
"Dark_LightRainSun",
"LightRain",
"Rain",
"DrizzleSun",
"Dark_DrizzleSun",
"RainSun",
"Dark_RainSun",
"Drizzle",
],
ATTR_CONDITION_SNOWY: [
"SnowSun",
"Dark_SnowSun",
"Snow",
"LightSnowSun",
"Dark_LightSnowSun",
"HeavySnowSun",
"Dark_HeavySnowSun",
"LightSnow",
"HeavySnow",
],
ATTR_CONDITION_SNOWY_RAINY: [
"SleetSun",
"Dark_SleetSun",
"Sleet",
"LightSleetSun",
"Dark_LightSleetSun",
"HeavySleetSun",
"Dark_HeavySleetSun",
"LightSleet",
"HeavySleet",
],
ATTR_CONDITION_SUNNY: "Sun",
}
@@ -0,0 +1,8 @@
{
"domain": "met_eireann",
"name": "Met Éireann",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/met_eireann",
"requirements": ["pyMetEireann==0.2"],
"codeowners": ["@DylanGore"]
}
@@ -0,0 +1,17 @@
{
"config": {
"step": {
"user": {
"title": "[%key:common::config_flow::data::location%]",
"description": "Enter your location to use weather data from the Met Éireann Public Weather Forecast API",
"data": {
"name": "[%key:common::config_flow::data::name%]",
"latitude": "[%key:common::config_flow::data::latitude%]",
"longitude": "[%key:common::config_flow::data::longitude%]",
"elevation": "[%key:common::config_flow::data::elevation%]"
}
}
},
"error": { "already_configured": "[%key:common::config_flow::abort::already_configured_service%]" }
}
}
@@ -0,0 +1,23 @@
{
"config": {
"step": {
"user": {
"title": "Met Éireann",
"description": "Enter your location to use weather data from the Met Éireann Public Weather Forecast API",
"data": {
"name": "Name",
"latitude": "Latitude",
"longitude": "Longitude",
"elevation": "Elevation (in meters)"
}
}
},
"error": {
"name_exists": "Location already exists"
},
"abort": {
"already_configured": "Location is already configured",
"unknown": "Unexpected error"
}
}
}
@@ -0,0 +1,191 @@
"""Support for Met Éireann weather service."""
import logging
from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TIME,
WeatherEntity,
)
from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
LENGTH_INCHES,
LENGTH_METERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
PRESSURE_HPA,
PRESSURE_INHG,
TEMP_CELSIUS,
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity
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 .const import ATTRIBUTION, CONDITION_MAP, DEFAULT_NAME, DOMAIN, FORECAST_MAP
_LOGGER = logging.getLogger(__name__)
def format_condition(condition: str):
"""Map the conditions provided by the weather API to those supported by the frontend."""
if condition is not None:
for key, value in CONDITION_MAP.items():
if condition in value:
return key
return condition
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Add a weather entity from a config_entry."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities(
[
MetEireannWeather(
coordinator, config_entry.data, hass.config.units.is_metric, False
),
MetEireannWeather(
coordinator, config_entry.data, hass.config.units.is_metric, True
),
]
)
class MetEireannWeather(CoordinatorEntity, WeatherEntity):
"""Implementation of a Met Éireann weather condition."""
def __init__(self, coordinator, config, is_metric, hourly):
"""Initialise the platform with a data instance and site."""
super().__init__(coordinator)
self._config = config
self._is_metric = is_metric
self._hourly = hourly
@property
def unique_id(self):
"""Return unique ID."""
name_appendix = ""
if self._hourly:
name_appendix = "-hourly"
return f"{self._config[CONF_LATITUDE]}-{self._config[CONF_LONGITUDE]}{name_appendix}"
@property
def name(self):
"""Return the name of the sensor."""
name = self._config.get(CONF_NAME)
name_appendix = ""
if self._hourly:
name_appendix = " Hourly"
if name is not None:
return f"{name}{name_appendix}"
return f"{DEFAULT_NAME}{name_appendix}"
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return not self._hourly
@property
def condition(self):
"""Return the current condition."""
return format_condition(
self.coordinator.data.current_weather_data.get("condition")
)
@property
def temperature(self):
"""Return the temperature."""
return self.coordinator.data.current_weather_data.get("temperature")
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def pressure(self):
"""Return the pressure."""
pressure_hpa = 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
def humidity(self):
"""Return the humidity."""
return self.coordinator.data.current_weather_data.get("humidity")
@property
def wind_speed(self):
"""Return the wind speed."""
speed_m_s = 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_s = convert_distance(speed_m_s, LENGTH_METERS, LENGTH_MILES)
speed_mi_h = speed_mi_s / 3600.0
return int(round(speed_mi_h))
@property
def wind_bearing(self):
"""Return the wind direction."""
return self.coordinator.data.current_weather_data.get("wind_bearing")
@property
def attribution(self):
"""Return the attribution."""
return ATTRIBUTION
@property
def forecast(self):
"""Return the forecast array."""
if self._hourly:
me_forecast = self.coordinator.data.hourly_forecast
else:
me_forecast = self.coordinator.data.daily_forecast
required_keys = {ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME}
ha_forecast = []
for item in me_forecast:
if not set(item).issuperset(required_keys):
continue
ha_item = {
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):
ha_item[ATTR_FORECAST_CONDITION] = format_condition(
ha_item[ATTR_FORECAST_CONDITION]
)
# Convert timestamp to UTC
if ha_item.get(ATTR_FORECAST_TIME):
ha_item[ATTR_FORECAST_TIME] = dt_util.as_utc(
ha_item.get(ATTR_FORECAST_TIME)
).isoformat()
ha_forecast.append(ha_item)
return ha_forecast
@property
def device_info(self):
"""Device info."""
return {
"identifiers": {(DOMAIN,)},
"manufacturer": "Met Éireann",
"model": "Forecast",
"default_name": "Forecast",
"entry_type": "service",
}
+1
View File
@@ -139,6 +139,7 @@ FLOWS = [
"mazda",
"melcloud",
"met",
"met_eireann",
"meteo_france",
"metoffice",
"mikrotik",