Make connected stations coordinator a dict in devolo Home Network (#147042)

This commit is contained in:
Guido Schmitz 2025-07-05 11:42:15 +02:00 committed by GitHub
parent 2ea09ff37a
commit 8d82e34ba5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 28 deletions

View File

@ -207,7 +207,7 @@ class DevoloUptimeGetCoordinator(DevoloDataUpdateCoordinator[int]):
class DevoloWifiConnectedStationsGetCoordinator( class DevoloWifiConnectedStationsGetCoordinator(
DevoloDataUpdateCoordinator[list[ConnectedStationInfo]] DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]]
): ):
"""Class to manage fetching data from the WifiGuestAccessGet endpoint.""" """Class to manage fetching data from the WifiGuestAccessGet endpoint."""
@ -230,10 +230,11 @@ class DevoloWifiConnectedStationsGetCoordinator(
) )
self.update_method = self.async_get_wifi_connected_station self.update_method = self.async_get_wifi_connected_station
async def async_get_wifi_connected_station(self) -> list[ConnectedStationInfo]: async def async_get_wifi_connected_station(self) -> dict[str, ConnectedStationInfo]:
"""Fetch data from API endpoint.""" """Fetch data from API endpoint."""
assert self.device.device assert self.device.device
return await self.device.device.async_get_wifi_connected_station() clients = await self.device.device.async_get_wifi_connected_station()
return {client.mac_address: client for client in clients}
class DevoloWifiGuestAccessGetCoordinator( class DevoloWifiGuestAccessGetCoordinator(

View File

@ -28,9 +28,9 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Get all devices and sensors and setup them via config entry.""" """Get all devices and sensors and setup them via config entry."""
device = entry.runtime_data.device device = entry.runtime_data.device
coordinators: dict[str, DevoloDataUpdateCoordinator[list[ConnectedStationInfo]]] = ( coordinators: dict[
entry.runtime_data.coordinators str, DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]]
) ] = entry.runtime_data.coordinators
registry = er.async_get(hass) registry = er.async_get(hass)
tracked = set() tracked = set()
@ -38,16 +38,16 @@ async def async_setup_entry(
def new_device_callback() -> None: def new_device_callback() -> None:
"""Add new devices if needed.""" """Add new devices if needed."""
new_entities = [] new_entities = []
for station in coordinators[CONNECTED_WIFI_CLIENTS].data: for mac_address in coordinators[CONNECTED_WIFI_CLIENTS].data:
if station.mac_address in tracked: if mac_address in tracked:
continue continue
new_entities.append( new_entities.append(
DevoloScannerEntity( DevoloScannerEntity(
coordinators[CONNECTED_WIFI_CLIENTS], device, station.mac_address coordinators[CONNECTED_WIFI_CLIENTS], device, mac_address
) )
) )
tracked.add(station.mac_address) tracked.add(mac_address)
async_add_entities(new_entities) async_add_entities(new_entities)
@callback @callback
@ -82,7 +82,7 @@ async def async_setup_entry(
# The pylint disable is needed because of https://github.com/pylint-dev/pylint/issues/9138 # The pylint disable is needed because of https://github.com/pylint-dev/pylint/issues/9138
class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module
CoordinatorEntity[DevoloDataUpdateCoordinator[list[ConnectedStationInfo]]], CoordinatorEntity[DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]]],
ScannerEntity, ScannerEntity,
): ):
"""Representation of a devolo device tracker.""" """Representation of a devolo device tracker."""
@ -92,7 +92,7 @@ class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module
def __init__( def __init__(
self, self,
coordinator: DevoloDataUpdateCoordinator[list[ConnectedStationInfo]], coordinator: DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]],
device: Device, device: Device,
mac: str, mac: str,
) -> None: ) -> None:
@ -109,14 +109,8 @@ class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module
if not self.coordinator.data: if not self.coordinator.data:
return {} return {}
station = next( assert self.mac_address
( station = self.coordinator.data.get(self.mac_address)
station
for station in self.coordinator.data
if station.mac_address == self.mac_address
),
None,
)
if station: if station:
attrs["wifi"] = WIFI_APTYPE.get(station.vap_type, STATE_UNKNOWN) attrs["wifi"] = WIFI_APTYPE.get(station.vap_type, STATE_UNKNOWN)
attrs["band"] = ( attrs["band"] = (
@ -129,11 +123,8 @@ class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module
@property @property
def is_connected(self) -> bool: def is_connected(self) -> bool:
"""Return true if the device is connected to the network.""" """Return true if the device is connected to the network."""
return any( assert self.mac_address
station return self.coordinator.data.get(self.mac_address) is not None
for station in self.coordinator.data
if station.mac_address == self.mac_address
)
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:

View File

@ -21,7 +21,7 @@ from .coordinator import DevoloDataUpdateCoordinator, DevoloHomeNetworkConfigEnt
type _DataType = ( type _DataType = (
LogicalNetwork LogicalNetwork
| DataRate | DataRate
| list[ConnectedStationInfo] | dict[str, ConnectedStationInfo]
| list[NeighborAPInfo] | list[NeighborAPInfo]
| WifiGuestAccessGet | WifiGuestAccessGet
| bool | bool

View File

@ -47,7 +47,11 @@ def _last_restart(runtime: int) -> datetime:
type _CoordinatorDataType = ( type _CoordinatorDataType = (
LogicalNetwork | DataRate | list[ConnectedStationInfo] | list[NeighborAPInfo] | int LogicalNetwork
| DataRate
| dict[str, ConnectedStationInfo]
| list[NeighborAPInfo]
| int
) )
type _SensorDataType = int | float | datetime type _SensorDataType = int | float | datetime
@ -79,7 +83,7 @@ SENSOR_TYPES: dict[str, DevoloSensorEntityDescription[Any, Any]] = {
), ),
), ),
CONNECTED_WIFI_CLIENTS: DevoloSensorEntityDescription[ CONNECTED_WIFI_CLIENTS: DevoloSensorEntityDescription[
list[ConnectedStationInfo], int dict[str, ConnectedStationInfo], int
]( ](
key=CONNECTED_WIFI_CLIENTS, key=CONNECTED_WIFI_CLIENTS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,