Use SensorEntityDescription in Nettigo Air Monitor (#53539)
This commit is contained in:
parent
c0e84a7b32
commit
ca020e1f87
3 changed files with 166 additions and 215 deletions
|
@ -6,12 +6,11 @@ import logging
|
|||
from typing import cast
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
ATTR_STATE_CLASS,
|
||||
DOMAIN as PLATFORM,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ICON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -20,15 +19,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from . import NAMDataUpdateCoordinator
|
||||
from .const import (
|
||||
ATTR_ENABLED,
|
||||
ATTR_LABEL,
|
||||
ATTR_UNIT,
|
||||
ATTR_UPTIME,
|
||||
DOMAIN,
|
||||
MIGRATION_SENSORS,
|
||||
SENSORS,
|
||||
)
|
||||
from .const import ATTR_UPTIME, DOMAIN, MIGRATION_SENSORS, SENSORS
|
||||
|
||||
PARALLEL_UPDATES = 1
|
||||
|
||||
|
@ -57,12 +48,12 @@ async def async_setup_entry(
|
|||
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
|
||||
|
||||
sensors: list[NAMSensor | NAMSensorUptime] = []
|
||||
for sensor in SENSORS:
|
||||
if getattr(coordinator.data, sensor) is not None:
|
||||
if sensor == ATTR_UPTIME:
|
||||
sensors.append(NAMSensorUptime(coordinator, sensor))
|
||||
for description in SENSORS:
|
||||
if getattr(coordinator.data, description.key) is not None:
|
||||
if description.key == ATTR_UPTIME:
|
||||
sensors.append(NAMSensorUptime(coordinator, description))
|
||||
else:
|
||||
sensors.append(NAMSensor(coordinator, sensor))
|
||||
sensors.append(NAMSensor(coordinator, description))
|
||||
|
||||
async_add_entities(sensors, False)
|
||||
|
||||
|
@ -72,24 +63,23 @@ class NAMSensor(CoordinatorEntity, SensorEntity):
|
|||
|
||||
coordinator: NAMDataUpdateCoordinator
|
||||
|
||||
def __init__(self, coordinator: NAMDataUpdateCoordinator, sensor_type: str) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NAMDataUpdateCoordinator,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
description = SENSORS[sensor_type]
|
||||
self._attr_device_class = description[ATTR_DEVICE_CLASS]
|
||||
self._attr_device_info = coordinator.device_info
|
||||
self._attr_entity_registry_enabled_default = description[ATTR_ENABLED]
|
||||
self._attr_icon = description[ATTR_ICON]
|
||||
self._attr_name = description[ATTR_LABEL]
|
||||
self._attr_state_class = description[ATTR_STATE_CLASS]
|
||||
self._attr_unique_id = f"{coordinator.unique_id}-{sensor_type}"
|
||||
self._attr_unit_of_measurement = description[ATTR_UNIT]
|
||||
self.sensor_type = sensor_type
|
||||
self._attr_unique_id = f"{coordinator.unique_id}-{description.key}"
|
||||
self.entity_description = description
|
||||
|
||||
@property
|
||||
def state(self) -> StateType:
|
||||
"""Return the state."""
|
||||
return cast(StateType, getattr(self.coordinator.data, self.sensor_type))
|
||||
return cast(
|
||||
StateType, getattr(self.coordinator.data, self.entity_description.key)
|
||||
)
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
|
@ -100,7 +90,8 @@ class NAMSensor(CoordinatorEntity, SensorEntity):
|
|||
# sensors. For this reason, we mark entities for which data is missing as
|
||||
# unavailable.
|
||||
return (
|
||||
available and getattr(self.coordinator.data, self.sensor_type) is not None
|
||||
available
|
||||
and getattr(self.coordinator.data, self.entity_description.key) is not None
|
||||
)
|
||||
|
||||
|
||||
|
@ -110,7 +101,7 @@ class NAMSensorUptime(NAMSensor):
|
|||
@property
|
||||
def state(self) -> str:
|
||||
"""Return the state."""
|
||||
uptime_sec = getattr(self.coordinator.data, self.sensor_type)
|
||||
uptime_sec = getattr(self.coordinator.data, self.entity_description.key)
|
||||
return (
|
||||
(utcnow() - timedelta(seconds=uptime_sec))
|
||||
.replace(microsecond=0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue