Add Unifi device state for additional diagnostics (#105138)
* Add device state for additional diagnostics * Add state test and fix existing tests * Utilize IntEnum and dict for state lookup * Update aiounifi to v68
This commit is contained in:
parent
9c134c6b51
commit
1271f16385
7 changed files with 95 additions and 9 deletions
|
@ -36,6 +36,7 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .const import DEVICE_STATES
|
||||
from .controller import UniFiController
|
||||
from .entity import (
|
||||
HandlerT,
|
||||
|
@ -138,6 +139,12 @@ class UnifiSensorEntityDescriptionMixin(Generic[HandlerT, ApiItemT]):
|
|||
value_fn: Callable[[UniFiController, ApiItemT], datetime | float | str | None]
|
||||
|
||||
|
||||
@callback
|
||||
def async_device_state_value_fn(controller: UniFiController, device: Device) -> str:
|
||||
"""Retrieve the state of the device."""
|
||||
return DEVICE_STATES[device.state]
|
||||
|
||||
|
||||
@dataclass
|
||||
class UnifiSensorEntityDescription(
|
||||
SensorEntityDescription,
|
||||
|
@ -343,6 +350,25 @@ ENTITY_DESCRIPTIONS: tuple[UnifiSensorEntityDescription, ...] = (
|
|||
unique_id_fn=lambda controller, obj_id: f"device_temperature-{obj_id}",
|
||||
value_fn=lambda ctrlr, device: device.general_temperature,
|
||||
),
|
||||
UnifiSensorEntityDescription[Devices, Device](
|
||||
key="Device State",
|
||||
device_class=SensorDeviceClass.ENUM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
has_entity_name=True,
|
||||
allowed_fn=lambda controller, obj_id: True,
|
||||
api_handler_fn=lambda api: api.devices,
|
||||
available_fn=async_device_available_fn,
|
||||
device_info_fn=async_device_device_info_fn,
|
||||
event_is_on=None,
|
||||
event_to_subscribe=None,
|
||||
name_fn=lambda device: "State",
|
||||
object_fn=lambda api, obj_id: api.devices[obj_id],
|
||||
should_poll=False,
|
||||
supported_fn=lambda controller, obj_id: True,
|
||||
unique_id_fn=lambda controller, obj_id: f"device_state-{obj_id}",
|
||||
value_fn=async_device_state_value_fn,
|
||||
options=list(DEVICE_STATES.values()),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue