Home Assistant Unofficial Reference 2024.12.1
controller.py
Go to the documentation of this file.
1 """Controller for sharing Omada API coordinators between platforms."""
2 
3 from tplink_omada_client import OmadaSiteClient
4 from tplink_omada_client.devices import OmadaSwitch
5 
6 from homeassistant.core import HomeAssistant
7 
8 from .coordinator import (
9  OmadaClientsCoordinator,
10  OmadaDevicesCoordinator,
11  OmadaGatewayCoordinator,
12  OmadaSwitchPortCoordinator,
13 )
14 
15 
17  """Controller for the Omada SDN site."""
18 
19  _gateway_coordinator: OmadaGatewayCoordinator | None = None
20 
21  def __init__(
22  self,
23  hass: HomeAssistant,
24  omada_client: OmadaSiteClient,
25  ) -> None:
26  """Create the controller."""
27  self._hass_hass = hass
28  self._omada_client_omada_client = omada_client
29 
30  self._switch_port_coordinators: dict[str, OmadaSwitchPortCoordinator] = {}
31  self._devices_coordinator_devices_coordinator = OmadaDevicesCoordinator(hass, omada_client)
32  self._clients_coordinator_clients_coordinator = OmadaClientsCoordinator(hass, omada_client)
33 
34  async def initialize_first_refresh(self) -> None:
35  """Initialize the all coordinators, and perform first refresh."""
36  await self._devices_coordinator_devices_coordinator.async_config_entry_first_refresh()
37 
38  devices = self._devices_coordinator_devices_coordinator.data.values()
39  gateway = next((d for d in devices if d.type == "gateway"), None)
40  if gateway:
41  self._gateway_coordinator_gateway_coordinator = OmadaGatewayCoordinator(
42  self._hass_hass, self._omada_client_omada_client, gateway.mac
43  )
44  await self._gateway_coordinator_gateway_coordinator.async_config_entry_first_refresh()
45 
46  await self.clients_coordinatorclients_coordinator.async_config_entry_first_refresh()
47 
48  @property
49  def omada_client(self) -> OmadaSiteClient:
50  """Get the connected client API for the site to manage."""
51  return self._omada_client_omada_client
52 
54  self, switch: OmadaSwitch
55  ) -> OmadaSwitchPortCoordinator:
56  """Get coordinator for network port information of a given switch."""
57  if switch.mac not in self._switch_port_coordinators:
58  self._switch_port_coordinators[switch.mac] = OmadaSwitchPortCoordinator(
59  self._hass_hass, self._omada_client_omada_client, switch
60  )
61 
62  return self._switch_port_coordinators[switch.mac]
63 
64  @property
65  def gateway_coordinator(self) -> OmadaGatewayCoordinator | None:
66  """Gets the coordinator for site's gateway, or None if there is no gateway."""
67  return self._gateway_coordinator_gateway_coordinator
68 
69  @property
70  def devices_coordinator(self) -> OmadaDevicesCoordinator:
71  """Gets the coordinator for site's devices."""
72  return self._devices_coordinator_devices_coordinator
73 
74  @property
75  def clients_coordinator(self) -> OmadaClientsCoordinator:
76  """Gets the coordinator for site's clients."""
77  return self._clients_coordinator_clients_coordinator