diff --git a/.coveragerc b/.coveragerc index 19c02da166d..0840e5bcb4d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/homeassistant/components/sensor/waqi.py b/homeassistant/components/sensor/waqi.py new file mode 100644 index 00000000000..3acb507f86d --- /dev/null +++ b/homeassistant/components/sensor/waqi.py @@ -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.') diff --git a/requirements_all.txt b/requirements_all.txt index d948e1958c2..c5257dd0dde 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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