From c4a0b41b8e9a14da455879795f2b06edd7f60c6e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 23 Jun 2015 08:23:53 +0200 Subject: [PATCH 1/3] update pyowm to 2.2.1 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index e004acab60c..2684855be54 100644 --- a/requirements.txt +++ b/requirements.txt @@ -51,7 +51,7 @@ python-pushover>=0.2 transmissionrpc>=0.11 # OpenWeatherMap Web API (sensor.openweathermap) -pyowm>=2.2.0 +pyowm>=2.2.1 # XMPP Bindings (notify.xmpp) sleekxmpp>=1.3.1 From fe600b78772f9881a0a24507fb896a64ebaf326e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 23 Jun 2015 12:33:31 +0200 Subject: [PATCH 2/3] 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() From e971a01acdf61bc1e5712d358ad31b7ff2a5d97f Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sat, 27 Jun 2015 09:59:05 +0200 Subject: [PATCH 3/3] re-add unit --- homeassistant/components/sensor/openweathermap.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/openweathermap.py b/homeassistant/components/sensor/openweathermap.py index a9c019cbecb..07413e7b1ea 100644 --- a/homeassistant/components/sensor/openweathermap.py +++ b/homeassistant/components/sensor/openweathermap.py @@ -115,11 +115,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class OpenWeatherMapSensor(Entity): """ 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._name = SENSOR_TYPES[sensor_type][0] self.owa_client = weather_data - self._unit = unit + self.temp_unit = temp_unit self.type = sensor_type self._state = None self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] @@ -150,10 +150,10 @@ class OpenWeatherMapSensor(Entity): if self.type == 'weather': self._state = data.get_detailed_status() elif self.type == 'temperature': - if self._unit == TEMP_CELCIUS: + if self.temp_unit == TEMP_CELCIUS: self._state = round(data.get_temperature('celsius')['temp'], 1) - elif self._unit == TEMP_FAHRENHEIT: + elif self.temp_unit == TEMP_FAHRENHEIT: self._state = round(data.get_temperature('fahrenheit')['temp'], 1) else: @@ -169,12 +169,14 @@ class OpenWeatherMapSensor(Entity): elif self.type == 'rain': if data.get_rain(): self._state = round(data.get_rain()['3h'], 0) + self._unit_of_measurement = 'mm' else: self._state = 'not raining' self._unit_of_measurement = '' elif self.type == 'snow': if data.get_snow(): self._state = round(data.get_snow(), 0) + self._unit_of_measurement = 'mm' else: self._state = 'not snowing' self._unit_of_measurement = ''