-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Adds allergy/disease sensor platform from Pollen.com #11573
Conversation
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.
Looks good overall. See comments below.
For more details about this platform, please refer to the documentation at | ||
https://home-assistant.io/components/sensor.pollen/ | ||
""" | ||
from logging import getLogger |
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.
Please import logging
like most modules do.
data.update() | ||
|
||
sensors = [] | ||
for condition in config.get(CONF_MONITORED_CONDITIONS, []): |
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.
This is a required key in the config schema so don't use dict.get
.
sensors = [] | ||
for condition in config.get(CONF_MONITORED_CONDITIONS, []): | ||
name, sensor_class, data_key, params, icon = CONDITIONS[condition] | ||
sensors.append(globals()[sensor_class]( |
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.
I don't think you need to use globals
. Is there a specific reason you want that?
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.
This so that I can dynamically instantiate objects of a certain type from the CONDITIONS
dict. Open to better suggestions!
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.
Can't you just call sensor_class
and instantiate? I think that should work.
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.
Sorry I wasn't clear. If you replace the string representing the class name in CONDITIONS
with the actual class name you could call it directly from here. You might have to move the conditions dict below the class defines though, so that might be uglier. You decide.
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.
Ah, I see. Putting CONDITIONS
at the bottom of the file feels confusing to me (given all other constants are at the top). Given that, are you okay with me keeping as-is?
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.
Yeah, I'm ok with it.
|
||
def average_of_list(list_of_nums, decimal_places=1): | ||
"""Return the average of a list of ints.""" | ||
return round(sum(list_of_nums, 0.0)/len(list_of_nums), decimal_places) |
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.
|
||
|
||
def calculate_trend(list_of_nums): | ||
"""Return the average of a list of ints.""" |
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.
Stale docstring.
|
||
def calculate_trend(list_of_nums): | ||
"""Return the average of a list of ints.""" | ||
ratings = list( |
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.
You can achieve the same with a two level nested generator expression and next
. That wouldn't be super readable either but I'd prefer that over this.
ratings = next((
r['label'] for n in list_of_nums
for r in RATING_MAPPING
if r['minimum'] <= n <= r['maximum']), None)
@property | ||
def device_state_attributes(self): | ||
"""Return the device state attributes.""" | ||
return merge_two_dicts( |
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 do you need this? Can't you just update the attributes?
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.
I'm merging common attributes of the base class (e.g. ATTR_ATTRIBUTION
) why (pseudo-)dynamic attributes of the subclasses. I've done this before; not good?
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 do you need to copy before updating? You can just do:
self._attr.update({...})
return self._attr
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.
Oh wow, silly on me. Thanks!
from pypollencom.exceptions import HTTPError | ||
|
||
try: | ||
self.extended_data = self._client.allergens.extended() |
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.
You could break out a function to access a client method inside a try except block, since you do the same thing 6 times.
|
||
@Throttle(MIN_TIME_UPDATE_INDICES) | ||
def update(self): | ||
"""Update with new AirVisual data.""" |
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.
Stale docstring.
|
||
@Throttle(MIN_TIME_UPDATE_INDICES) | ||
def update(self): | ||
"""Update with new AirVisual data.""" |
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.
Same as above.
06849e9
to
ddd7976
Compare
ddd7976
to
b47223a
Compare
@MartinHjelmare Need anything else from me on this one? |
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.
Looks great!
I'm getting data from
but not from
Anyone else experiencing this? |
@grantalewis Please open a new bug report and we'll be able to help debug. |
Description:
Adds a sensor platform for allergy and disease information from Pollen.com.
Related issue (if applicable): N/A
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4392
Example entry for
configuration.yaml
(if applicable):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
tox
run successfully. Your PR cannot be merged unless tests passREQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
..coveragerc
.