2019-10-16 10:06:52 +00:00
|
|
|
"""Support for the Airly sensor service."""
|
2021-05-07 14:47:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-06-10 17:32:41 +00:00
|
|
|
from typing import cast
|
2021-05-07 14:47:52 +00:00
|
|
|
|
2021-05-31 14:00:58 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorEntity
|
2021-05-07 14:47:52 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-05-20 09:34:32 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_ICON,
|
|
|
|
CONF_NAME,
|
|
|
|
)
|
2021-05-07 14:47:52 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-08-30 12:41:39 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-10-16 10:06:52 +00:00
|
|
|
|
2021-05-07 14:47:52 +00:00
|
|
|
from . import AirlyDataUpdateCoordinator
|
2019-10-16 10:06:52 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_API_PM1,
|
|
|
|
ATTR_API_PRESSURE,
|
2021-05-20 09:34:32 +00:00
|
|
|
ATTR_LABEL,
|
|
|
|
ATTR_UNIT,
|
2021-02-05 11:41:36 +00:00
|
|
|
ATTRIBUTION,
|
2020-08-05 10:55:14 +00:00
|
|
|
DEFAULT_NAME,
|
2019-10-16 10:06:52 +00:00
|
|
|
DOMAIN,
|
2020-08-05 10:55:14 +00:00
|
|
|
MANUFACTURER,
|
2021-05-07 14:47:52 +00:00
|
|
|
SENSOR_TYPES,
|
2019-10-16 10:06:52 +00:00
|
|
|
)
|
|
|
|
|
2020-07-30 18:41:18 +00:00
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
2021-05-07 14:47:52 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2019-10-16 10:06:52 +00:00
|
|
|
"""Set up Airly sensor entities based on a config entry."""
|
2021-05-07 14:47:52 +00:00
|
|
|
name = entry.data[CONF_NAME]
|
2019-10-16 10:06:52 +00:00
|
|
|
|
2021-05-07 14:47:52 +00:00
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
2019-10-16 10:06:52 +00:00
|
|
|
|
|
|
|
sensors = []
|
|
|
|
for sensor in SENSOR_TYPES:
|
2021-01-04 22:14:45 +00:00
|
|
|
# When we use the nearest method, we are not sure which sensors are available
|
|
|
|
if coordinator.data.get(sensor):
|
|
|
|
sensors.append(AirlySensor(coordinator, name, sensor))
|
2019-12-19 13:10:27 +00:00
|
|
|
|
2020-03-25 18:13:28 +00:00
|
|
|
async_add_entities(sensors, False)
|
2019-10-16 10:06:52 +00:00
|
|
|
|
|
|
|
|
2021-03-22 19:05:13 +00:00
|
|
|
class AirlySensor(CoordinatorEntity, SensorEntity):
|
2019-10-16 10:06:52 +00:00
|
|
|
"""Define an Airly sensor."""
|
|
|
|
|
2021-05-07 14:47:52 +00:00
|
|
|
coordinator: AirlyDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: AirlyDataUpdateCoordinator, name: str, kind: str
|
|
|
|
) -> None:
|
2019-10-16 10:06:52 +00:00
|
|
|
"""Initialize."""
|
2020-08-30 12:41:39 +00:00
|
|
|
super().__init__(coordinator)
|
2021-06-10 17:32:41 +00:00
|
|
|
description = SENSOR_TYPES[kind]
|
|
|
|
self._attr_device_class = description[ATTR_DEVICE_CLASS]
|
|
|
|
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
self._attr_icon = description[ATTR_ICON]
|
|
|
|
self._attr_name = f"{name} {description[ATTR_LABEL]}"
|
|
|
|
self._attr_state_class = description[ATTR_STATE_CLASS]
|
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{coordinator.latitude}-{coordinator.longitude}-{kind.lower()}"
|
|
|
|
)
|
|
|
|
self._attr_unit_of_measurement = description[ATTR_UNIT]
|
2019-10-16 10:06:52 +00:00
|
|
|
self.kind = kind
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
2021-05-07 14:47:52 +00:00
|
|
|
def state(self) -> StateType:
|
2019-10-16 10:06:52 +00:00
|
|
|
"""Return the state."""
|
2020-03-25 18:13:28 +00:00
|
|
|
self._state = self.coordinator.data[self.kind]
|
2019-10-16 10:06:52 +00:00
|
|
|
if self.kind in [ATTR_API_PM1, ATTR_API_PRESSURE]:
|
2021-05-07 14:47:52 +00:00
|
|
|
return round(cast(float, self._state))
|
|
|
|
return round(cast(float, self._state), 1)
|
2019-10-16 10:06:52 +00:00
|
|
|
|
2020-08-05 10:55:14 +00:00
|
|
|
@property
|
2021-05-07 14:47:52 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-08-05 10:55:14 +00:00
|
|
|
"""Return the device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {
|
2021-05-07 14:47:52 +00:00
|
|
|
(
|
|
|
|
DOMAIN,
|
2021-05-10 10:13:45 +00:00
|
|
|
f"{self.coordinator.latitude}-{self.coordinator.longitude}",
|
2021-05-07 14:47:52 +00:00
|
|
|
)
|
2020-08-05 10:55:14 +00:00
|
|
|
},
|
|
|
|
"name": DEFAULT_NAME,
|
|
|
|
"manufacturer": MANUFACTURER,
|
|
|
|
"entry_type": "service",
|
|
|
|
}
|