-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Benadryl Social Pollen Count Sensor #24403
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """The pollen_benadryl component.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "domain": "pollen_benadryl", | ||
| "name": "Benadryl Social Pollen Count", | ||
| "documentation": "https://www.home-assistant.io/components/pollen_benadryl", | ||
| "requirements": [], | ||
| "dependencies": [], | ||
| "codeowners": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| """Support for Pollen.""" | ||
| from datetime import timedelta | ||
| import logging | ||
| import requests | ||
| from requests.exceptions import HTTPError | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import (CONF_NAME) | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| from homeassistant.helpers.entity import Entity | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEFAULT_NAME = 'Pollen Count' | ||
|
|
||
| ATTR_DATE_TAKEN = 'date_taken' | ||
|
|
||
| SCAN_INTERVAL = timedelta(minutes=10) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Optional(CONF_NAME): cv.string | ||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_entities, discovery_info=None): | ||
| """Set up the Pollen sensor platform.""" | ||
| if None in (hass.config.latitude, hass.config.longitude): | ||
| _LOGGER.error( | ||
| "Latitude/Longitude has not been set in the Home Assistant config") | ||
| return False | ||
|
|
||
| add_entities([PollenSensor(PollenData( | ||
| config.get(CONF_NAME), | ||
| hass.config.latitude, | ||
| hass.config.longitude | ||
| ))], True) | ||
|
|
||
|
|
||
| class PollenSensor(Entity): | ||
| """Representation of the Pollen sensor.""" | ||
|
|
||
| def __init__(self, pollen_data): | ||
| """Initialize the Pollen sensor.""" | ||
| self._name = None | ||
| self._state = None | ||
| self._available = False | ||
| self._date_taken = None | ||
| self._pollen_data = pollen_data | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the sensor.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def available(self): | ||
| """Return True if entity is available.""" | ||
| return self._available | ||
|
|
||
| @property | ||
| def device_state_attributes(self): | ||
| """Return the state attributes.""" | ||
| return { | ||
| ATTR_DATE_TAKEN: self._date_taken | ||
| } | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Return the icon to use in the frontend.""" | ||
| return 'mdi:flower' | ||
|
|
||
| def update(self): | ||
| """Collect updated data from Pollen API.""" | ||
| self._pollen_data.update() | ||
|
|
||
| self._name = self._pollen_data.name | ||
| self._state = self._pollen_data.count | ||
| self._available = self._pollen_data.avaliable | ||
| self._date_taken = self._pollen_data.date_taken | ||
|
|
||
|
|
||
| class PollenData(): | ||
| """Pollen Data object.""" | ||
|
|
||
| def __init__(self, name, latitude, longitude): | ||
| """Set up Pollen Sensor.""" | ||
| if name is not None: | ||
| self.name = name | ||
| else: | ||
| self.name = DEFAULT_NAME | ||
| self._url = "https://socialpollencount.co.uk{}[{},{}]".format( | ||
| "/api/forecast?location=", latitude, longitude) | ||
| self.count = None | ||
| self.date_taken = None | ||
| self.avaliable = None | ||
|
|
||
| def update(self): | ||
| """Update Pollen Sensor.""" | ||
| try: | ||
| response = requests.get(self._url) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally don't want to use requests anymore (#23685) - Could you try converting this to aiohttp (API is mostly the same, and there are plenty of examples in this repo) |
||
| response.raise_for_status() | ||
|
|
||
| json = response.json() | ||
|
|
||
| self.count = json["forecast"][0]["pollen_count"] | ||
| self.date_taken = json["forecast"][0]["date"] | ||
|
|
||
| self.avaliable = True | ||
|
|
||
| except HTTPError as http_err: | ||
| self.avaliable = False | ||
| _LOGGER.error('A HTTP error occurred: %s', http_err) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why pipe the name to
PollenDataand not to the entity? That way you wouldn't have to fetch it from PollenData each time you call update (which is kinda ugly)