core/homeassistant/components/waqi/sensor.py

180 lines
5.2 KiB
Python
Raw Normal View History

"""Support for the World Air Quality Index service."""
2017-07-07 14:55:58 +00:00
import asyncio
import logging
from datetime import timedelta
2017-07-07 14:55:58 +00:00
import aiohttp
import voluptuous as vol
from waqiasync import WaqiClient
2017-07-07 14:55:58 +00:00
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_ATTRIBUTION,
ATTR_TIME,
ATTR_TEMPERATURE,
CONF_TOKEN,
)
2017-07-07 14:55:58 +00:00
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_DOMINENTPOL = "dominentpol"
ATTR_HUMIDITY = "humidity"
ATTR_NITROGEN_DIOXIDE = "nitrogen_dioxide"
ATTR_OZONE = "ozone"
ATTR_PM10 = "pm_10"
ATTR_PM2_5 = "pm_2_5"
ATTR_PRESSURE = "pressure"
ATTR_SULFUR_DIOXIDE = "sulfur_dioxide"
2017-07-07 14:55:58 +00:00
KEY_TO_ATTR = {
2019-07-31 19:25:30 +00:00
"pm25": ATTR_PM2_5,
"pm10": ATTR_PM10,
"h": ATTR_HUMIDITY,
"p": ATTR_PRESSURE,
"t": ATTR_TEMPERATURE,
"o3": ATTR_OZONE,
"no2": ATTR_NITROGEN_DIOXIDE,
"so2": ATTR_SULFUR_DIOXIDE,
2017-07-07 14:55:58 +00:00
}
2019-07-31 19:25:30 +00:00
ATTRIBUTION = "Data provided by the World Air Quality Index project"
2019-07-31 19:25:30 +00:00
CONF_LOCATIONS = "locations"
CONF_STATIONS = "stations"
SCAN_INTERVAL = timedelta(minutes=5)
2017-07-07 14:55:58 +00:00
TIMEOUT = 10
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_STATIONS): cv.ensure_list,
vol.Required(CONF_TOKEN): cv.string,
vol.Required(CONF_LOCATIONS): cv.ensure_list,
}
)
2019-07-31 19:25:30 +00:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the requested World Air Quality Index locations."""
token = config.get(CONF_TOKEN)
station_filter = config.get(CONF_STATIONS)
locations = config.get(CONF_LOCATIONS)
client = WaqiClient(token, async_get_clientsession(hass), timeout=TIMEOUT)
dev = []
2017-07-07 14:55:58 +00:00
try:
for location_name in locations:
2018-10-01 06:55:43 +00:00
stations = await client.search(location_name)
2017-07-07 14:55:58 +00:00
_LOGGER.debug("The following stations were returned: %s", stations)
for station in stations:
waqi_sensor = WaqiSensor(client, station)
2019-07-31 19:25:30 +00:00
if not station_filter or {
waqi_sensor.uid,
waqi_sensor.url,
waqi_sensor.station_name,
} & set(station_filter):
2017-07-07 14:55:58 +00:00
dev.append(waqi_sensor)
2019-07-31 19:25:30 +00:00
except (aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError):
_LOGGER.exception("Failed to connect to WAQI servers.")
2017-07-07 14:55:58 +00:00
raise PlatformNotReady
async_add_entities(dev, True)
class WaqiSensor(Entity):
"""Implementation of a WAQI sensor."""
2017-07-07 14:55:58 +00:00
def __init__(self, client, station):
"""Initialize the sensor."""
2017-07-07 14:55:58 +00:00
self._client = client
try:
2019-07-31 19:25:30 +00:00
self.uid = station["uid"]
2017-07-07 14:55:58 +00:00
except (KeyError, TypeError):
self.uid = None
try:
2019-07-31 19:25:30 +00:00
self.url = station["station"]["url"]
except (KeyError, TypeError):
2017-07-07 14:55:58 +00:00
self.url = None
try:
2019-07-31 19:25:30 +00:00
self.station_name = station["station"]["name"]
except (KeyError, TypeError):
2017-07-07 14:55:58 +00:00
self.station_name = None
self._data = None
@property
def name(self):
"""Return the name of the sensor."""
if self.station_name:
return f"WAQI {self.station_name}"
2019-07-31 19:25:30 +00:00
return "WAQI {}".format(self.url if self.url else self.uid)
@property
def icon(self):
"""Icon to use in the frontend, if any."""
2019-07-31 19:25:30 +00:00
return "mdi:cloud"
@property
def state(self):
"""Return the state of the device."""
2017-07-07 14:55:58 +00:00
if self._data is not None:
2019-07-31 19:25:30 +00:00
return self._data.get("aqi")
return None
@property
def available(self):
"""Return sensor availability."""
return self._data is not None
@property
def unique_id(self):
"""Return unique ID."""
return self.uid
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
2019-07-31 19:25:30 +00:00
return "AQI"
@property
def device_state_attributes(self):
"""Return the state attributes of the last update."""
attrs = {}
2017-07-07 14:55:58 +00:00
if self._data is not None:
try:
2019-07-31 19:25:30 +00:00
attrs[ATTR_ATTRIBUTION] = " and ".join(
[ATTRIBUTION]
+ [v["name"] for v in self._data.get("attributions", [])]
)
2017-07-07 14:55:58 +00:00
2019-07-31 19:25:30 +00:00
attrs[ATTR_TIME] = self._data["time"]["s"]
attrs[ATTR_DOMINENTPOL] = self._data.get("dominentpol")
2017-07-07 14:55:58 +00:00
2019-07-31 19:25:30 +00:00
iaqi = self._data["iaqi"]
2017-07-07 14:55:58 +00:00
for key in iaqi:
if key in KEY_TO_ATTR:
2019-07-31 19:25:30 +00:00
attrs[KEY_TO_ATTR[key]] = iaqi[key]["v"]
2017-07-07 14:55:58 +00:00
else:
2019-07-31 19:25:30 +00:00
attrs[key] = iaqi[key]["v"]
return attrs
except (IndexError, KeyError):
return {ATTR_ATTRIBUTION: ATTRIBUTION}
2018-10-01 06:55:43 +00:00
async def async_update(self):
"""Get the latest data and updates the states."""
2017-07-07 14:55:58 +00:00
if self.uid:
2018-10-01 06:55:43 +00:00
result = await self._client.get_station_by_number(self.uid)
2017-07-07 14:55:58 +00:00
elif self.url:
2018-10-01 06:55:43 +00:00
result = await self._client.get_station_by_name(self.url)
2017-07-07 14:55:58 +00:00
else:
result = None
self._data = result