Addresses issues with Pollen.com API troubles (#12930)

* Addresses issues with Pollen.com API troubles (#12916)

* Reverted some unnecessary style changes
This commit is contained in:
Aaron Bach 2018-03-05 15:15:54 -07:00 committed by Paulus Schoutsen
parent 60d7e32f81
commit 18b288dcfe

View file

@ -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
average = round(mean(indices), 1)
self._attrs[ATTR_TREND] = calculate_trend(indices)
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)
[rating] = [
i['label'] for i in RATING_MAPPING
if i['minimum'] <= average <= i['maximum']
@ -245,32 +246,52 @@ class AllergyIndexSensor(BaseSensor):
try:
location_data = self.data.current_data['Location']
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']
]
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']
[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
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
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
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)