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:
parent
c28d4e8e01
commit
f3399aa8aa
17 changed files with 696 additions and 0 deletions
|
@ -577,6 +577,8 @@ omit =
|
|||
homeassistant/components/melcloud/water_heater.py
|
||||
homeassistant/components/message_bird/notify.py
|
||||
homeassistant/components/met/weather.py
|
||||
homeassistant/components/met_eireann/__init__.py
|
||||
homeassistant/components/met_eireann/weather.py
|
||||
homeassistant/components/meteo_france/__init__.py
|
||||
homeassistant/components/meteo_france/const.py
|
||||
homeassistant/components/meteo_france/sensor.py
|
||||
|
|
|
@ -278,6 +278,7 @@ homeassistant/components/mediaroom/* @dgomes
|
|||
homeassistant/components/melcloud/* @vilppuvuorinen
|
||||
homeassistant/components/melissa/* @kennedyshead
|
||||
homeassistant/components/met/* @danielhiversen @thimic
|
||||
homeassistant/components/met_eireann/* @DylanGore
|
||||
homeassistant/components/meteo_france/* @hacf-fr @oncleben31 @Quentame
|
||||
homeassistant/components/meteoalarm/* @rolfberkenbosch
|
||||
homeassistant/components/metoffice/* @MrHarcombe
|
||||
|
|
84
homeassistant/components/met_eireann/__init__.py
Normal file
84
homeassistant/components/met_eireann/__init__.py
Normal file
|
@ -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
|
48
homeassistant/components/met_eireann/config_flow.py
Normal file
48
homeassistant/components/met_eireann/config_flow.py
Normal file
|
@ -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)
|
121
homeassistant/components/met_eireann/const.py
Normal file
121
homeassistant/components/met_eireann/const.py
Normal file
|
@ -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",
|
||||
}
|
8
homeassistant/components/met_eireann/manifest.json
Normal file
8
homeassistant/components/met_eireann/manifest.json
Normal file
|
@ -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"]
|
||||
}
|
17
homeassistant/components/met_eireann/strings.json
Normal file
17
homeassistant/components/met_eireann/strings.json
Normal file
|
@ -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%]" }
|
||||
}
|
||||
}
|
23
homeassistant/components/met_eireann/translations/en.json
Normal file
23
homeassistant/components/met_eireann/translations/en.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
191
homeassistant/components/met_eireann/weather.py
Normal file
191
homeassistant/components/met_eireann/weather.py
Normal file
|
@ -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",
|
||||
}
|
|
@ -139,6 +139,7 @@ FLOWS = [
|
|||
"mazda",
|
||||
"melcloud",
|
||||
"met",
|
||||
"met_eireann",
|
||||
"meteo_france",
|
||||
"metoffice",
|
||||
"mikrotik",
|
||||
|
|
|
@ -1227,6 +1227,9 @@ pyControl4==0.0.6
|
|||
# homeassistant.components.tplink
|
||||
pyHS100==0.3.5.2
|
||||
|
||||
# homeassistant.components.met_eireann
|
||||
pyMetEireann==0.2
|
||||
|
||||
# homeassistant.components.met
|
||||
# homeassistant.components.norway_air
|
||||
pyMetno==0.8.1
|
||||
|
|
|
@ -652,6 +652,9 @@ pyControl4==0.0.6
|
|||
# homeassistant.components.tplink
|
||||
pyHS100==0.3.5.2
|
||||
|
||||
# homeassistant.components.met_eireann
|
||||
pyMetEireann==0.2
|
||||
|
||||
# homeassistant.components.met
|
||||
# homeassistant.components.norway_air
|
||||
pyMetno==0.8.1
|
||||
|
|
27
tests/components/met_eireann/__init__.py
Normal file
27
tests/components/met_eireann/__init__.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
"""Tests for Met Éireann."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.met_eireann.const import DOMAIN
|
||||
from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def init_integration(hass) -> MockConfigEntry:
|
||||
"""Set up the Met Éireann integration in Home Assistant."""
|
||||
entry_data = {
|
||||
CONF_NAME: "test",
|
||||
CONF_LATITUDE: 0,
|
||||
CONF_LONGITUDE: 0,
|
||||
CONF_ELEVATION: 0,
|
||||
}
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=entry_data)
|
||||
with patch(
|
||||
"homeassistant.components.met_eireann.meteireann.WeatherData.fetching_data",
|
||||
return_value=True,
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return entry
|
22
tests/components/met_eireann/conftest.py
Normal file
22
tests/components/met_eireann/conftest.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
"""Fixtures for Met Éireann weather testing."""
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_weather():
|
||||
"""Mock weather data."""
|
||||
with patch("meteireann.WeatherData") as mock_data:
|
||||
mock_data = mock_data.return_value
|
||||
mock_data.fetching_data = AsyncMock(return_value=True)
|
||||
mock_data.get_current_weather.return_value = {
|
||||
"condition": "Cloud",
|
||||
"temperature": 15,
|
||||
"pressure": 100,
|
||||
"humidity": 50,
|
||||
"wind_speed": 10,
|
||||
"wind_bearing": "NE",
|
||||
}
|
||||
mock_data.get_forecast.return_value = {}
|
||||
yield mock_data
|
95
tests/components/met_eireann/test_config_flow.py
Normal file
95
tests/components/met_eireann/test_config_flow.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
"""Tests for Met Éireann config flow."""
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow
|
||||
from homeassistant.components.met_eireann.const import DOMAIN, HOME_LOCATION_NAME
|
||||
from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE
|
||||
|
||||
|
||||
@pytest.fixture(name="met_eireann_setup", autouse=True)
|
||||
def met_setup_fixture():
|
||||
"""Patch Met Éireann setup entry."""
|
||||
with patch(
|
||||
"homeassistant.components.met_eireann.async_setup_entry", return_value=True
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
async def test_show_config_form(hass):
|
||||
"""Test show configuration form."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == config_entries.SOURCE_USER
|
||||
|
||||
|
||||
async def test_flow_with_home_location(hass):
|
||||
"""Test config flow.
|
||||
|
||||
Test the flow when a default location is configured.
|
||||
Then it should return a form with default values.
|
||||
"""
|
||||
hass.config.latitude = 1
|
||||
hass.config.longitude = 2
|
||||
hass.config.elevation = 3
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == config_entries.SOURCE_USER
|
||||
|
||||
default_data = result["data_schema"]({})
|
||||
assert default_data["name"] == HOME_LOCATION_NAME
|
||||
assert default_data["latitude"] == 1
|
||||
assert default_data["longitude"] == 2
|
||||
assert default_data["elevation"] == 3
|
||||
|
||||
|
||||
async def test_create_entry(hass):
|
||||
"""Test create entry from user input."""
|
||||
test_data = {
|
||||
"name": "test",
|
||||
CONF_LONGITUDE: 0,
|
||||
CONF_LATITUDE: 0,
|
||||
CONF_ELEVATION: 0,
|
||||
}
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=test_data
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == test_data.get("name")
|
||||
assert result["data"] == test_data
|
||||
|
||||
|
||||
async def test_flow_entry_already_exists(hass):
|
||||
"""Test user input for config_entry that already exists.
|
||||
|
||||
Test to ensure the config form does not allow duplicate entries.
|
||||
"""
|
||||
test_data = {
|
||||
"name": "test",
|
||||
CONF_LONGITUDE: 0,
|
||||
CONF_LATITUDE: 0,
|
||||
CONF_ELEVATION: 0,
|
||||
}
|
||||
|
||||
# Create the first entry and assert that it is created successfully
|
||||
result1 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=test_data
|
||||
)
|
||||
assert result1["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
|
||||
# Create the second entry and assert that it is aborted
|
||||
result2 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=test_data
|
||||
)
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result2["reason"] == "already_configured"
|
19
tests/components/met_eireann/test_init.py
Normal file
19
tests/components/met_eireann/test_init.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
"""Test the Met Éireann integration init."""
|
||||
from homeassistant.components.met_eireann.const import DOMAIN
|
||||
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
|
||||
|
||||
from . import init_integration
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test successful unload of entry."""
|
||||
entry = await init_integration(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state == ENTRY_STATE_NOT_LOADED
|
||||
assert not hass.data.get(DOMAIN)
|
31
tests/components/met_eireann/test_weather.py
Normal file
31
tests/components/met_eireann/test_weather.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
"""Test Met Éireann weather entity."""
|
||||
|
||||
from homeassistant.components.met_eireann.const import DOMAIN
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_weather(hass, mock_weather):
|
||||
"""Test weather entity."""
|
||||
# Create a mock configuration for testing
|
||||
mock_data = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"name": "Somewhere", "latitude": 10, "longitude": 20, "elevation": 0},
|
||||
)
|
||||
mock_data.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(mock_data.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids("weather")) == 1
|
||||
assert len(mock_weather.mock_calls) == 4
|
||||
|
||||
# Test we do not track config
|
||||
await hass.config.async_update(latitude=10, longitude=20)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_weather.mock_calls) == 4
|
||||
|
||||
entry = hass.config_entries.async_entries()[0]
|
||||
await hass.config_entries.async_remove(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids("weather")) == 0
|
Loading…
Add table
Reference in a new issue