* new weather component for meteofrance * linting * upgrade meteofrance package version * Update .coveragerc * Remove updates to the weather component architecture * Rewrite Météo-France as a component * Update .coveragerc * Update requirements_all.txt * remove Weather Card option * Update conf name Changing conf name to something more universal for worldwide weather forecast (postal code was only relevent for France) * Update meteofrance pypi package * fix line too long * Update requirements_all.txt * prevent from calling an API endpoint if not in monitored conditions * fix stale url and remove blank line * Insure that all cities are unique * rename CONF_ATTRIBUTION * Updating data from component setup * fix missing extra lines
89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
"""
|
|
Support for Meteo France raining forecast.
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
https://home-assistant.io/components/sensor.meteo_france/
|
|
"""
|
|
import logging
|
|
|
|
|
|
from homeassistant.components.meteo_france import (SENSOR_TYPES,
|
|
DATA_METEO_FRANCE,
|
|
CONF_CITY,
|
|
ATTRIBUTION)
|
|
from homeassistant.const import (
|
|
CONF_MONITORED_CONDITIONS, ATTR_ATTRIBUTION)
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
STATE_ATTR_FORECAST = '1h rain forecast'
|
|
|
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
|
"""Set up the Meteo-France sensor."""
|
|
if discovery_info is None:
|
|
return
|
|
|
|
city = discovery_info[CONF_CITY]
|
|
monitored_conditions = discovery_info[CONF_MONITORED_CONDITIONS]
|
|
|
|
client = hass.data[DATA_METEO_FRANCE][city]
|
|
|
|
add_entities([MeteoFranceSensor(variable, client)
|
|
for variable in monitored_conditions],
|
|
True)
|
|
|
|
|
|
class MeteoFranceSensor(Entity):
|
|
"""Representation of a Sensor."""
|
|
|
|
def __init__(self, condition, client):
|
|
"""Initialize the sensor."""
|
|
self._condition = condition
|
|
self._client = client
|
|
self._state = None
|
|
self._data = {}
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the name of the sensor."""
|
|
return "{} {}".format(self._data["name"],
|
|
SENSOR_TYPES[self._condition][0])
|
|
|
|
@property
|
|
def state(self):
|
|
"""Return the state of the sensor."""
|
|
return self._state
|
|
|
|
@property
|
|
def device_state_attributes(self):
|
|
"""Return the state attributes of the sensor."""
|
|
if self._condition == 'next_rain' and "rain_forecast" in self._data:
|
|
return {
|
|
**{
|
|
STATE_ATTR_FORECAST: self._data["rain_forecast"],
|
|
},
|
|
** self._data["next_rain_intervals"],
|
|
**{
|
|
ATTR_ATTRIBUTION: ATTRIBUTION
|
|
}
|
|
}
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
@property
|
|
def unit_of_measurement(self):
|
|
"""Return the unit of measurement."""
|
|
return SENSOR_TYPES[self._condition][1]
|
|
|
|
def update(self):
|
|
"""Fetch new state data for the sensor."""
|
|
try:
|
|
self._client.update()
|
|
self._data = self._client.get_data()
|
|
self._state = self._data[self._condition]
|
|
except KeyError:
|
|
_LOGGER.error("No condition `%s` for location `%s`",
|
|
self._condition, self._data["name"])
|
|
self._state = None
|