2015-05-01 19:52:34 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.openweathermap
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
OpenWeatherMap (OWM) service.
|
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
To use the OpenWeatherMap sensor you will need to add something like the
|
2015-09-07 17:40:09 +00:00
|
|
|
following to your configuration.yaml file.
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
sensor:
|
|
|
|
platform: openweathermap
|
|
|
|
api_key: YOUR_APP_KEY
|
2015-06-23 10:33:31 +00:00
|
|
|
forecast: 0 or 1
|
2015-06-20 14:22:00 +00:00
|
|
|
monitored_conditions:
|
2015-05-21 17:05:31 +00:00
|
|
|
- weather
|
|
|
|
- temperature
|
|
|
|
- wind_speed
|
|
|
|
- humidity
|
|
|
|
- pressure
|
|
|
|
- clouds
|
|
|
|
- rain
|
|
|
|
- snow
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2015-05-15 13:33:35 +00:00
|
|
|
Variables:
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
api_key
|
|
|
|
*Required
|
|
|
|
To retrieve this value log into your account at http://openweathermap.org/
|
|
|
|
|
2015-06-23 10:33:31 +00:00
|
|
|
forecast
|
|
|
|
*Optional
|
|
|
|
Enables the forecast. The default is to display the current conditions.
|
|
|
|
|
2015-05-21 17:05:31 +00:00
|
|
|
monitored_conditions
|
2015-09-08 14:33:13 +00:00
|
|
|
*Required
|
2015-06-20 14:22:00 +00:00
|
|
|
Conditions to monitor. See the configuration example above for a
|
2015-05-21 17:05:31 +00:00
|
|
|
list of all available conditions to monitor.
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
Details for the API : http://bugs.openweathermap.org/projects/api/wiki
|
|
|
|
|
|
|
|
Only metric measurements are supported at the moment.
|
|
|
|
"""
|
|
|
|
import logging
|
2015-05-22 08:11:21 +00:00
|
|
|
from datetime import timedelta
|
2015-05-01 19:52:34 +00:00
|
|
|
|
2015-05-22 08:11:21 +00:00
|
|
|
from homeassistant.util import Throttle
|
2015-05-01 19:52:34 +00:00
|
|
|
from homeassistant.const import (CONF_API_KEY, TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2015-08-30 01:39:50 +00:00
|
|
|
REQUIREMENTS = ['pyowm==2.2.1']
|
2015-05-01 19:52:34 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
SENSOR_TYPES = {
|
|
|
|
'weather': ['Condition', ''],
|
|
|
|
'temperature': ['Temperature', ''],
|
|
|
|
'wind_speed': ['Wind speed', 'm/s'],
|
|
|
|
'humidity': ['Humidity', '%'],
|
|
|
|
'pressure': ['Pressure', 'hPa'],
|
|
|
|
'clouds': ['Cloud coverage', '%'],
|
|
|
|
'rain': ['Rain', 'mm'],
|
|
|
|
'snow': ['Snow', 'mm']
|
|
|
|
}
|
|
|
|
|
2015-05-22 08:11:21 +00:00
|
|
|
# Return cached results if last scan was less then this time ago
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
|
|
|
|
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Get the OpenWeatherMap sensor. """
|
|
|
|
|
|
|
|
if None in (hass.config.latitude, hass.config.longitude):
|
|
|
|
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
from pyowm import OWM
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
_LOGGER.exception(
|
|
|
|
"Unable to import pyowm. "
|
|
|
|
"Did you maybe not install the 'PyOWM' package?")
|
|
|
|
|
2015-06-17 20:36:54 +00:00
|
|
|
return False
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES['temperature'][1] = hass.config.temperature_unit
|
|
|
|
unit = hass.config.temperature_unit
|
2015-06-23 10:33:31 +00:00
|
|
|
forecast = config.get('forecast', 0)
|
2015-05-01 19:52:34 +00:00
|
|
|
owm = OWM(config.get(CONF_API_KEY, None))
|
|
|
|
|
|
|
|
if not owm:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Connection error "
|
|
|
|
"Please check your settings for OpenWeatherMap.")
|
|
|
|
return None
|
|
|
|
|
2015-06-23 10:33:31 +00:00
|
|
|
data = WeatherData(owm, forecast, hass.config.latitude,
|
|
|
|
hass.config.longitude)
|
2015-05-01 19:52:34 +00:00
|
|
|
dev = []
|
2015-06-23 10:33:31 +00:00
|
|
|
try:
|
|
|
|
for variable in config['monitored_conditions']:
|
|
|
|
if variable not in SENSOR_TYPES:
|
|
|
|
_LOGGER.error('Sensor type: "%s" does not exist', variable)
|
|
|
|
else:
|
|
|
|
dev.append(OpenWeatherMapSensor(data, variable, unit))
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if forecast == 1:
|
|
|
|
SENSOR_TYPES['forecast'] = ['Forecast', '']
|
|
|
|
dev.append(OpenWeatherMapSensor(data, 'forecast', unit))
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
add_devices(dev)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class OpenWeatherMapSensor(Entity):
|
|
|
|
""" Implements an OpenWeatherMap sensor. """
|
|
|
|
|
2015-06-27 07:59:05 +00:00
|
|
|
def __init__(self, weather_data, sensor_type, temp_unit):
|
2015-06-23 10:33:31 +00:00
|
|
|
self.client_name = 'Weather'
|
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]
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2015-06-23 10:33:31 +00:00
|
|
|
return '{} {}'.format(self.client_name, self._name)
|
2015-05-01 19:52:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the device. """
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
""" Unit of measurement of this entity, if any. """
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
# pylint: disable=too-many-branches
|
|
|
|
def update(self):
|
|
|
|
""" Gets the latest data from OWM and updates the states. """
|
2015-05-22 08:11:21 +00:00
|
|
|
|
|
|
|
self.owa_client.update()
|
|
|
|
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
|
|
|
|
|
|
|
if self.type == 'weather':
|
|
|
|
self._state = data.get_detailed_status()
|
2015-05-15 13:33:35 +00:00
|
|
|
elif self.type == 'temperature':
|
2015-06-27 07:59:05 +00:00
|
|
|
if self.temp_unit == TEMP_CELCIUS:
|
2015-05-01 19:52:34 +00:00
|
|
|
self._state = round(data.get_temperature('celsius')['temp'],
|
|
|
|
1)
|
2015-06-27 07:59:05 +00:00
|
|
|
elif self.temp_unit == TEMP_FAHRENHEIT:
|
2015-05-01 19:52:34 +00:00
|
|
|
self._state = round(data.get_temperature('fahrenheit')['temp'],
|
|
|
|
1)
|
|
|
|
else:
|
|
|
|
self._state = round(data.get_temperature()['temp'], 1)
|
|
|
|
elif self.type == 'wind_speed':
|
|
|
|
self._state = data.get_wind()['speed']
|
|
|
|
elif self.type == 'humidity':
|
|
|
|
self._state = data.get_humidity()
|
|
|
|
elif self.type == 'pressure':
|
|
|
|
self._state = round(data.get_pressure()['press'], 0)
|
|
|
|
elif self.type == 'clouds':
|
|
|
|
self._state = data.get_clouds()
|
|
|
|
elif self.type == 'rain':
|
|
|
|
if data.get_rain():
|
|
|
|
self._state = round(data.get_rain()['3h'], 0)
|
2015-06-27 07:59:05 +00:00
|
|
|
self._unit_of_measurement = 'mm'
|
2015-05-01 19:52:34 +00:00
|
|
|
else:
|
|
|
|
self._state = 'not raining'
|
|
|
|
self._unit_of_measurement = ''
|
|
|
|
elif self.type == 'snow':
|
|
|
|
if data.get_snow():
|
|
|
|
self._state = round(data.get_snow(), 0)
|
2015-06-27 07:59:05 +00:00
|
|
|
self._unit_of_measurement = 'mm'
|
2015-05-01 19:52:34 +00:00
|
|
|
else:
|
|
|
|
self._state = 'not snowing'
|
|
|
|
self._unit_of_measurement = ''
|
2015-06-23 10:33:31 +00:00
|
|
|
elif self.type == 'forecast':
|
|
|
|
self._state = fc_data.get_weathers()[0].get_status()
|
2015-05-22 08:11:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WeatherData(object):
|
|
|
|
""" Gets the latest data from OpenWeatherMap. """
|
|
|
|
|
2015-06-23 10:33:31 +00:00
|
|
|
def __init__(self, owm, forecast, latitude, longitude):
|
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):
|
|
|
|
""" Gets the latest data from OpenWeatherMap. """
|
|
|
|
obs = self.owm.weather_at_coords(self.latitude, self.longitude)
|
|
|
|
self.data = obs.get_weather()
|
2015-06-23 10:33:31 +00:00
|
|
|
|
|
|
|
if self.forecast == 1:
|
|
|
|
obs = self.owm.three_hours_forecast_at_coords(self.latitude,
|
|
|
|
self.longitude)
|
|
|
|
self.fc_data = obs.get_forecast()
|