Преглед изворни кода

Add UniFi wifi key helper for guest mode integration

ZiB пре 4 месеци
родитељ
комит
d38d4649c7
1 измењених фајлова са 28 додато и 0 уклоњено
  1. 28 0
      custom_components/room_cal_ha/wifi.py

+ 28 - 0
custom_components/room_cal_ha/wifi.py

@@ -0,0 +1,28 @@
+"""UniFi WLAN key helper — looks up a WLAN by SSID and sets its password."""
+from __future__ import annotations
+
+import aiounifi
+from aiounifi.models.wlan import WlanChangePasswordRequest
+
+from homeassistant.core import HomeAssistant
+from homeassistant.exceptions import HomeAssistantError
+
+UNIFI_DOMAIN = "unifi"
+
+
+async def async_set_wifi_key(hass: HomeAssistant, ssid: str, key: str) -> None:
+  """Set the WPA2 password for a UniFi WLAN identified by its SSID name."""
+  for config_entry in hass.config_entries.async_loaded_entries(UNIFI_DOMAIN):
+    hub = config_entry.runtime_data
+    if not hub.available:
+      continue
+    for wlan_id, wlan in hub.api.wlans.items():
+      if wlan.name != ssid:
+        continue
+      try:
+        await hub.api.request(WlanChangePasswordRequest.create(wlan_id, key))
+      except aiounifi.AiounifiException as err:
+        raise HomeAssistantError(f"Failed to set WiFi key for '{ssid}': {err}") from err
+      return
+
+  raise HomeAssistantError(f"UniFi WLAN '{ssid}' not found in any loaded hub")