Merge pull request #138 from fabaff/rewrite-owm

OpenWeatherMap sensor
This commit is contained in:
Paulus Schoutsen 2015-05-22 07:05:44 -07:00
commit 852305b996

View File

@ -13,14 +13,14 @@ sensor:
platform: openweathermap platform: openweathermap
api_key: YOUR_APP_KEY api_key: YOUR_APP_KEY
monitored_variables: monitored_variables:
- type: 'weather' - weather
- type: 'temperature' - temperature
- type: 'wind_speed' - wind_speed
- type: 'humidity' - humidity
- type: 'pressure' - pressure
- type: 'clouds' - clouds
- type: 'rain' - rain
- type: 'snow' - snow
Variables: Variables:
@ -28,28 +28,29 @@ api_key
*Required *Required
To retrieve this value log into your account at http://openweathermap.org/ To retrieve this value log into your account at http://openweathermap.org/
monitored_variables monitored_conditions
*Required *Required
An array specifying the variables to monitor. An array specifying the variables to monitor.
These are the variables for the monitored_variables array: These are the variables for the monitored_conditions array:
type type
*Required *Required
The variable you wish to monitor, see the configuration example above for a The variable you wish to monitor, see the configuration example above for a
list of all available variables list of all available conditions to monitor.
Details for the API : http://bugs.openweathermap.org/projects/api/wiki Details for the API : http://bugs.openweathermap.org/projects/api/wiki
Only metric measurements are supported at the moment. Only metric measurements are supported at the moment.
""" """
import logging import logging
from datetime import timedelta
from homeassistant.util import Throttle
from homeassistant.const import (CONF_API_KEY, TEMP_CELCIUS, TEMP_FAHRENHEIT) from homeassistant.const import (CONF_API_KEY, TEMP_CELCIUS, TEMP_FAHRENHEIT)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_THROTTLED_REFRESH = None
SENSOR_TYPES = { SENSOR_TYPES = {
'weather': ['Condition', ''], 'weather': ['Condition', ''],
'temperature': ['Temperature', ''], 'temperature': ['Temperature', ''],
@ -61,6 +62,9 @@ SENSOR_TYPES = {
'snow': ['Snow', 'mm'] 'snow': ['Snow', 'mm']
} }
# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
""" Get the OpenWeatherMap sensor. """ """ Get the OpenWeatherMap sensor. """
@ -82,7 +86,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
SENSOR_TYPES['temperature'][1] = hass.config.temperature_unit SENSOR_TYPES['temperature'][1] = hass.config.temperature_unit
unit = hass.config.temperature_unit unit = hass.config.temperature_unit
owm = OWM(config.get(CONF_API_KEY, None)) owm = OWM(config.get(CONF_API_KEY, None))
obs = owm.weather_at_coords(hass.config.latitude, hass.config.longitude)
if not owm: if not owm:
_LOGGER.error( _LOGGER.error(
@ -90,12 +93,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"Please check your settings for OpenWeatherMap.") "Please check your settings for OpenWeatherMap.")
return None return None
data = WeatherData(owm, hass.config.latitude, hass.config.longitude)
dev = [] dev = []
for variable in config['monitored_variables']: for variable in config['monitored_conditions']:
if variable['type'] not in SENSOR_TYPES: if variable not in SENSOR_TYPES:
_LOGGER.error('Sensor type: "%s" does not exist', variable['type']) _LOGGER.error('Sensor type: "%s" does not exist', variable)
else: else:
dev.append(OpenWeatherMapSensor(variable['type'], obs, unit)) dev.append(OpenWeatherMapSensor(data, variable, unit))
add_devices(dev) add_devices(dev)
@ -104,7 +108,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class OpenWeatherMapSensor(Entity): class OpenWeatherMapSensor(Entity):
""" Implements an OpenWeatherMap sensor. """ """ Implements an OpenWeatherMap sensor. """
def __init__(self, sensor_type, weather_data, unit): def __init__(self, weather_data, sensor_type, unit):
self.client_name = 'Weather - ' self.client_name = 'Weather - '
self._name = SENSOR_TYPES[sensor_type][0] self._name = SENSOR_TYPES[sensor_type][0]
self.owa_client = weather_data self.owa_client = weather_data
@ -131,7 +135,9 @@ class OpenWeatherMapSensor(Entity):
# pylint: disable=too-many-branches # pylint: disable=too-many-branches
def update(self): def update(self):
""" Gets the latest data from OWM and updates the states. """ """ Gets the latest data from OWM and updates the states. """
data = self.owa_client.get_weather()
self.owa_client.update()
data = self.owa_client.data
if self.type == 'weather': if self.type == 'weather':
self._state = data.get_detailed_status() self._state = data.get_detailed_status()
@ -164,3 +170,20 @@ class OpenWeatherMapSensor(Entity):
else: else:
self._state = 'not snowing' self._state = 'not snowing'
self._unit_of_measurement = '' self._unit_of_measurement = ''
class WeatherData(object):
""" Gets the latest data from OpenWeatherMap. """
def __init__(self, owm, latitude, longitude):
self.owm = owm
self.latitude = latitude
self.longitude = longitude
self.data = None
@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()