From 4001a2d31613ca0082ec69722cc16352cef2b445 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 1 May 2015 15:55:33 +0200 Subject: [PATCH 1/3] add pyown --- requirements.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/requirements.txt b/requirements.txt index 59a1c3ea39a..3afa9c39a22 100644 --- a/requirements.txt +++ b/requirements.txt @@ -46,3 +46,6 @@ python-pushover>=0.2 # Transmission Torrent Client transmissionrpc>=0.11 + +# OpenWeatherMap Web API +pyowm>=2.2.0 \ No newline at end of file From 70f1ec9dce684929b911dd276dbc435f50a63a40 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 1 May 2015 21:52:34 +0200 Subject: [PATCH 2/3] add openweathermap sensor --- .../components/sensor/openweathermap.py | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 homeassistant/components/sensor/openweathermap.py diff --git a/homeassistant/components/sensor/openweathermap.py b/homeassistant/components/sensor/openweathermap.py new file mode 100644 index 00000000000..ed5520a3817 --- /dev/null +++ b/homeassistant/components/sensor/openweathermap.py @@ -0,0 +1,174 @@ +""" +homeassistant.components.sensor.openweathermap +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +OpenWeatherMap (OWM) service. + +Configuration: + +To use the OpenWeatherMap sensor you will need to add something like the +following to your config/configuration.yaml + +sensor: + platform: openweathermap + api_key: YOUR_APP_KEY + monitored_variables: + - type: 'weather' + - type: 'temperature' + - type: 'wind_speed' + - type: 'humidity' + - type: 'pressure' + - type: 'clouds' + - type: 'rain' + - type: 'snow' + +VARIABLES: + +api_key +*Required +To retrieve this value log into your account at http://openweathermap.org/ + +monitored_variables +*Required +An array specifying the variables to monitor. + +These are the variables for the monitored_variables array: + +type +*Required +The variable you wish to monitor, see the configuration example above for a +list of all available variables + +Details for the API : http://bugs.openweathermap.org/projects/api/wiki + +Only metric measurements are supported at the moment. + +""" +import logging + +from homeassistant.const import (CONF_API_KEY, TEMP_CELCIUS, TEMP_FAHRENHEIT) +from homeassistant.helpers.entity import Entity + +_LOGGER = logging.getLogger(__name__) +_THROTTLED_REFRESH = None +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'] +} + + +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?") + + return None + + SENSOR_TYPES['temperature'][1] = hass.config.temperature_unit + unit = hass.config.temperature_unit + owm = OWM(config.get(CONF_API_KEY, None)) + obs = owm.weather_at_coords(hass.config.latitude, hass.config.longitude) + + if not owm: + _LOGGER.error( + "Connection error " + "Please check your settings for OpenWeatherMap.") + return None + + dev = [] + for variable in config['monitored_variables']: + if variable['type'] not in SENSOR_TYPES: + _LOGGER.error('Sensor type: "%s" does not exist', variable['type']) + else: + dev.append(OpenWeatherMapSensor(variable['type'], obs, unit)) + + add_devices(dev) + + +# pylint: disable=too-few-public-methods +class OpenWeatherMapSensor(Entity): + """ Implements an OpenWeatherMap sensor. """ + + def __init__(self, sensor_type, weather_data, unit): + self.client_name = 'Weather - ' + self._name = SENSOR_TYPES[sensor_type][0] + self.owa_client = weather_data + self._unit = unit + self.type = sensor_type + self._state = None + self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] + self.update() + + @property + def name(self): + return self.client_name + ' ' + self._name + + @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. """ + data = self.owa_client.get_weather() + + if self.type == 'weather': + self._state = data.get_detailed_status() + + if self.type == 'temperature': + if self._unit == TEMP_CELCIUS: + self._state = round(data.get_temperature('celsius')['temp'], + 1) + elif self._unit == TEMP_FAHRENHEIT: + 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) + else: + self._state = 'not raining' + self._unit_of_measurement = '' + + elif self.type == 'snow': + if data.get_snow(): + self._state = round(data.get_snow(), 0) + else: + self._state = 'not snowing' + self._unit_of_measurement = '' From fd6de36d2ca625ce2163863f508e7d3d677fb489 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 1 May 2015 21:56:42 +0200 Subject: [PATCH 3/3] add openweathermap, sort the content alphabetically --- .coveragerc | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/.coveragerc b/.coveragerc index 2115ee07b3c..250d207f343 100644 --- a/.coveragerc +++ b/.coveragerc @@ -22,25 +22,26 @@ omit = homeassistant/components/*/tellstick.py homeassistant/components/*/vera.py - homeassistant/components/keyboard.py homeassistant/components/browser.py - homeassistant/components/switch/wemo.py - homeassistant/components/thermostat/nest.py - homeassistant/components/light/hue.py - homeassistant/components/sensor/systemmonitor.py - homeassistant/components/sensor/sabnzbd.py - homeassistant/components/sensor/mysensors.py - homeassistant/components/notify/pushbullet.py - homeassistant/components/notify/pushover.py - homeassistant/components/notify/instapush.py - homeassistant/components/notify/nma.py - homeassistant/components/media_player/cast.py + homeassistant/components/device_tracker/ddwrt.py homeassistant/components/device_tracker/luci.py - homeassistant/components/device_tracker/tomato.py homeassistant/components/device_tracker/netgear.py homeassistant/components/device_tracker/nmap_tracker.py - homeassistant/components/device_tracker/ddwrt.py + homeassistant/components/device_tracker/tomato.py + homeassistant/components/keyboard.py + homeassistant/components/light/hue.py + homeassistant/components/media_player/cast.py + homeassistant/components/notify/instapush.py + homeassistant/components/notify/nma.py + homeassistant/components/notify/pushbullet.py + homeassistant/components/notify/pushover.py + homeassistant/components/sensor/mysensors.py + homeassistant/components/sensor/openweathermap.py + homeassistant/components/sensor/sabnzbd.py + homeassistant/components/sensor/systemmonitor.py homeassistant/components/sensor/transmission.py + homeassistant/components/switch/wemo.py + homeassistant/components/thermostat/nest.py [report]