World Air Quality Index sensor (#4434)

* Implement WAQI sensor

* Corrections based on CI check.

* Updated requirements_all.txt for pwaqi==1.2

* Require latest version of pwaqi

* Fix lint: single argument for .exception and no more pass statement.

* Further lint fixes.

* pydocstyle fix

* Implement rate throttle.
Data on WAQI is usually updated once an hour - make it refresh every thirty minutes.

* Implement schema validation with voluptuous.
Change exception handling scope.
Move messages to debug().

* Fix lint (empty indented line).

* Sort lines correctly.

* Fix last lint issue.

* Provide additional sensor data as received from WAQI.
Easier-to-read throttle timing.

* Additional object attributes to be unrolled later.
This commit is contained in:
Valentin Alexeev 2016-11-23 08:59:27 +02:00 committed by Paulus Schoutsen
parent 3f9250415f
commit bb46009efa
3 changed files with 101 additions and 0 deletions

View File

@ -308,6 +308,7 @@ omit =
homeassistant/components/sensor/twitch.py
homeassistant/components/sensor/uber.py
homeassistant/components/sensor/vasttrafik.py
homeassistant/components/sensor/waqi.py
homeassistant/components/sensor/xbox_live.py
homeassistant/components/sensor/yweather.py
homeassistant/components/switch/acer_projector.py

View File

@ -0,0 +1,97 @@
"""
Support for the World Air Quality Index service.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.waqi/
"""
import logging
from datetime import timedelta
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers import config_validation as cv
import voluptuous as vol
REQUIREMENTS = ["pwaqi==1.2"]
_LOGGER = logging.getLogger(__name__)
SENSOR_TYPES = {
'aqi': ['AQI', '0-300+', 'mdi:cloud']
}
ATTR_LOCATION = 'locations'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(ATTR_LOCATION): cv.ensure_list
})
# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the requested World Air Quality Index locations."""
dev = []
import pwaqi
# Iterate each module
for location_name in config[ATTR_LOCATION]:
_LOGGER.debug('Adding location %s', location_name)
station_ids = pwaqi.findStationCodesByCity(location_name)
_LOGGER.debug('I got the following stations: %s', station_ids)
for station in station_ids:
dev.append(WaqiSensor(station))
add_devices(dev)
# pylint: disable=too-few-public-methods
class WaqiSensor(Entity):
"""Implementation of a WAQI sensor."""
def __init__(self, station_id):
"""Initialize the sensor."""
self._station_id = station_id
self._state = None
self.update()
@property
def name(self):
"""Return the name of the sensor."""
if 'city' in self._data:
return "WAQI {}".format(self._data['city']['name'])
return "WAQI {}".format(self._station_id)
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return "mdi:cloud"
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return "AQI"
@property
def state_attributes(self):
"""Return the state attributes of the last update."""
return {
"time": self._data.get('time', 'no data'),
"dominentpol": self._data.get('dominentpol', 'no data')
}
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the data from World Air Quality Index and updates the states."""
import pwaqi
try:
self._data = pwaqi.getStationObservation(self._station_id)
self._state = self._data.get('aqi', 'no data')
except KeyError:
_LOGGER.exception('Unable to fetch data from WAQI.')

View File

@ -330,6 +330,9 @@ pushbullet.py==0.10.0
# homeassistant.components.notify.pushetta
pushetta==1.0.15
# homeassistant.components.sensor.waqi
pwaqi==1.2
# homeassistant.components.sensor.cpuspeed
py-cpuinfo==0.2.3