2019-04-03 15:40:03 +00:00
|
|
|
"""Support for the OpenWeatherMap (OWM) service."""
|
2015-05-22 08:11:21 +00:00
|
|
|
from datetime import timedelta
|
2017-12-29 09:06:52 +00:00
|
|
|
import logging
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2019-10-18 05:12:32 +00:00
|
|
|
from pyowm import OWM
|
|
|
|
from pyowm.exceptions.api_call_error import APICallError
|
2016-06-05 23:00:51 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-09-01 08:38:50 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_NAME,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
)
|
2016-06-05 23:00:51 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-05-01 19:52:34 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.util import Throttle
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-09-01 08:38:50 +00:00
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by OpenWeatherMap"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_FORECAST = "forecast"
|
|
|
|
CONF_LANGUAGE = "language"
|
2016-09-01 08:38:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "OWM"
|
2016-09-01 08:38:50 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
|
|
|
|
|
2015-05-01 19:52:34 +00:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"weather": ["Condition", None],
|
|
|
|
"temperature": ["Temperature", None],
|
|
|
|
"wind_speed": ["Wind speed", "m/s"],
|
|
|
|
"wind_bearing": ["Wind bearing", "°"],
|
|
|
|
"humidity": ["Humidity", "%"],
|
|
|
|
"pressure": ["Pressure", "mbar"],
|
|
|
|
"clouds": ["Cloud coverage", "%"],
|
|
|
|
"rain": ["Rain", "mm"],
|
|
|
|
"snow": ["Snow", "mm"],
|
|
|
|
"weather_code": ["Weather code", None],
|
2015-05-01 19:52:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_FORECAST, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_LANGUAGE): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-06-05 23:00:51 +00:00
|
|
|
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the OpenWeatherMap sensor."""
|
2017-12-29 09:06:52 +00:00
|
|
|
|
2015-05-01 19:52:34 +00:00
|
|
|
if None in (hass.config.latitude, hass.config.longitude):
|
|
|
|
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
2018-11-01 15:32:36 +00:00
|
|
|
return
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TYPES["temperature"][1] = hass.config.units.temperature_unit
|
2016-09-01 08:38:50 +00:00
|
|
|
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
forecast = config.get(CONF_FORECAST)
|
2017-06-17 10:37:34 +00:00
|
|
|
language = config.get(CONF_LANGUAGE)
|
|
|
|
if isinstance(language, str):
|
|
|
|
language = language.lower()[:2]
|
2016-09-01 08:38:50 +00:00
|
|
|
|
2017-06-17 10:37:34 +00:00
|
|
|
owm = OWM(API_key=config.get(CONF_API_KEY), language=language)
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
if not owm:
|
2017-02-03 08:44:07 +00:00
|
|
|
_LOGGER.error("Unable to connect to OpenWeatherMap")
|
2018-11-01 15:32:36 +00:00
|
|
|
return
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = WeatherData(owm, forecast, hass.config.latitude, hass.config.longitude)
|
2015-05-01 19:52:34 +00:00
|
|
|
dev = []
|
2016-09-01 08:38:50 +00:00
|
|
|
for variable in config[CONF_MONITORED_CONDITIONS]:
|
2019-07-31 19:25:30 +00:00
|
|
|
dev.append(
|
|
|
|
OpenWeatherMapSensor(name, data, variable, SENSOR_TYPES[variable][1])
|
|
|
|
)
|
2015-06-23 10:33:31 +00:00
|
|
|
|
2016-06-09 03:59:20 +00:00
|
|
|
if forecast:
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TYPES["forecast"] = ["Forecast", None]
|
|
|
|
dev.append(
|
|
|
|
OpenWeatherMapSensor(name, data, "forecast", SENSOR_TYPES["temperature"][1])
|
|
|
|
)
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OpenWeatherMapSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of an OpenWeatherMap sensor."""
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2016-09-01 08:38:50 +00:00
|
|
|
def __init__(self, name, weather_data, sensor_type, temp_unit):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-09-01 08:38:50 +00:00
|
|
|
self.client_name = name
|
2015-05-01 19:52:34 +00:00
|
|
|
self._name = SENSOR_TYPES[sensor_type][0]
|
|
|
|
self.owa_client = weather_data
|
2015-06-27 07:59:05 +00:00
|
|
|
self.temp_unit = temp_unit
|
2015-05-01 19:52:34 +00:00
|
|
|
self.type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 18:35:00 +00:00
|
|
|
return f"{self.client_name} {self._name}"
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the device."""
|
2015-05-01 19:52:34 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2015-05-01 19:52:34 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2016-10-11 07:28:19 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2016-10-11 07:28:19 +00:00
|
|
|
|
2015-05-01 19:52:34 +00:00
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from OWM and updates the states."""
|
2017-10-15 08:31:34 +00:00
|
|
|
try:
|
|
|
|
self.owa_client.update()
|
|
|
|
except APICallError:
|
2018-11-01 15:32:36 +00:00
|
|
|
_LOGGER.error("Error when calling API to update data")
|
2017-10-15 08:31:34 +00:00
|
|
|
return
|
|
|
|
|
2015-05-22 08:11:21 +00:00
|
|
|
data = self.owa_client.data
|
2015-06-23 10:33:31 +00:00
|
|
|
fc_data = self.owa_client.fc_data
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2018-01-02 19:54:06 +00:00
|
|
|
if data is None:
|
2017-07-01 14:14:18 +00:00
|
|
|
return
|
|
|
|
|
2018-11-01 15:32:36 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.type == "weather":
|
2018-11-01 15:32:36 +00:00
|
|
|
self._state = data.get_detailed_status()
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "temperature":
|
2018-11-01 15:32:36 +00:00
|
|
|
if self.temp_unit == TEMP_CELSIUS:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = round(data.get_temperature("celsius")["temp"], 1)
|
2018-11-01 15:32:36 +00:00
|
|
|
elif self.temp_unit == TEMP_FAHRENHEIT:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = round(data.get_temperature("fahrenheit")["temp"], 1)
|
2018-11-01 15:32:36 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = round(data.get_temperature()["temp"], 1)
|
|
|
|
elif self.type == "wind_speed":
|
|
|
|
self._state = round(data.get_wind()["speed"], 1)
|
|
|
|
elif self.type == "wind_bearing":
|
|
|
|
self._state = round(data.get_wind()["deg"], 1)
|
|
|
|
elif self.type == "humidity":
|
2018-11-01 15:32:36 +00:00
|
|
|
self._state = round(data.get_humidity(), 1)
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "pressure":
|
|
|
|
self._state = round(data.get_pressure()["press"], 0)
|
|
|
|
elif self.type == "clouds":
|
2018-11-01 15:32:36 +00:00
|
|
|
self._state = data.get_clouds()
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "rain":
|
2018-11-01 15:32:36 +00:00
|
|
|
if data.get_rain():
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = round(data.get_rain()["3h"], 0)
|
|
|
|
self._unit_of_measurement = "mm"
|
2018-11-01 15:32:36 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = "not raining"
|
|
|
|
self._unit_of_measurement = ""
|
|
|
|
elif self.type == "snow":
|
2018-11-01 15:32:36 +00:00
|
|
|
if data.get_snow():
|
|
|
|
self._state = round(data.get_snow(), 0)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._unit_of_measurement = "mm"
|
2018-11-01 15:32:36 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = "not snowing"
|
|
|
|
self._unit_of_measurement = ""
|
|
|
|
elif self.type == "forecast":
|
2018-11-01 15:32:36 +00:00
|
|
|
if fc_data is None:
|
|
|
|
return
|
|
|
|
self._state = fc_data.get_weathers()[0].get_detailed_status()
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "weather_code":
|
2018-11-01 15:32:36 +00:00
|
|
|
self._state = data.get_weather_code()
|
|
|
|
except KeyError:
|
|
|
|
self._state = None
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Condition is currently not available: %s", self.type)
|
2015-05-22 08:11:21 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class WeatherData:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from OpenWeatherMap."""
|
2015-05-22 08:11:21 +00:00
|
|
|
|
2015-06-23 10:33:31 +00:00
|
|
|
def __init__(self, owm, forecast, latitude, longitude):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the data object."""
|
2015-05-22 08:11:21 +00:00
|
|
|
self.owm = owm
|
2015-06-23 10:33:31 +00:00
|
|
|
self.forecast = forecast
|
2015-05-22 08:11:21 +00:00
|
|
|
self.latitude = latitude
|
|
|
|
self.longitude = longitude
|
|
|
|
self.data = None
|
2015-06-23 10:33:31 +00:00
|
|
|
self.fc_data = None
|
2015-05-22 08:11:21 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from OpenWeatherMap."""
|
2017-06-17 10:37:34 +00:00
|
|
|
try:
|
|
|
|
obs = self.owm.weather_at_coords(self.latitude, self.longitude)
|
2017-10-15 08:31:34 +00:00
|
|
|
except (APICallError, TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Error when calling API to get weather at coordinates")
|
2017-06-17 10:37:34 +00:00
|
|
|
obs = None
|
2017-10-15 08:31:34 +00:00
|
|
|
|
2016-01-21 03:36:33 +00:00
|
|
|
if obs is None:
|
2017-07-01 14:14:18 +00:00
|
|
|
_LOGGER.warning("Failed to fetch data")
|
2016-01-21 03:36:33 +00:00
|
|
|
return
|
|
|
|
|
2015-05-22 08:11:21 +00:00
|
|
|
self.data = obs.get_weather()
|
2015-06-23 10:33:31 +00:00
|
|
|
|
|
|
|
if self.forecast == 1:
|
2017-06-17 10:37:34 +00:00
|
|
|
try:
|
|
|
|
obs = self.owm.three_hours_forecast_at_coords(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.latitude, self.longitude
|
|
|
|
)
|
2017-06-17 10:37:34 +00:00
|
|
|
self.fc_data = obs.get_forecast()
|
2017-10-15 08:31:34 +00:00
|
|
|
except (ConnectionResetError, TypeError):
|
2017-07-01 14:14:18 +00:00
|
|
|
_LOGGER.warning("Failed to fetch forecast")
|