Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses issues with Pollen.com API troubles #12930

Merged
merged 2 commits into from Mar 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 52 additions & 32 deletions homeassistant/components/sensor/pollen.py
Expand Up @@ -105,7 +105,6 @@
'maximum': 12
}]


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ZIP_CODE): cv.string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For another PR: Would it make sense to validate this as just str ? That way it will enforce people to put quotes around their zip code in their config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a great idea! I'll open it up.

vol.Required(CONF_MONITORED_CONDITIONS):
Expand Down Expand Up @@ -211,21 +210,23 @@ def update(self):

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)
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']
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']
Expand All @@ -245,31 +246,51 @@ def update(self):

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'
Expand All @@ -289,8 +310,7 @@ def _get_client_data(self, module, operation):
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)
Expand Down