Merge pull request #192 from fabaff/owm-forecast

Forecast option for OpenWeatherMap sensor
This commit is contained in:
Fabian Affolter 2015-06-27 10:08:33 +02:00
commit f10c51b5f3
2 changed files with 41 additions and 17 deletions

View File

@ -1,7 +1,6 @@
""" """
homeassistant.components.sensor.openweathermap homeassistant.components.sensor.openweathermap
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OpenWeatherMap (OWM) service. OpenWeatherMap (OWM) service.
Configuration: Configuration:
@ -12,6 +11,7 @@ following to your config/configuration.yaml
sensor: sensor:
platform: openweathermap platform: openweathermap
api_key: YOUR_APP_KEY api_key: YOUR_APP_KEY
forecast: 0 or 1
monitored_conditions: monitored_conditions:
- weather - weather
- temperature - temperature
@ -28,8 +28,12 @@ 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/
forecast
*Optional
Enables the forecast. The default is to display the current conditions.
monitored_conditions monitored_conditions
*Required *Optional
Conditions to monitor. See the configuration example above for a Conditions to monitor. See the configuration example above for a
list of all available conditions to monitor. list of all available conditions to monitor.
@ -79,6 +83,7 @@ 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
forecast = config.get('forecast', 0)
owm = OWM(config.get(CONF_API_KEY, None)) owm = OWM(config.get(CONF_API_KEY, None))
if not owm: if not owm:
@ -87,13 +92,21 @@ 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) data = WeatherData(owm, forecast, hass.config.latitude,
hass.config.longitude)
dev = [] dev = []
for variable in config['monitored_conditions']: try:
if variable not in SENSOR_TYPES: for variable in config['monitored_conditions']:
_LOGGER.error('Sensor type: "%s" does not exist', variable) if variable not in SENSOR_TYPES:
else: _LOGGER.error('Sensor type: "%s" does not exist', variable)
dev.append(OpenWeatherMapSensor(data, variable, unit)) else:
dev.append(OpenWeatherMapSensor(data, variable, unit))
except KeyError:
pass
if forecast == 1:
SENSOR_TYPES['forecast'] = ['Forecast', '']
dev.append(OpenWeatherMapSensor(data, 'forecast', unit))
add_devices(dev) add_devices(dev)
@ -102,11 +115,11 @@ 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, weather_data, sensor_type, unit): def __init__(self, weather_data, sensor_type, temp_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
self._unit = unit self.temp_unit = temp_unit
self.type = sensor_type self.type = sensor_type
self._state = None self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
@ -114,7 +127,7 @@ class OpenWeatherMapSensor(Entity):
@property @property
def name(self): def name(self):
return self.client_name + ' ' + self._name return '{} {}'.format(self.client_name, self._name)
@property @property
def state(self): def state(self):
@ -132,14 +145,15 @@ class OpenWeatherMapSensor(Entity):
self.owa_client.update() self.owa_client.update()
data = self.owa_client.data data = self.owa_client.data
fc_data = self.owa_client.fc_data
if self.type == 'weather': if self.type == 'weather':
self._state = data.get_detailed_status() self._state = data.get_detailed_status()
elif self.type == 'temperature': elif self.type == 'temperature':
if self._unit == TEMP_CELCIUS: if self.temp_unit == TEMP_CELCIUS:
self._state = round(data.get_temperature('celsius')['temp'], self._state = round(data.get_temperature('celsius')['temp'],
1) 1)
elif self._unit == TEMP_FAHRENHEIT: elif self.temp_unit == TEMP_FAHRENHEIT:
self._state = round(data.get_temperature('fahrenheit')['temp'], self._state = round(data.get_temperature('fahrenheit')['temp'],
1) 1)
else: else:
@ -155,29 +169,39 @@ class OpenWeatherMapSensor(Entity):
elif self.type == 'rain': elif self.type == 'rain':
if data.get_rain(): if data.get_rain():
self._state = round(data.get_rain()['3h'], 0) self._state = round(data.get_rain()['3h'], 0)
self._unit_of_measurement = 'mm'
else: else:
self._state = 'not raining' self._state = 'not raining'
self._unit_of_measurement = '' self._unit_of_measurement = ''
elif self.type == 'snow': elif self.type == 'snow':
if data.get_snow(): if data.get_snow():
self._state = round(data.get_snow(), 0) self._state = round(data.get_snow(), 0)
self._unit_of_measurement = 'mm'
else: else:
self._state = 'not snowing' self._state = 'not snowing'
self._unit_of_measurement = '' self._unit_of_measurement = ''
elif self.type == 'forecast':
self._state = fc_data.get_weathers()[0].get_status()
class WeatherData(object): class WeatherData(object):
""" Gets the latest data from OpenWeatherMap. """ """ Gets the latest data from OpenWeatherMap. """
def __init__(self, owm, latitude, longitude): def __init__(self, owm, forecast, latitude, longitude):
self.owm = owm self.owm = owm
self.forecast = forecast
self.latitude = latitude self.latitude = latitude
self.longitude = longitude self.longitude = longitude
self.data = None self.data = None
self.fc_data = None
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self): def update(self):
""" Gets the latest data from OpenWeatherMap. """ """ Gets the latest data from OpenWeatherMap. """
obs = self.owm.weather_at_coords(self.latitude, self.longitude) obs = self.owm.weather_at_coords(self.latitude, self.longitude)
self.data = obs.get_weather() self.data = obs.get_weather()
if self.forecast == 1:
obs = self.owm.three_hours_forecast_at_coords(self.latitude,
self.longitude)
self.fc_data = obs.get_forecast()

View File

@ -51,7 +51,7 @@ python-pushover>=0.2
transmissionrpc>=0.11 transmissionrpc>=0.11
# OpenWeatherMap Web API (sensor.openweathermap) # OpenWeatherMap Web API (sensor.openweathermap)
pyowm>=2.2.0 pyowm>=2.2.1
# XMPP Bindings (notify.xmpp) # XMPP Bindings (notify.xmpp)
sleekxmpp>=1.3.1 sleekxmpp>=1.3.1