2019-10-16 10:06:52 +00:00
|
|
|
"""Support for the Airly air_quality service."""
|
2019-10-04 11:58:29 +00:00
|
|
|
from homeassistant.components.air_quality import (
|
|
|
|
ATTR_AQI,
|
|
|
|
ATTR_PM_2_5,
|
2019-12-08 12:41:51 +00:00
|
|
|
ATTR_PM_10,
|
|
|
|
AirQualityEntity,
|
2019-10-04 11:58:29 +00:00
|
|
|
)
|
2020-01-13 12:28:07 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2019-10-16 10:06:52 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_API_ADVICE,
|
|
|
|
ATTR_API_CAQI,
|
|
|
|
ATTR_API_CAQI_DESCRIPTION,
|
|
|
|
ATTR_API_CAQI_LEVEL,
|
|
|
|
ATTR_API_PM10,
|
|
|
|
ATTR_API_PM10_LIMIT,
|
|
|
|
ATTR_API_PM10_PERCENT,
|
|
|
|
ATTR_API_PM25,
|
|
|
|
ATTR_API_PM25_LIMIT,
|
|
|
|
ATTR_API_PM25_PERCENT,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2019-10-04 11:58:29 +00:00
|
|
|
|
|
|
|
ATTRIBUTION = "Data provided by Airly"
|
|
|
|
|
|
|
|
LABEL_ADVICE = "advice"
|
2020-03-25 18:13:28 +00:00
|
|
|
LABEL_AQI_DESCRIPTION = f"{ATTR_AQI}_description"
|
2019-10-04 11:58:29 +00:00
|
|
|
LABEL_AQI_LEVEL = f"{ATTR_AQI}_level"
|
|
|
|
LABEL_PM_2_5_LIMIT = f"{ATTR_PM_2_5}_limit"
|
|
|
|
LABEL_PM_2_5_PERCENT = f"{ATTR_PM_2_5}_percent_of_limit"
|
|
|
|
LABEL_PM_10_LIMIT = f"{ATTR_PM_10}_limit"
|
|
|
|
LABEL_PM_10_PERCENT = f"{ATTR_PM_10}_percent_of_limit"
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2019-10-16 10:06:52 +00:00
|
|
|
"""Set up Airly air_quality entity based on a config entry."""
|
2019-10-04 11:58:29 +00:00
|
|
|
name = config_entry.data[CONF_NAME]
|
|
|
|
|
2020-03-25 18:13:28 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2019-10-04 11:58:29 +00:00
|
|
|
|
2020-03-25 18:13:28 +00:00
|
|
|
async_add_entities(
|
|
|
|
[AirlyAirQuality(coordinator, name, config_entry.unique_id)], False
|
|
|
|
)
|
2019-10-04 11:58:29 +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
|
|
|
|
|
|
|
|
|
|
|
|
class AirlyAirQuality(AirQualityEntity):
|
2019-10-16 10:06:52 +00:00
|
|
|
"""Define an Airly air quality."""
|
2019-10-04 11:58:29 +00:00
|
|
|
|
2020-03-25 18:13:28 +00:00
|
|
|
def __init__(self, coordinator, name, unique_id):
|
2019-10-04 11:58:29 +00:00
|
|
|
"""Initialize."""
|
2020-03-25 18:13:28 +00:00
|
|
|
self.coordinator = coordinator
|
2019-10-04 11:58:29 +00:00
|
|
|
self._name = name
|
2019-12-19 13:10:27 +00:00
|
|
|
self._unique_id = unique_id
|
2019-10-04 11:58:29 +00:00
|
|
|
self._icon = "mdi:blur"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
|
|
|
return self._name
|
|
|
|
|
2020-03-25 18:13:28 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the polling requirement of the entity."""
|
|
|
|
return False
|
|
|
|
|
2019-10-04 11:58:29 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def air_quality_index(self):
|
|
|
|
"""Return the air quality index."""
|
2020-03-25 18:13:28 +00:00
|
|
|
return self.coordinator.data[ATTR_API_CAQI]
|
2019-10-04 11:58:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def particulate_matter_2_5(self):
|
|
|
|
"""Return the particulate matter 2.5 level."""
|
2020-03-25 18:13:28 +00:00
|
|
|
return self.coordinator.data[ATTR_API_PM25]
|
2019-10-04 11:58:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@round_state
|
|
|
|
def particulate_matter_10(self):
|
|
|
|
"""Return the particulate matter 10 level."""
|
2020-03-25 18:13:28 +00:00
|
|
|
return self.coordinator.data[ATTR_API_PM10]
|
2019-10-04 11:58:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def attribution(self):
|
|
|
|
"""Return the attribution."""
|
|
|
|
return ATTRIBUTION
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
2019-12-19 13:10:27 +00:00
|
|
|
return self._unique_id
|
2019-10-04 11:58:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
2020-03-25 18:13:28 +00:00
|
|
|
return self.coordinator.last_update_success
|
2019-10-04 11:58:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2020-03-25 18:13:28 +00:00
|
|
|
return {
|
|
|
|
LABEL_AQI_DESCRIPTION: self.coordinator.data[ATTR_API_CAQI_DESCRIPTION],
|
|
|
|
LABEL_ADVICE: self.coordinator.data[ATTR_API_ADVICE],
|
|
|
|
LABEL_AQI_LEVEL: self.coordinator.data[ATTR_API_CAQI_LEVEL],
|
|
|
|
LABEL_PM_2_5_LIMIT: self.coordinator.data[ATTR_API_PM25_LIMIT],
|
|
|
|
LABEL_PM_2_5_PERCENT: round(self.coordinator.data[ATTR_API_PM25_PERCENT]),
|
|
|
|
LABEL_PM_10_LIMIT: self.coordinator.data[ATTR_API_PM10_LIMIT],
|
|
|
|
LABEL_PM_10_PERCENT: round(self.coordinator.data[ATTR_API_PM10_PERCENT]),
|
|
|
|
}
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Connect to dispatcher listening for entity data notifications."""
|
2020-04-21 12:12:08 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
self.coordinator.async_add_listener(self.async_write_ha_state)
|
|
|
|
)
|
2019-10-04 11:58:29 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
2020-03-25 18:13:28 +00:00
|
|
|
"""Update Airly entity."""
|
|
|
|
await self.coordinator.async_request_refresh()
|