2019-12-31 12:05:31 +00:00
|
|
|
"""Support for the GIOS service."""
|
|
|
|
from homeassistant.components.air_quality import (
|
|
|
|
ATTR_CO,
|
|
|
|
ATTR_NO2,
|
|
|
|
ATTR_OZONE,
|
|
|
|
ATTR_PM_2_5,
|
|
|
|
ATTR_PM_10,
|
|
|
|
ATTR_SO2,
|
|
|
|
AirQualityEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.const import CONF_NAME
|
2020-08-30 14:37:03 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-12-31 12:05:31 +00:00
|
|
|
|
2020-08-05 10:47:33 +00:00
|
|
|
from .const import ATTR_STATION, DEFAULT_NAME, DOMAIN, ICONS_MAP, MANUFACTURER
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
ATTRIBUTION = "Data provided by GIOŚ"
|
2020-03-29 03:57:06 +00:00
|
|
|
|
|
|
|
SENSOR_MAP = {
|
|
|
|
"CO": ATTR_CO,
|
|
|
|
"NO2": ATTR_NO2,
|
|
|
|
"O3": ATTR_OZONE,
|
|
|
|
"PM10": ATTR_PM_10,
|
|
|
|
"PM2.5": ATTR_PM_2_5,
|
|
|
|
"SO2": ATTR_SO2,
|
|
|
|
}
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Add a GIOS entities from a config_entry."""
|
|
|
|
name = config_entry.data[CONF_NAME]
|
|
|
|
|
2020-03-29 03:57:06 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2019-12-31 12:05:31 +00:00
|
|
|
|
2020-03-29 03:57:06 +00:00
|
|
|
async_add_entities([GiosAirQuality(coordinator, name)], False)
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def round_state(func):
|
|
|
|
"""Round state."""
|
|
|
|
|
|
|
|
def _decorator(self):
|
|
|
|
res = func(self)
|
|
|
|
if isinstance(res, float):
|
|
|
|
return round(res)
|
|
|
|
return res
|
|
|
|
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
2020-08-30 14:37:03 +00:00
|
|
|
class GiosAirQuality(CoordinatorEntity, AirQualityEntity):
|
2019-12-31 12:05:31 +00:00
|
|
|
"""Define an GIOS sensor."""
|
|
|
|
|
2020-03-29 03:57:06 +00:00
|
|
|
def __init__(self, coordinator, name):
|
2019-12-31 12:05:31 +00:00
|
|
|
"""Initialize."""
|
2020-08-30 14:37:03 +00:00
|
|
|
super().__init__(coordinator)
|
2019-12-31 12:05:31 +00:00
|
|
|
self._name = name
|
|
|
|
self._attrs = {}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
2020-03-29 03:57:06 +00:00
|
|
|
if self.air_quality_index in ICONS_MAP:
|
|
|
|
return ICONS_MAP[self.air_quality_index]
|
2019-12-31 12:05:31 +00:00
|
|
|
return "mdi:blur"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def air_quality_index(self):
|
|
|
|
"""Return the air quality index."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self._get_sensor_value("AQI")
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def particulate_matter_2_5(self):
|
|
|
|
"""Return the particulate matter 2.5 level."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self._get_sensor_value("PM2.5")
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def particulate_matter_10(self):
|
|
|
|
"""Return the particulate matter 10 level."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self._get_sensor_value("PM10")
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def ozone(self):
|
|
|
|
"""Return the O3 (ozone) level."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self._get_sensor_value("O3")
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def carbon_monoxide(self):
|
|
|
|
"""Return the CO (carbon monoxide) level."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self._get_sensor_value("CO")
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def sulphur_dioxide(self):
|
|
|
|
"""Return the SO2 (sulphur dioxide) level."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self._get_sensor_value("SO2")
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def nitrogen_dioxide(self):
|
|
|
|
"""Return the NO2 (nitrogen dioxide) level."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self._get_sensor_value("NO2")
|
2019-12-31 12:05:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def attribution(self):
|
|
|
|
"""Return the attribution."""
|
|
|
|
return ATTRIBUTION
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
2020-03-29 03:57:06 +00:00
|
|
|
return self.coordinator.gios.station_id
|
|
|
|
|
2020-08-05 10:47:33 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return the device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, self.coordinator.gios.station_id)},
|
|
|
|
"name": DEFAULT_NAME,
|
|
|
|
"manufacturer": MANUFACTURER,
|
|
|
|
"entry_type": "service",
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:05:31 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2020-03-29 03:57:06 +00:00
|
|
|
# Different measuring stations have different sets of sensors. We don't know
|
|
|
|
# what data we will get.
|
|
|
|
for sensor in SENSOR_MAP:
|
|
|
|
if sensor in self.coordinator.data:
|
|
|
|
self._attrs[f"{SENSOR_MAP[sensor]}_index"] = self.coordinator.data[
|
|
|
|
sensor
|
|
|
|
]["index"]
|
|
|
|
self._attrs[ATTR_STATION] = self.coordinator.gios.station_name
|
2019-12-31 12:05:31 +00:00
|
|
|
return self._attrs
|
|
|
|
|
2020-03-29 03:57:06 +00:00
|
|
|
def _get_sensor_value(self, sensor):
|
|
|
|
"""Return value of specified sensor."""
|
|
|
|
if sensor in self.coordinator.data:
|
|
|
|
return self.coordinator.data[sensor]["value"]
|
|
|
|
return None
|