1 """The TP-Link Omada integration."""
3 from __future__
import annotations
5 from tplink_omada_client
import OmadaSite
6 from tplink_omada_client.devices
import OmadaListDevice
7 from tplink_omada_client.exceptions
import (
11 UnsupportedControllerVersion,
20 from .config_flow
import CONF_SITE, create_omada_client
21 from .const
import DOMAIN
22 from .controller
import OmadaSiteController
24 PLATFORMS: list[Platform] = [
25 Platform.BINARY_SENSOR,
26 Platform.DEVICE_TRACKER,
33 type OmadaConfigEntry = ConfigEntry[OmadaSiteController]
37 """Set up TP-Link Omada from a config entry."""
43 except (LoginFailed, UnsupportedControllerVersion)
as ex:
45 f
"Omada controller refused login attempt: {ex}"
47 except ConnectionFailed
as ex:
49 f
"Omada controller could not be reached: {ex}"
52 except OmadaClientException
as ex:
54 f
"Unexpected error connecting to Omada controller: {ex}"
57 site_client = await client.get_site_client(OmadaSite(
"", entry.data[CONF_SITE]))
59 await controller.initialize_first_refresh()
61 entry.runtime_data = controller
63 async
def handle_reconnect_client(call: ServiceCall) ->
None:
64 """Handle the service action call."""
65 mac: str |
None = call.data.get(
"mac")
69 await site_client.reconnect_client(mac)
71 hass.services.async_register(DOMAIN,
"reconnect_client", handle_reconnect_client)
75 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
81 """Unload a config entry."""
82 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
85 for entry
in hass.config_entries.async_entries(DOMAIN)
86 if entry.state == ConfigEntryState.LOADED
88 if len(loaded_entries) == 1:
90 hass.services.async_remove(DOMAIN,
"reconnect_client")
97 entry: OmadaConfigEntry,
98 omada_devices: dict[str, OmadaListDevice],
100 device_registry = dr.async_get(hass)
102 for registered_device
in device_registry.devices.get_devices_for_config_entry_id(
106 (i[1]
for i
in registered_device.identifiers
if i[0] == DOMAIN),
None
108 if mac
and mac
not in omada_devices:
109 device_registry.async_update_device(
110 registered_device.id, remove_config_entry_id=entry.entry_id
OmadaClient create_omada_client(HomeAssistant hass, MappingProxyType[str, Any] data)
bool async_setup_entry(HomeAssistant hass, OmadaConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, OmadaConfigEntry entry)
None _remove_old_devices(HomeAssistant hass, OmadaConfigEntry entry, dict[str, OmadaListDevice] omada_devices)