From 4d2480bbd19220a6a7135debceb2a25652884632 Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Sun, 11 Dec 2016 18:43:42 -0500 Subject: [PATCH] Added support to language codes on Weather Underground (#4815) * Added supported to language codes to Weather Underground * Removed unecessary None assigments --- .../components/sensor/wunderground.py | 35 ++++++++++++++++--- tests/components/sensor/test_wunderground.py | 20 ++++++----- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/sensor/wunderground.py b/homeassistant/components/sensor/wunderground.py index 3194afbe94e..2f6558cd9da 100644 --- a/homeassistant/components/sensor/wunderground.py +++ b/homeassistant/components/sensor/wunderground.py @@ -19,12 +19,15 @@ from homeassistant.helpers.entity import Entity from homeassistant.util import Throttle import homeassistant.helpers.config_validation as cv -_RESOURCE = 'http://api.wunderground.com/api/{}/conditions/q/' -_ALERTS = 'http://api.wunderground.com/api/{}/alerts/q/' +_RESOURCE = 'http://api.wunderground.com/api/{}/conditions/{}/q/' +_ALERTS = 'http://api.wunderground.com/api/{}/alerts/{}/q/' _LOGGER = logging.getLogger(__name__) CONF_ATTRIBUTION = "Data provided by the WUnderground weather service" CONF_PWS_ID = 'pws_id' +CONF_LANG = 'lang' + +DEFAULT_LANG = 'EN' MIN_TIME_BETWEEN_UPDATES_ALERTS = timedelta(minutes=15) MIN_TIME_BETWEEN_UPDATES_OBSERVATION = timedelta(minutes=5) @@ -80,9 +83,29 @@ ALERTS_ATTRS = [ 'message', ] +# Language Supported Codes +LANG_CODES = [ + 'AF', 'AL', 'AR', 'HY', 'AZ', 'EU', + 'BY', 'BU', 'LI', 'MY', 'CA', 'CN', + 'TW', 'CR', 'CZ', 'DK', 'DV', 'NL', + 'EN', 'EO', 'ET', 'FA', 'FI', 'FR', + 'FC', 'GZ', 'DL', 'KA', 'GR', 'GU', + 'HT', 'IL', 'HI', 'HU', 'IS', 'IO', + 'ID', 'IR', 'IT', 'JP', 'JW', 'KM', + 'KR', 'KU', 'LA', 'LV', 'LT', 'ND', + 'MK', 'MT', 'GM', 'MI', 'MR', 'MN', + 'NO', 'OC', 'PS', 'GN', 'PL', 'BR', + 'PA', 'PU', 'RO', 'RU', 'SR', 'SK', + 'SL', 'SP', 'SI', 'SW', 'CH', 'TL', + 'TT', 'TH', 'UA', 'UZ', 'VU', 'CY', + 'SN', 'JI', 'YI', +] + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_API_KEY): cv.string, vol.Optional(CONF_PWS_ID): cv.string, + vol.Optional(CONF_LANG, default=DEFAULT_LANG): + vol.All(vol.In(LANG_CODES)), vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]), }) @@ -92,7 +115,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the WUnderground sensor.""" rest = WUndergroundData(hass, config.get(CONF_API_KEY), - config.get(CONF_PWS_ID, None)) + config.get(CONF_PWS_ID), + config.get(CONF_LANG)) sensors = [] for variable in config[CONF_MONITORED_CONDITIONS]: sensors.append(WUndergroundSensor(rest, variable)) @@ -192,18 +216,19 @@ class WUndergroundSensor(Entity): class WUndergroundData(object): """Get data from WUnderground.""" - def __init__(self, hass, api_key, pws_id=None): + def __init__(self, hass, api_key, pws_id, lang): """Initialize the data object.""" self._hass = hass self._api_key = api_key self._pws_id = pws_id + self._lang = 'lang:{}'.format(lang) self._latitude = hass.config.latitude self._longitude = hass.config.longitude self.data = None self.alerts = None def _build_url(self, baseurl=_RESOURCE): - url = baseurl.format(self._api_key) + url = baseurl.format(self._api_key, self._lang) if self._pws_id: url = url + 'pws:{}'.format(self._pws_id) else: diff --git a/tests/components/sensor/test_wunderground.py b/tests/components/sensor/test_wunderground.py index 05c7fc93921..7c92ac20424 100644 --- a/tests/components/sensor/test_wunderground.py +++ b/tests/components/sensor/test_wunderground.py @@ -23,6 +23,16 @@ VALID_CONFIG = { ] } +INVALID_CONFIG = { + 'platform': 'wunderground', + 'api_key': 'BOB', + 'pws_id': 'bar', + 'lang': 'foo', + 'monitored_conditions': [ + 'weather', 'feelslike_c', 'alerts' + ] +} + FEELS_LIKE = '40' WEATHER = 'Clear' HTTPS_ICON_URL = 'https://icons.wxug.com/i/c/k/clear.gif' @@ -128,17 +138,9 @@ class TestWundergroundSetup(unittest.TestCase): self.assertTrue( wunderground.setup_platform(self.hass, VALID_CONFIG, self.add_devices, None)) - invalid_config = { - 'platform': 'wunderground', - 'api_key': 'BOB', - 'pws_id': 'bar', - 'monitored_conditions': [ - 'weather', 'feelslike_c', 'alerts' - ] - } self.assertTrue( - wunderground.setup_platform(self.hass, invalid_config, + wunderground.setup_platform(self.hass, INVALID_CONFIG, self.add_devices, None)) @unittest.mock.patch('requests.get', side_effect=mocked_requests_get)