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

Use luftdaten module #10970

Merged
merged 5 commits into from
Dec 12, 2017
Merged

Use luftdaten module #10970

merged 5 commits into from
Dec 12, 2017

Conversation

fabaff
Copy link
Member

@fabaff fabaff commented Dec 5, 2017

Description:

  • Use luftdaten for retrieving the data
  • Add attribution, attributes (incl. coordinates) and pressure
  • Give warning if initially not data is available
  • Make default name shorter

Related issue (if applicable): fixes #10951

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

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: luftdaten
    sensorid: 6401
    monitored_conditions:
      - temperature
      - humidity

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][ex-requir]).
  • New dependencies are only imported inside functions that use them ([example][ex-import]).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.

from luftdaten import Luftdaten

self.hass = hass
self._websession = async_get_clientsession(self.hass)
Copy link
Member

Choose a reason for hiding this comment

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

hass.helpers.aiohttp_client.async_get_clientsession()

Copy link
Member Author

Choose a reason for hiding this comment

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

See line 16.

try:
yield from self.data.async_get_data()
except LuftdatenError:
_LOGGER.error("Unable to retrieve data from luftdaten.info")
self.data = None
Copy link
Member

@MartinHjelmare MartinHjelmare Dec 6, 2017

Choose a reason for hiding this comment

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

Shouldn't it be able to retry at next update?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, would make sense.


rest_client = LuftdatenData(resource, verify_ssl)
rest_client.update()
yield from luftdaten.async_get()
Copy link
Member

Choose a reason for hiding this comment

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

There's no method in the data class by that name.

Copy link
Member

Choose a reason for hiding this comment

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

My mistake, see below.

@property
def device_state_attributes(self):
"""Return the state attributes."""
if self.luftdaten is None:
Copy link
Member

Choose a reason for hiding this comment

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

This can't happen.

Copy link
Member Author

Choose a reason for hiding this comment

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

True.

def async_update(self):
"""Get the latest data from luftdaten.info and update the state."""
try:
yield from self.luftdaten.async_get()
Copy link
Member

Choose a reason for hiding this comment

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

Wrong method call.

Copy link
Member Author

Choose a reason for hiding this comment

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

What would the correct call be?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I missed that you had changed the name, and I assumed the data class also would have a method called update or async_update since that is what we usually use.

Nevermind this comment. 😊

Copy link
Member Author

Choose a reason for hiding this comment

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

I will rename it to be consistent with the other platforms.

"""Get the latest data from luftdaten.info and update the state."""
try:
yield from self.luftdaten.async_update()
except TypeError:
Copy link
Member

Choose a reason for hiding this comment

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

When could this error happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

All sensors are under the control of their owner and if one decided to changing something it can happen that several update windows of the Luftdate API are missed. There are sensors which are not properly implemented and only send partial data. Also, there is no way to tell if the sensor is temporary offline or permanent.

This can be removed if the Luftdaten API is declared stable and the module implements more error handling.

return self._state
try:
return self.luftdaten.data.values[self.sensor_type]
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

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

When could this error happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems to be obsolete with the latest changes.

Copy link
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.

Looks good!

@fabaff
Copy link
Member Author

fabaff commented Dec 12, 2017

Breaking change:
The luftdaten sensor was migrated to use the third-party asyncio module luftdaten which interacts with the (Luftdaten API)[https://github.com/opendata-stuttgart/meta/wiki/APIs#api-luftdateninfo]. The option to use an URL of a random API endpoint was removed for now.

@fabaff fabaff merged commit ed06b8c into dev Dec 12, 2017
@fabaff fabaff deleted the luftdaten branch December 12, 2017 07:09
akatrevorjay added a commit to akatrevorjay/home-assistant that referenced this pull request Dec 15, 2017
…into dev

* 'dev' of https://github.com/home-assistant/home-assistant:
  Report availability for TP-Link smart bulbs (home-assistant#10976)
  Bump pyatv to 0.3.9 (home-assistant#11104)
  Use luftdaten module (home-assistant#10970)
  Bump pymusiccast to version 0.1.6 (home-assistant#11091)
  Update tellcore-net to 0.4 (home-assistant#11087)
  Upgrade shodan to 1.7.7 (home-assistant#11084)
  Upgrade youtube_dl to 2017.12.10 (home-assistant#11080)
  Upgrade psutil to 5.4.2 (home-assistant#11083)
  Upgrade yarl to 0.16.0 (home-assistant#11078)
  Upgrade aiohttp to 2.3.6 (home-assistant#11079)
  Allow tradfri to read the available state of the device (home-assistant#11056)
@fabaff fabaff mentioned this pull request Dec 16, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Mar 30, 2018
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.

Luftdaten
5 participants