2019-02-15 09:57:47 +00:00
|
|
|
"""Support for Meteo-France weather service."""
|
2019-02-14 13:40:27 +00:00
|
|
|
from datetime import datetime, timedelta
|
2019-02-14 15:42:03 +00:00
|
|
|
import logging
|
2019-02-14 13:40:27 +00:00
|
|
|
|
|
|
|
from homeassistant.components.weather import (
|
2019-02-14 15:42:03 +00:00
|
|
|
ATTR_FORECAST_CONDITION, ATTR_FORECAST_TEMP, ATTR_FORECAST_TEMP_LOW,
|
|
|
|
ATTR_FORECAST_TIME, WeatherEntity)
|
2019-02-14 13:40:27 +00:00
|
|
|
from homeassistant.const import TEMP_CELSIUS
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import ATTRIBUTION, CONDITION_CLASSES, CONF_CITY, DATA_METEO_FRANCE
|
|
|
|
|
2019-02-14 13:40:27 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
|
|
|
"""Set up the Meteo-France weather platform."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
city = discovery_info[CONF_CITY]
|
|
|
|
client = hass.data[DATA_METEO_FRANCE][city]
|
|
|
|
|
|
|
|
add_entities([MeteoFranceWeather(client)], True)
|
|
|
|
|
|
|
|
|
|
|
|
class MeteoFranceWeather(WeatherEntity):
|
|
|
|
"""Representation of a weather condition."""
|
|
|
|
|
|
|
|
def __init__(self, client):
|
|
|
|
"""Initialise the platform with a data instance and station name."""
|
|
|
|
self._client = client
|
|
|
|
self._data = {}
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update current conditions."""
|
|
|
|
self._client.update()
|
|
|
|
self._data = self._client.get_data()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-02-15 09:57:47 +00:00
|
|
|
return self._data['name']
|
2019-02-14 13:40:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def condition(self):
|
|
|
|
"""Return the current condition."""
|
2019-02-15 09:57:47 +00:00
|
|
|
return self.format_condition(self._data['weather'])
|
2019-02-14 13:40:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature(self):
|
2019-02-15 09:57:47 +00:00
|
|
|
"""Return the temperature."""
|
|
|
|
return self._data['temperature']
|
2019-02-14 13:40:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def humidity(self):
|
2019-02-15 09:57:47 +00:00
|
|
|
"""Return the humidity."""
|
2019-02-14 13:40:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature_unit(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return TEMP_CELSIUS
|
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_speed(self):
|
|
|
|
"""Return the wind speed."""
|
2019-02-15 09:57:47 +00:00
|
|
|
return self._data['wind_speed']
|
2019-02-14 13:40:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_bearing(self):
|
|
|
|
"""Return the wind bearing."""
|
2019-02-15 09:57:47 +00:00
|
|
|
return self._data['wind_bearing']
|
2019-02-14 13:40:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def attribution(self):
|
|
|
|
"""Return the attribution."""
|
|
|
|
return ATTRIBUTION
|
|
|
|
|
|
|
|
@property
|
|
|
|
def forecast(self):
|
|
|
|
"""Return the forecast."""
|
|
|
|
reftime = datetime.now().replace(hour=12, minute=00)
|
|
|
|
reftime += timedelta(hours=24)
|
|
|
|
forecast_data = []
|
2019-02-15 09:57:47 +00:00
|
|
|
for key in self._data['forecast']:
|
|
|
|
value = self._data['forecast'][key]
|
2019-02-14 13:40:27 +00:00
|
|
|
data_dict = {
|
|
|
|
ATTR_FORECAST_TIME: reftime.isoformat(),
|
|
|
|
ATTR_FORECAST_TEMP: int(value['max_temp']),
|
|
|
|
ATTR_FORECAST_TEMP_LOW: int(value['min_temp']),
|
|
|
|
ATTR_FORECAST_CONDITION:
|
2019-02-15 09:57:47 +00:00
|
|
|
self.format_condition(value['weather'])
|
2019-02-14 13:40:27 +00:00
|
|
|
}
|
|
|
|
reftime = reftime + timedelta(hours=24)
|
|
|
|
forecast_data.append(data_dict)
|
|
|
|
return forecast_data
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format_condition(condition):
|
|
|
|
"""Return condition from dict CONDITION_CLASSES."""
|
|
|
|
for key, value in CONDITION_CLASSES.items():
|
|
|
|
if condition in value:
|
|
|
|
return key
|
|
|
|
return condition
|
2019-06-19 21:39:13 +00:00
|
|
|
|
|
|
|
@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
|