Add weather alert sensor to meteo france component (#23128)
* Quick & Dirty weather alert integration * Add attributes in weather alert sensor. * MeteoFranceUpdate returns dept to init the alert watcher * add rain forecast to weather attribute * Add checks when no weather alert data are available * Improve date and state when online source is unreachable * update to take into account new API of vigilancemeteo 3.0.0 * Clean local patchs and put requirements in manfiest.json * Use only one proxy for weather alerts to avoid too much HTTP requests * linting and comments style corrections * Add error catching and debug logging * Correction following PR checklist * Add code owners * Update requirements_all.txt * Comment style * Update CODEOWNERS after rebaseline with dev branch * update requirements_all.txtpull/24640/head
parent
55997c74b0
commit
6ea92f86a5
|
@ -159,7 +159,7 @@ homeassistant/components/mcp23017/* @jardiamj
|
||||||
homeassistant/components/mediaroom/* @dgomes
|
homeassistant/components/mediaroom/* @dgomes
|
||||||
homeassistant/components/melissa/* @kennedyshead
|
homeassistant/components/melissa/* @kennedyshead
|
||||||
homeassistant/components/met/* @danielhiversen
|
homeassistant/components/met/* @danielhiversen
|
||||||
homeassistant/components/meteo_france/* @victorcerutti
|
homeassistant/components/meteo_france/* @victorcerutti @oncleben31
|
||||||
homeassistant/components/meteoalarm/* @rolfberkenbosch
|
homeassistant/components/meteoalarm/* @rolfberkenbosch
|
||||||
homeassistant/components/miflora/* @danielhiversen @ChristianKuehnel
|
homeassistant/components/miflora/* @danielhiversen @ChristianKuehnel
|
||||||
homeassistant/components/mill/* @danielhiversen
|
homeassistant/components/mill/* @danielhiversen
|
||||||
|
|
|
@ -31,6 +31,7 @@ SENSOR_TYPES = {
|
||||||
'next_rain': ['Next rain', 'min'],
|
'next_rain': ['Next rain', 'min'],
|
||||||
'temperature': ['Temperature', TEMP_CELSIUS],
|
'temperature': ['Temperature', TEMP_CELSIUS],
|
||||||
'uv': ['UV', None],
|
'uv': ['UV', None],
|
||||||
|
'weather_alert': ['Weather Alert', None],
|
||||||
}
|
}
|
||||||
|
|
||||||
CONDITION_CLASSES = {
|
CONDITION_CLASSES = {
|
||||||
|
@ -77,6 +78,28 @@ def setup(hass, config):
|
||||||
"""Set up the Meteo-France component."""
|
"""Set up the Meteo-France component."""
|
||||||
hass.data[DATA_METEO_FRANCE] = {}
|
hass.data[DATA_METEO_FRANCE] = {}
|
||||||
|
|
||||||
|
# Check if at least weather alert have to be monitored for one location.
|
||||||
|
need_weather_alert_watcher = False
|
||||||
|
for location in config[DOMAIN]:
|
||||||
|
if CONF_MONITORED_CONDITIONS in location \
|
||||||
|
and 'weather_alert' in location[CONF_MONITORED_CONDITIONS]:
|
||||||
|
need_weather_alert_watcher = True
|
||||||
|
|
||||||
|
# If weather alert monitoring is expected initiate a client to be used by
|
||||||
|
# all weather_alert entities.
|
||||||
|
if need_weather_alert_watcher:
|
||||||
|
from vigilancemeteo import VigilanceMeteoFranceProxy, \
|
||||||
|
VigilanceMeteoError
|
||||||
|
|
||||||
|
weather_alert_client = VigilanceMeteoFranceProxy()
|
||||||
|
try:
|
||||||
|
weather_alert_client.update_data()
|
||||||
|
except VigilanceMeteoError as exp:
|
||||||
|
_LOGGER.error(exp)
|
||||||
|
else:
|
||||||
|
weather_alert_client = None
|
||||||
|
hass.data[DATA_METEO_FRANCE]['weather_alert_client'] = weather_alert_client
|
||||||
|
|
||||||
for location in config[DOMAIN]:
|
for location in config[DOMAIN]:
|
||||||
|
|
||||||
city = location[CONF_CITY]
|
city = location[CONF_CITY]
|
||||||
|
@ -98,6 +121,7 @@ def setup(hass, config):
|
||||||
|
|
||||||
if CONF_MONITORED_CONDITIONS in location:
|
if CONF_MONITORED_CONDITIONS in location:
|
||||||
monitored_conditions = location[CONF_MONITORED_CONDITIONS]
|
monitored_conditions = location[CONF_MONITORED_CONDITIONS]
|
||||||
|
_LOGGER.debug("meteo_france sensor platfrom loaded for %s", city)
|
||||||
load_platform(
|
load_platform(
|
||||||
hass, 'sensor', DOMAIN, {
|
hass, 'sensor', DOMAIN, {
|
||||||
CONF_CITY: city,
|
CONF_CITY: city,
|
||||||
|
|
|
@ -3,10 +3,12 @@
|
||||||
"name": "Meteo france",
|
"name": "Meteo france",
|
||||||
"documentation": "https://www.home-assistant.io/components/meteo_france",
|
"documentation": "https://www.home-assistant.io/components/meteo_france",
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"meteofrance==0.3.7"
|
"meteofrance==0.3.7",
|
||||||
|
"vigilancemeteo==3.0.0"
|
||||||
],
|
],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": [
|
"codeowners": [
|
||||||
"@victorcerutti"
|
"@victorcerutti",
|
||||||
|
"@oncleben31"
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -9,6 +9,7 @@ from . import ATTRIBUTION, CONF_CITY, DATA_METEO_FRANCE, SENSOR_TYPES
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
STATE_ATTR_FORECAST = '1h rain forecast'
|
STATE_ATTR_FORECAST = '1h rain forecast'
|
||||||
|
STATE_ATTR_BULLETIN_TIME = 'Bulletin date'
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
|
@ -19,18 +20,44 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
city = discovery_info[CONF_CITY]
|
city = discovery_info[CONF_CITY]
|
||||||
monitored_conditions = discovery_info[CONF_MONITORED_CONDITIONS]
|
monitored_conditions = discovery_info[CONF_MONITORED_CONDITIONS]
|
||||||
client = hass.data[DATA_METEO_FRANCE][city]
|
client = hass.data[DATA_METEO_FRANCE][city]
|
||||||
|
weather_alert_client = hass.data[DATA_METEO_FRANCE]['weather_alert_client']
|
||||||
|
|
||||||
add_entities([MeteoFranceSensor(variable, client)
|
from vigilancemeteo import DepartmentWeatherAlert
|
||||||
|
|
||||||
|
alert_watcher = None
|
||||||
|
if 'weather_alert' in monitored_conditions:
|
||||||
|
datas = hass.data[DATA_METEO_FRANCE][city].get_data()
|
||||||
|
# Check if a department code is available for this city.
|
||||||
|
if "dept" in datas:
|
||||||
|
try:
|
||||||
|
# If yes create the watcher DepartmentWeatherAlert object.
|
||||||
|
alert_watcher = DepartmentWeatherAlert(datas["dept"],
|
||||||
|
weather_alert_client)
|
||||||
|
except ValueError as exp:
|
||||||
|
_LOGGER.error(exp)
|
||||||
|
alert_watcher = None
|
||||||
|
else:
|
||||||
|
_LOGGER.info("weather alert watcher added for %s"
|
||||||
|
"in department %s",
|
||||||
|
city, datas["dept"])
|
||||||
|
else:
|
||||||
|
_LOGGER.warning("No dept key found for '%s'. So weather alert "
|
||||||
|
"information won't be available", city)
|
||||||
|
# Exit and don't create the sensor if no department code available.
|
||||||
|
return
|
||||||
|
|
||||||
|
add_entities([MeteoFranceSensor(variable, client, alert_watcher)
|
||||||
for variable in monitored_conditions], True)
|
for variable in monitored_conditions], True)
|
||||||
|
|
||||||
|
|
||||||
class MeteoFranceSensor(Entity):
|
class MeteoFranceSensor(Entity):
|
||||||
"""Representation of a Meteo-France sensor."""
|
"""Representation of a Meteo-France sensor."""
|
||||||
|
|
||||||
def __init__(self, condition, client):
|
def __init__(self, condition, client, alert_watcher):
|
||||||
"""Initialize the Meteo-France sensor."""
|
"""Initialize the Meteo-France sensor."""
|
||||||
self._condition = condition
|
self._condition = condition
|
||||||
self._client = client
|
self._client = client
|
||||||
|
self._alert_watcher = alert_watcher
|
||||||
self._state = None
|
self._state = None
|
||||||
self._data = {}
|
self._data = {}
|
||||||
|
|
||||||
|
@ -48,12 +75,25 @@ class MeteoFranceSensor(Entity):
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes of the sensor."""
|
"""Return the state attributes of the sensor."""
|
||||||
|
# Attributes for next_rain sensor.
|
||||||
if self._condition == 'next_rain' and 'rain_forecast' in self._data:
|
if self._condition == 'next_rain' and 'rain_forecast' in self._data:
|
||||||
return {
|
return {
|
||||||
**{STATE_ATTR_FORECAST: self._data['rain_forecast']},
|
**{STATE_ATTR_FORECAST: self._data['rain_forecast']},
|
||||||
** self._data['next_rain_intervals'],
|
** self._data['next_rain_intervals'],
|
||||||
**{ATTR_ATTRIBUTION: ATTRIBUTION}
|
**{ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Attributes for weather_alert sensor.
|
||||||
|
if self._condition == 'weather_alert' \
|
||||||
|
and self._alert_watcher is not None:
|
||||||
|
return {
|
||||||
|
**{STATE_ATTR_BULLETIN_TIME:
|
||||||
|
self._alert_watcher.bulletin_date},
|
||||||
|
** self._alert_watcher.alerts_list,
|
||||||
|
ATTR_ATTRIBUTION: ATTRIBUTION
|
||||||
|
}
|
||||||
|
|
||||||
|
# Attributes for all other sensors.
|
||||||
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -66,6 +106,18 @@ class MeteoFranceSensor(Entity):
|
||||||
try:
|
try:
|
||||||
self._client.update()
|
self._client.update()
|
||||||
self._data = self._client.get_data()
|
self._data = self._client.get_data()
|
||||||
|
|
||||||
|
if self._condition == 'weather_alert':
|
||||||
|
if self._alert_watcher is not None:
|
||||||
|
self._alert_watcher.update_department_status()
|
||||||
|
self._state = self._alert_watcher.department_color
|
||||||
|
_LOGGER.debug("weather alert watcher for %s updated. Proxy"
|
||||||
|
" have the status: %s", self._data['name'],
|
||||||
|
self._alert_watcher.proxy.status)
|
||||||
|
else:
|
||||||
|
_LOGGER.warning("No weather alert data for location %s",
|
||||||
|
self._data['name'])
|
||||||
|
else:
|
||||||
self._state = self._data[self._condition]
|
self._state = self._data[self._condition]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
_LOGGER.error("No condition %s for location %s",
|
_LOGGER.error("No condition %s for location %s",
|
||||||
|
|
|
@ -102,3 +102,11 @@ class MeteoFranceWeather(WeatherEntity):
|
||||||
if condition in value:
|
if condition in value:
|
||||||
return key
|
return key
|
||||||
return condition
|
return condition
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_state_attributes(self):
|
||||||
|
"""Return the state attributes."""
|
||||||
|
data = dict()
|
||||||
|
if self._data and "next_rain" in self._data:
|
||||||
|
data["next_rain"] = self._data["next_rain"]
|
||||||
|
return data
|
||||||
|
|
|
@ -1820,6 +1820,9 @@ uvcclient==0.11.0
|
||||||
# homeassistant.components.venstar
|
# homeassistant.components.venstar
|
||||||
venstarcolortouch==0.7
|
venstarcolortouch==0.7
|
||||||
|
|
||||||
|
# homeassistant.components.meteo_france
|
||||||
|
vigilancemeteo==3.0.0
|
||||||
|
|
||||||
# homeassistant.components.volkszaehler
|
# homeassistant.components.volkszaehler
|
||||||
volkszaehler==0.1.2
|
volkszaehler==0.1.2
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue