From fe600b78772f9881a0a24507fb896a64ebaf326e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 23 Jun 2015 12:33:31 +0200 Subject: [PATCH] add forecast --- .../components/sensor/openweathermap.py | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/sensor/openweathermap.py b/homeassistant/components/sensor/openweathermap.py index 2a82fa1206f..a9c019cbecb 100644 --- a/homeassistant/components/sensor/openweathermap.py +++ b/homeassistant/components/sensor/openweathermap.py @@ -1,7 +1,6 @@ """ homeassistant.components.sensor.openweathermap ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - OpenWeatherMap (OWM) service. Configuration: @@ -12,6 +11,7 @@ following to your config/configuration.yaml sensor: platform: openweathermap api_key: YOUR_APP_KEY + forecast: 0 or 1 monitored_conditions: - weather - temperature @@ -28,8 +28,12 @@ api_key *Required 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 -*Required +*Optional Conditions to monitor. See the configuration example above for a 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 unit = hass.config.temperature_unit + forecast = config.get('forecast', 0) owm = OWM(config.get(CONF_API_KEY, None)) if not owm: @@ -87,13 +92,21 @@ def setup_platform(hass, config, add_devices, discovery_info=None): "Please check your settings for OpenWeatherMap.") return None - data = WeatherData(owm, hass.config.latitude, hass.config.longitude) + data = WeatherData(owm, forecast, hass.config.latitude, + hass.config.longitude) dev = [] - 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)) + 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)) add_devices(dev) @@ -103,7 +116,7 @@ class OpenWeatherMapSensor(Entity): """ Implements an OpenWeatherMap sensor. """ def __init__(self, weather_data, sensor_type, unit): - self.client_name = 'Weather - ' + self.client_name = 'Weather' self._name = SENSOR_TYPES[sensor_type][0] self.owa_client = weather_data self._unit = unit @@ -114,7 +127,7 @@ class OpenWeatherMapSensor(Entity): @property def name(self): - return self.client_name + ' ' + self._name + return '{} {}'.format(self.client_name, self._name) @property def state(self): @@ -132,6 +145,7 @@ class OpenWeatherMapSensor(Entity): self.owa_client.update() data = self.owa_client.data + fc_data = self.owa_client.fc_data if self.type == 'weather': self._state = data.get_detailed_status() @@ -164,20 +178,28 @@ class OpenWeatherMapSensor(Entity): else: self._state = 'not snowing' self._unit_of_measurement = '' + elif self.type == 'forecast': + self._state = fc_data.get_weathers()[0].get_status() class WeatherData(object): """ Gets the latest data from OpenWeatherMap. """ - def __init__(self, owm, latitude, longitude): + def __init__(self, owm, forecast, latitude, longitude): self.owm = owm + self.forecast = forecast self.latitude = latitude self.longitude = longitude self.data = None + self.fc_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() + + if self.forecast == 1: + obs = self.owm.three_hours_forecast_at_coords(self.latitude, + self.longitude) + self.fc_data = obs.get_forecast()