2021-05-07 13:59:29 +00:00
|
|
|
"""Support for the Nettigo Air Monitor service."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-23 22:25:48 +00:00
|
|
|
from datetime import datetime, timedelta
|
2021-06-22 13:44:53 +00:00
|
|
|
import logging
|
|
|
|
from typing import cast
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DOMAIN as PLATFORM,
|
|
|
|
SensorEntity,
|
2021-07-27 10:12:15 +00:00
|
|
|
SensorEntityDescription,
|
2021-06-22 13:44:53 +00:00
|
|
|
)
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-06-22 13:44:53 +00:00
|
|
|
from homeassistant.helpers import entity_registry
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-06-22 13:44:53 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2021-05-19 11:20:11 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
from . import NAMDataUpdateCoordinator
|
2021-07-27 10:12:15 +00:00
|
|
|
from .const import ATTR_UPTIME, DOMAIN, MIGRATION_SENSORS, SENSORS
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
2021-06-22 13:44:53 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Add a Nettigo Air Monitor entities from a config_entry."""
|
2021-05-19 11:20:11 +00:00
|
|
|
coordinator: NAMDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2021-06-25 10:07:32 +00:00
|
|
|
# Due to the change of the attribute name of two sensors, it is necessary to migrate
|
2021-06-22 13:44:53 +00:00
|
|
|
# the unique_ids to the new names.
|
|
|
|
ent_reg = entity_registry.async_get(hass)
|
|
|
|
for old_sensor, new_sensor in MIGRATION_SENSORS:
|
|
|
|
old_unique_id = f"{coordinator.unique_id}-{old_sensor}"
|
|
|
|
new_unique_id = f"{coordinator.unique_id}-{new_sensor}"
|
|
|
|
if entity_id := ent_reg.async_get_entity_id(PLATFORM, DOMAIN, old_unique_id):
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Migrating entity %s from old unique ID '%s' to new unique ID '%s'",
|
|
|
|
entity_id,
|
|
|
|
old_unique_id,
|
|
|
|
new_unique_id,
|
|
|
|
)
|
|
|
|
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
|
|
|
|
|
2021-05-19 11:20:11 +00:00
|
|
|
sensors: list[NAMSensor | NAMSensorUptime] = []
|
2021-07-27 10:12:15 +00:00
|
|
|
for description in SENSORS:
|
|
|
|
if getattr(coordinator.data, description.key) is not None:
|
|
|
|
if description.key == ATTR_UPTIME:
|
|
|
|
sensors.append(NAMSensorUptime(coordinator, description))
|
2021-05-19 11:20:11 +00:00
|
|
|
else:
|
2021-07-27 10:12:15 +00:00
|
|
|
sensors.append(NAMSensor(coordinator, description))
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
async_add_entities(sensors, False)
|
|
|
|
|
|
|
|
|
2022-03-21 13:14:46 +00:00
|
|
|
class NAMSensor(CoordinatorEntity[NAMDataUpdateCoordinator], SensorEntity):
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Define an Nettigo Air Monitor sensor."""
|
|
|
|
|
2021-07-27 10:12:15 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: NAMDataUpdateCoordinator,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(coordinator)
|
2021-06-10 12:42:28 +00:00
|
|
|
self._attr_device_info = coordinator.device_info
|
2021-07-27 10:12:15 +00:00
|
|
|
self._attr_unique_id = f"{coordinator.unique_id}-{description.key}"
|
|
|
|
self.entity_description = description
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-23 22:25:48 +00:00
|
|
|
def native_value(self) -> StateType | datetime:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Return the state."""
|
2021-07-27 10:12:15 +00:00
|
|
|
return cast(
|
|
|
|
StateType, getattr(self.coordinator.data, self.entity_description.key)
|
|
|
|
)
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if entity is available."""
|
|
|
|
available = super().available
|
|
|
|
|
|
|
|
# For a short time after booting, the device does not return values for all
|
|
|
|
# sensors. For this reason, we mark entities for which data is missing as
|
|
|
|
# unavailable.
|
2021-06-22 13:44:53 +00:00
|
|
|
return (
|
2021-07-27 10:12:15 +00:00
|
|
|
available
|
|
|
|
and getattr(self.coordinator.data, self.entity_description.key) is not None
|
2021-05-07 13:59:29 +00:00
|
|
|
)
|
2021-05-19 11:20:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NAMSensorUptime(NAMSensor):
|
|
|
|
"""Define an Nettigo Air Monitor uptime sensor."""
|
|
|
|
|
|
|
|
@property
|
2021-11-23 22:25:48 +00:00
|
|
|
def native_value(self) -> datetime:
|
2021-05-19 11:20:11 +00:00
|
|
|
"""Return the state."""
|
2021-07-27 10:12:15 +00:00
|
|
|
uptime_sec = getattr(self.coordinator.data, self.entity_description.key)
|
2021-11-23 22:25:48 +00:00
|
|
|
return utcnow() - timedelta(seconds=uptime_sec)
|