diff --git a/homeassistant/components/sensor/pollen.py b/homeassistant/components/sensor/pollen.py index 98252eb6f06..d299a74b450 100644 --- a/homeassistant/components/sensor/pollen.py +++ b/homeassistant/components/sensor/pollen.py @@ -105,7 +105,6 @@ RATING_MAPPING = [{ 'maximum': 12 }] - PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_ZIP_CODE): cv.string, vol.Required(CONF_MONITORED_CONDITIONS): @@ -211,21 +210,23 @@ class AllergyAverageSensor(BaseSensor): try: data_attr = getattr(self.data, self._data_params['data_attr']) - indices = [ - p['Index'] - for p in data_attr['Location']['periods'] - ] + indices = [p['Index'] for p in data_attr['Location']['periods']] + self._attrs[ATTR_TREND] = calculate_trend(indices) except KeyError: _LOGGER.error("Pollen.com API didn't return any data") return + try: + self._attrs[ATTR_CITY] = data_attr['Location']['City'].title() + self._attrs[ATTR_STATE] = data_attr['Location']['State'] + self._attrs[ATTR_ZIP_CODE] = data_attr['Location']['ZIP'] + except KeyError: + _LOGGER.debug('Location data not included in API response') + self._attrs[ATTR_CITY] = None + self._attrs[ATTR_STATE] = None + self._attrs[ATTR_ZIP_CODE] = None + average = round(mean(indices), 1) - - self._attrs[ATTR_TREND] = calculate_trend(indices) - self._attrs[ATTR_CITY] = data_attr['Location']['City'].title() - self._attrs[ATTR_STATE] = data_attr['Location']['State'] - self._attrs[ATTR_ZIP_CODE] = data_attr['Location']['ZIP'] - [rating] = [ i['label'] for i in RATING_MAPPING if i['minimum'] <= average <= i['maximum'] @@ -245,31 +246,51 @@ class AllergyIndexSensor(BaseSensor): try: location_data = self.data.current_data['Location'] + [period] = [ + p for p in location_data['periods'] + if p['Type'] == self._data_params['key'] + ] + [rating] = [ + i['label'] for i in RATING_MAPPING + if i['minimum'] <= period['Index'] <= i['maximum'] + ] + self._attrs[ATTR_ALLERGEN_GENUS] = period['Triggers'][0]['Genus'] + self._attrs[ATTR_ALLERGEN_NAME] = period['Triggers'][0]['Name'] + self._attrs[ATTR_ALLERGEN_TYPE] = period['Triggers'][0][ + 'PlantType'] + self._attrs[ATTR_RATING] = rating + except KeyError: _LOGGER.error("Pollen.com API didn't return any data") return - [period] = [ - p for p in location_data['periods'] - if p['Type'] == self._data_params['key'] - ] + try: + self._attrs[ATTR_CITY] = location_data['City'].title() + self._attrs[ATTR_STATE] = location_data['State'] + self._attrs[ATTR_ZIP_CODE] = location_data['ZIP'] + except KeyError: + _LOGGER.debug('Location data not included in API response') + self._attrs[ATTR_CITY] = None + self._attrs[ATTR_STATE] = None + self._attrs[ATTR_ZIP_CODE] = None - self._attrs[ATTR_ALLERGEN_GENUS] = period['Triggers'][0]['Genus'] - self._attrs[ATTR_ALLERGEN_NAME] = period['Triggers'][0]['Name'] - self._attrs[ATTR_ALLERGEN_TYPE] = period['Triggers'][0]['PlantType'] - self._attrs[ATTR_OUTLOOK] = self.data.outlook_data['Outlook'] - self._attrs[ATTR_SEASON] = self.data.outlook_data['Season'] - self._attrs[ATTR_TREND] = self.data.outlook_data[ - 'Trend'].title() - self._attrs[ATTR_CITY] = location_data['City'].title() - self._attrs[ATTR_STATE] = location_data['State'] - self._attrs[ATTR_ZIP_CODE] = location_data['ZIP'] + try: + self._attrs[ATTR_OUTLOOK] = self.data.outlook_data['Outlook'] + except KeyError: + _LOGGER.debug('Outlook data not included in API response') + self._attrs[ATTR_OUTLOOK] = None - [rating] = [ - i['label'] for i in RATING_MAPPING - if i['minimum'] <= period['Index'] <= i['maximum'] - ] - self._attrs[ATTR_RATING] = rating + try: + self._attrs[ATTR_SEASON] = self.data.outlook_data['Season'] + except KeyError: + _LOGGER.debug('Season data not included in API response') + self._attrs[ATTR_SEASON] = None + + try: + self._attrs[ATTR_TREND] = self.data.outlook_data['Trend'].title() + except KeyError: + _LOGGER.debug('Trend data not included in API response') + self._attrs[ATTR_TREND] = None self._state = period['Index'] self._unit = 'index' @@ -289,8 +310,7 @@ class DataBase(object): data = {} try: data = getattr(getattr(self._client, module), operation)() - _LOGGER.debug('Received "%s_%s" data: %s', module, - operation, data) + _LOGGER.debug('Received "%s_%s" data: %s', module, operation, data) except HTTPError as exc: _LOGGER.error('An error occurred while retrieving data') _LOGGER.debug(exc)