Skip to content

Adds the AirVisual air quality sensor platform#9320

Merged
MartinHjelmare merged 9 commits into
home-assistant:devfrom
bachya:airvisual
Sep 8, 2017
Merged

Adds the AirVisual air quality sensor platform#9320
MartinHjelmare merged 9 commits into
home-assistant:devfrom
bachya:airvisual

Conversation

@bachya
Copy link
Copy Markdown
Contributor

@bachya bachya commented Sep 6, 2017

Description:

Adds a sensor platform for AirVisual air quality and pollution data.

Related issue (if applicable): N/A

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#3321

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: airvisual
    api_key: abc123
    monitored_conditions:
      - us
      - cn
    latitude: 42.81212
    longitude: 108.12422
    radius: 500

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:
- [ ] Local tests with tox run successfully. Your PR cannot be merged unless tests pass
- [ ] Tests have been added to verify that the new code works.

@property
def pollution_info(self):
"""Define a property to access the pollution information."""
return self._pollution_info # pylint: disable=syntax-error
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

at least two spaces before inline comment

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

In general looks very good I think. Some comments below.

@@ -0,0 +1,304 @@
"""Define sensors for AirVisual air quality information."""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Look at another sensor platform (eg DHT) for our default way of writing the module docstring. It should contain a link to the docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do!

ATTR_COUNTRY = 'country'
ATTR_POLLUTANT_SYMBOL = 'pollutant_symbol'
ATTR_POLLUTANT_UNIT = 'pollutant_unit'
ATTR_STATE = 'state'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Import from const.py.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was under the impression that const.py was for generally-useful constants, and that attributes applying to certain platforms only should exist in those platform files. I could see country and state being generally-useful, but what about the two pollutant ones?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're correct, you shouldn't add non general constants to const.py. I'm only talking about ATTR_STATE which already exists in const.py.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, got it! Will do!

@property
def state(self):
"""Define a property to access the state."""
return self._state
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider making all these properties regular instance attributes instead, since you don't seem to need any more logic than returning the attribute.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do!

if self._data:
return merge_two_dicts(super().device_state_attributes, {
ATTR_POLLUTANT_SYMBOL: self._symbol,
ATTR_POLLUTANT_UNIT: self._unit
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't you want to have a unit of measure for these sensors? Why not overwrite the unit_of_measurement property?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see you're working on that now. 😉

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would love your thoughts here:

  • The Air Quality Index doesn't really have a unit; I want to add something so I get a nice graph, but not sure what it would be.
  • The Air Quality Level doesn't have a unit, since its values are strings like "Good".
  • Same for the Main Pollutant.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe use 'PSI' as unit for the pollution index?

Copy link
Copy Markdown
Contributor Author

@bachya bachya Sep 8, 2017

Choose a reason for hiding this comment

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

PSI isn't quite right, as we're not specifically measuring pressure. Sadly, the EPA specifically states that AQI:

Ranges from 0 to 500 (no units)

Check out my latest push; not sure if you'll approve, but if I return an empty string for that sensor's unit_of_measurement property, I get the bar chart that I want and the unit_of_measurement attribute is the technically correct value: nothing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was referring to pollutant standard index:
https://en.m.wikipedia.org/wiki/Pollutant_Standards_Index

An empty string could be OK, if we make that standard for all unit less measurements that should have a line chart.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

@MartinHjelmare MartinHjelmare added this to the 0.53 milestone Sep 8, 2017
@MartinHjelmare MartinHjelmare merged commit d1ef473 into home-assistant:dev Sep 8, 2017
@bachya bachya deleted the airvisual branch September 8, 2017 15:01
balloob pushed a commit that referenced this pull request Sep 9, 2017
* Adds the AirVisual air quality sensor platform

* Updated .coveragerc

* Removed some un-needed code

* Adding strangely-necessary pylint disable

* Removing a Python3.5-specific dict combiner method

* Restarting stuck coverage test

* Added units to AQI sensor (to get nice graph)

* Making collaborator-requested changes

* Removing unnecessary parameter from data object
@balloob balloob mentioned this pull request Sep 9, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Dec 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants