Add UniFi sensor for number of clients connected to a device (#119509)
Co-authored-by: Kim de Vos <kim.de.vos@vippsmobilepay.com>
This commit is contained in:
parent
4fb8202de1
commit
707e422a31
2 changed files with 142 additions and 0 deletions
|
@ -108,6 +108,27 @@ def async_wlan_client_value_fn(hub: UnifiHub, wlan: Wlan) -> int:
|
|||
)
|
||||
|
||||
|
||||
@callback
|
||||
def async_device_clients_value_fn(hub: UnifiHub, device: Device) -> int:
|
||||
"""Calculate the amount of clients connected to a device."""
|
||||
|
||||
return len(
|
||||
[
|
||||
client.mac
|
||||
for client in hub.api.clients.values()
|
||||
if (
|
||||
(
|
||||
client.access_point_mac != ""
|
||||
and client.access_point_mac == device.mac
|
||||
)
|
||||
or (client.access_point_mac == "" and client.switch_mac == device.mac)
|
||||
)
|
||||
and dt_util.utcnow() - dt_util.utc_from_timestamp(client.last_seen or 0)
|
||||
< hub.config.option_detection_time
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def async_device_uptime_value_fn(hub: UnifiHub, device: Device) -> datetime | None:
|
||||
"""Calculate the approximate time the device started (based on uptime returned from API, in seconds)."""
|
||||
|
@ -302,6 +323,20 @@ ENTITY_DESCRIPTIONS: tuple[UnifiSensorEntityDescription, ...] = (
|
|||
unique_id_fn=lambda hub, obj_id: f"wlan_clients-{obj_id}",
|
||||
value_fn=async_wlan_client_value_fn,
|
||||
),
|
||||
UnifiSensorEntityDescription[Devices, Device](
|
||||
key="Device clients",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_registry_enabled_default=False,
|
||||
api_handler_fn=lambda api: api.devices,
|
||||
available_fn=async_device_available_fn,
|
||||
device_info_fn=async_device_device_info_fn,
|
||||
name_fn=lambda device: "Clients",
|
||||
object_fn=lambda api, obj_id: api.devices[obj_id],
|
||||
should_poll=True,
|
||||
unique_id_fn=lambda hub, obj_id: f"device_clients-{obj_id}",
|
||||
value_fn=async_device_clients_value_fn,
|
||||
),
|
||||
UnifiSensorEntityDescription[Outlets, Outlet](
|
||||
key="Outlet power metering",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue