From 41f84d9e204386722a7bab676addcf6d0dd35937 Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:27:38 -0600 Subject: [PATCH 01/13] Pydoc for unit test methods --- tests/components/sensor/test_wunderground.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/components/sensor/test_wunderground.py b/tests/components/sensor/test_wunderground.py index c6664b71254..6c4979fb440 100644 --- a/tests/components/sensor/test_wunderground.py +++ b/tests/components/sensor/test_wunderground.py @@ -28,12 +28,17 @@ ICON_URL = 'http://icons.wxug.com/i/c/k/clear.gif' def mocked_requests_get(*args, **kwargs): + """Mock requests.get invocations.""" + class MockResponse: + """Class to represent a mocked response.""" def __init__(self, json_data, status_code): + """Initialize the mock response class.""" self.json_data = json_data self.status_code = status_code def json(self): + """Return the json of the response.""" return self.json_data if str(args[0]).startswith('http://api.wunderground.com/api/foo/'): @@ -123,6 +128,7 @@ class TestWundergroundSetup(unittest.TestCase): @unittest.mock.patch('requests.get', side_effect=mocked_requests_get) def test_sensor(self, req_mock): + """Test the wundergroun sensor class and methods.""" wunderground.setup_platform(self.hass, VALID_CONFIG, self.add_devices, None) print(str(self.DEVICES)) From 2b8e2a3d36c671e7ce562e0b46257f0009bdf142 Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:27:53 -0600 Subject: [PATCH 02/13] Remove print lines --- tests/components/sensor/test_wunderground.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/components/sensor/test_wunderground.py b/tests/components/sensor/test_wunderground.py index 6c4979fb440..15a903aec56 100644 --- a/tests/components/sensor/test_wunderground.py +++ b/tests/components/sensor/test_wunderground.py @@ -42,8 +42,6 @@ def mocked_requests_get(*args, **kwargs): return self.json_data if str(args[0]).startswith('http://api.wunderground.com/api/foo/'): - # Return valid response - print('VALID RESPONSE') return MockResponse({ "response": { "version": "0.1", @@ -65,8 +63,6 @@ def mocked_requests_get(*args, **kwargs): } }, 200) else: - # Return invalid api key - print('INVALID RESPONSE') return MockResponse({ "response": { "version": "0.1", @@ -104,11 +100,9 @@ class TestWundergroundSetup(unittest.TestCase): @unittest.mock.patch('requests.get', side_effect=mocked_requests_get) def test_setup(self, req_mock): """Test that the component is loaded if passed in PSW Id.""" - print('1') self.assertTrue( wunderground.setup_platform(self.hass, VALID_CONFIG_PWS, self.add_devices, None)) - print('2') self.assertTrue( wunderground.setup_platform(self.hass, VALID_CONFIG, self.add_devices, @@ -131,7 +125,6 @@ class TestWundergroundSetup(unittest.TestCase): """Test the wundergroun sensor class and methods.""" wunderground.setup_platform(self.hass, VALID_CONFIG, self.add_devices, None) - print(str(self.DEVICES)) for device in self.DEVICES: self.assertTrue(str(device.name).startswith('PWS_')) if device.name == 'PWS_weather': From 0490fe832a34d84c9980c24e267b8bb9d839dd4a Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:32:19 -0600 Subject: [PATCH 03/13] Unneeded validation removed --- homeassistant/components/sensor/wunderground.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index cb724247436..509a1e91ffa 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -66,10 +66,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): config.get(CONF_PWS_ID, None)) sensors = [] for variable in config['monitored_conditions']: - if variable in SENSOR_TYPES: - sensors.append(WUndergroundSensor(rest, variable)) - else: - _LOGGER.error('Wunderground sensor: "%s" does not exist', variable) + sensors.append(WUndergroundSensor(rest, variable)) try: rest.update() From 6f57d36134e3b3b7c7e50b56be62edb88f32cb6f Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:37:00 -0600 Subject: [PATCH 04/13] Add doc link to header of file --- homeassistant/components/sensor/wunderground.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index 509a1e91ffa..21ebcd1107b 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -1,4 +1,9 @@ -"""Support for Wunderground weather service.""" +""" +Support for Weather Underground weather service. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.wunderground/ +""" from datetime import timedelta import logging import requests From ddec28da4b342f14c928ee6fbbb2d0765e24944f Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:37:26 -0600 Subject: [PATCH 05/13] Use schema validators already avaialble --- homeassistant/components/sensor/wunderground.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index 21ebcd1107b..7e45d115eb6 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -11,7 +11,7 @@ import requests import voluptuous as vol from homeassistant.helpers.entity import Entity -from homeassistant.helpers.config_validation import ensure_list +from homeassistant.helpers.config_validation import (ensure_list, string) from homeassistant.util import Throttle from homeassistant.const import (CONF_PLATFORM, CONF_MONITORED_CONDITIONS, CONF_API_KEY, TEMP_FAHRENHEIT, TEMP_CELSIUS, @@ -57,8 +57,8 @@ SENSOR_TYPES = { PLATFORM_SCHEMA = vol.Schema({ vol.Required(CONF_PLATFORM): "wunderground", - vol.Required(CONF_API_KEY): vol.Coerce(str), - CONF_PWS_ID: vol.Coerce(str), + vol.Required(CONF_API_KEY): string, + vol.Optional(CONF_PWS_ID): string, vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(ensure_list, [vol.In(SENSOR_TYPES)]), }) From db2d9ec854713c8277cb6ff48c17aef3bf9ad775 Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:37:39 -0600 Subject: [PATCH 06/13] Unused property --- homeassistant/components/sensor/wunderground.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index 7e45d115eb6..737133e5c1d 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -91,7 +91,6 @@ class WUndergroundSensor(Entity): """Initialize the sensor.""" self.rest = rest self._condition = condition - self._unit_of_measurement = None @property def name(self): From ec5e20f0d99e2c7a7022073ad4516028ca10135e Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:38:34 -0600 Subject: [PATCH 07/13] Use string constant --- homeassistant/components/sensor/wunderground.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index 737133e5c1d..c041d7ceaff 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -70,7 +70,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): config.get(CONF_API_KEY), config.get(CONF_PWS_ID, None)) sensors = [] - for variable in config['monitored_conditions']: + for variable in config[CONF_MONITORED_CONDITIONS]: sensors.append(WUndergroundSensor(rest, variable)) try: From c2b75140bf01b8c8e57a4a835f7cae017b15a939 Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:40:28 -0600 Subject: [PATCH 08/13] Fix config validation import to make things more readable --- homeassistant/components/sensor/wunderground.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index c041d7ceaff..7d3084b47be 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -11,7 +11,7 @@ import requests import voluptuous as vol from homeassistant.helpers.entity import Entity -from homeassistant.helpers.config_validation import (ensure_list, string) +import homeassistant.helpers.config_validation as cv from homeassistant.util import Throttle from homeassistant.const import (CONF_PLATFORM, CONF_MONITORED_CONDITIONS, CONF_API_KEY, TEMP_FAHRENHEIT, TEMP_CELSIUS, @@ -57,10 +57,10 @@ SENSOR_TYPES = { PLATFORM_SCHEMA = vol.Schema({ vol.Required(CONF_PLATFORM): "wunderground", - vol.Required(CONF_API_KEY): string, - vol.Optional(CONF_PWS_ID): string, + vol.Required(CONF_API_KEY): cv.string, + vol.Optional(CONF_PWS_ID): cv.string, vol.Required(CONF_MONITORED_CONDITIONS, - default=[]): vol.All(ensure_list, [vol.In(SENSOR_TYPES)]), + default=[]): vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]), }) From 25840f97c258423a0c4d2dbdaa6aa7947e56d502 Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:46:04 -0600 Subject: [PATCH 09/13] Consistent use of WUnderground --- homeassistant/components/sensor/wunderground.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index 7d3084b47be..00e5fdb3e1d 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -1,5 +1,5 @@ """ -Support for Weather Underground weather service. +Support for WUnderground weather service. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.wunderground/ @@ -65,7 +65,7 @@ PLATFORM_SCHEMA = vol.Schema({ def setup_platform(hass, config, add_devices, discovery_info=None): - """Setup the Wunderground sensor.""" + """Setup the WUnderground sensor.""" rest = WUndergroundData(hass, config.get(CONF_API_KEY), config.get(CONF_PWS_ID, None)) @@ -85,7 +85,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class WUndergroundSensor(Entity): - """Implementing the Wunderground sensor.""" + """Implementing the WUnderground sensor.""" def __init__(self, rest, condition): """Initialize the sensor.""" @@ -124,7 +124,7 @@ class WUndergroundSensor(Entity): class WUndergroundData(object): - """Get data from Wundeground.""" + """Get data from WUnderground.""" def __init__(self, hass, api_key, pws_id=None): """Initialize the data object.""" @@ -146,7 +146,7 @@ class WUndergroundData(object): @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): - """Get the latest data from wunderground.""" + """Get the latest data from WUnderground.""" try: result = requests.get(self._build_url(), timeout=10).json() if "error" in result['response']: @@ -155,6 +155,6 @@ class WUndergroundData(object): else: self.data = result["current_observation"] except ValueError as err: - _LOGGER.error("Check Wunderground API %s", err.args) + _LOGGER.error("Check WUnderground API %s", err.args) self.data = None raise From 90449a90f1360f580b0a9d476c6d99cd1f182290 Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:46:24 -0600 Subject: [PATCH 10/13] Use string templating --- homeassistant/components/sensor/wunderground.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index 00e5fdb3e1d..e4fa30cabe3 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -138,7 +138,7 @@ class WUndergroundData(object): def _build_url(self): url = _RESOURCE.format(self._api_key) if self._pws_id: - url = url + 'pws:' + self._pws_id + url = url + 'pws:{}'.format(self._pws_id) else: url = url + '{},{}'.format(self._latitude, self._longitude) From 5bdcf60a214b84dbd9e81ea5b8d9db5c29deed11 Mon Sep 17 00:00:00 2001 From: "Teagan M. Glenn" Date: Thu, 18 Aug 2016 10:47:52 -0600 Subject: [PATCH 11/13] Extend platform schema --- homeassistant/components/sensor/wunderground.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index e4fa30cabe3..16672b03b4b 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -13,6 +13,7 @@ import voluptuous as vol from homeassistant.helpers.entity import Entity import homeassistant.helpers.config_validation as cv from homeassistant.util import Throttle +from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import (CONF_PLATFORM, CONF_MONITORED_CONDITIONS, CONF_API_KEY, TEMP_FAHRENHEIT, TEMP_CELSIUS, STATE_UNKNOWN) @@ -55,7 +56,7 @@ SENSOR_TYPES = { 'solarradiation': ['Solar Radiation', None] } -PLATFORM_SCHEMA = vol.Schema({ +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_PLATFORM): "wunderground", vol.Required(CONF_API_KEY): cv.string, vol.Optional(CONF_PWS_ID): cv.string, From cb3a37691f92104bbc0adc61c2972877a913275b Mon Sep 17 00:00:00 2001 From: Teagan Glenn Date: Thu, 18 Aug 2016 16:28:19 -0600 Subject: [PATCH 12/13] Type-o --- tests/components/sensor/test_wunderground.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/sensor/test_wunderground.py b/tests/components/sensor/test_wunderground.py index 15a903aec56..fccbbf3ca6c 100644 --- a/tests/components/sensor/test_wunderground.py +++ b/tests/components/sensor/test_wunderground.py @@ -122,7 +122,7 @@ class TestWundergroundSetup(unittest.TestCase): @unittest.mock.patch('requests.get', side_effect=mocked_requests_get) def test_sensor(self, req_mock): - """Test the wundergroun sensor class and methods.""" + """Test the wunderground sensor class and methods.""" wunderground.setup_platform(self.hass, VALID_CONFIG, self.add_devices, None) for device in self.DEVICES: From 297fca93512c2548638523f476e52d8ff8f83ea8 Mon Sep 17 00:00:00 2001 From: Teagan Glenn Date: Thu, 18 Aug 2016 16:39:16 -0600 Subject: [PATCH 13/13] Type-o --- tests/components/sensor/test_wunderground.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/components/sensor/test_wunderground.py b/tests/components/sensor/test_wunderground.py index fccbbf3ca6c..2de7da580f9 100644 --- a/tests/components/sensor/test_wunderground.py +++ b/tests/components/sensor/test_wunderground.py @@ -1,4 +1,4 @@ -"""The tests for the forecast.io platform.""" +"""The tests for the WUnderground platform.""" import unittest from homeassistant.components.sensor import wunderground @@ -78,7 +78,7 @@ def mocked_requests_get(*args, **kwargs): class TestWundergroundSetup(unittest.TestCase): - """Test the wunderground platform.""" + """Test the WUnderground platform.""" DEVICES = [] @@ -99,7 +99,7 @@ class TestWundergroundSetup(unittest.TestCase): @unittest.mock.patch('requests.get', side_effect=mocked_requests_get) def test_setup(self, req_mock): - """Test that the component is loaded if passed in PSW Id.""" + """Test that the component is loaded if passed in PWS Id.""" self.assertTrue( wunderground.setup_platform(self.hass, VALID_CONFIG_PWS, self.add_devices, None)) @@ -122,7 +122,7 @@ class TestWundergroundSetup(unittest.TestCase): @unittest.mock.patch('requests.get', side_effect=mocked_requests_get) def test_sensor(self, req_mock): - """Test the wunderground sensor class and methods.""" + """Test the WUnderground sensor class and methods.""" wunderground.setup_platform(self.hass, VALID_CONFIG, self.add_devices, None) for device in self.DEVICES: