diff --git a/homeassistant/components/unifi/__init__.py b/homeassistant/components/unifi/__init__.py index 086bae8d8cf..84540f7bea4 100644 --- a/homeassistant/components/unifi/__init__.py +++ b/homeassistant/components/unifi/__init__.py @@ -9,8 +9,13 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers.storage import Store from homeassistant.helpers.typing import ConfigType -from .const import CONF_CONTROLLER, DOMAIN as UNIFI_DOMAIN, UNIFI_WIRELESS_CLIENTS -from .controller import PLATFORMS, UniFiController, get_unifi_controller +from .const import ( + CONF_CONTROLLER, + DOMAIN as UNIFI_DOMAIN, + PLATFORMS, + UNIFI_WIRELESS_CLIENTS, +) +from .controller import UniFiController, get_unifi_controller from .errors import AuthenticationRequired, CannotConnect from .services import async_setup_services, async_unload_services diff --git a/homeassistant/components/unifi/const.py b/homeassistant/components/unifi/const.py index 406b8d23a18..e84e2795a71 100644 --- a/homeassistant/components/unifi/const.py +++ b/homeassistant/components/unifi/const.py @@ -1,9 +1,19 @@ """Constants for the UniFi Network integration.""" + import logging +from homeassistant.const import Platform + LOGGER = logging.getLogger(__package__) DOMAIN = "unifi" +PLATFORMS = [ + Platform.DEVICE_TRACKER, + Platform.SENSOR, + Platform.SWITCH, + Platform.UPDATE, +] + CONF_CONTROLLER = "controller" CONF_SITE_ID = "site" diff --git a/homeassistant/components/unifi/controller.py b/homeassistant/components/unifi/controller.py index 7446d6abbff..6ad8e416445 100644 --- a/homeassistant/components/unifi/controller.py +++ b/homeassistant/components/unifi/controller.py @@ -14,18 +14,9 @@ from aiounifi.controller import ( DATA_DPI_GROUP, DATA_DPI_GROUP_REMOVED, DATA_EVENT, - SIGNAL_CONNECTION_STATE, - SIGNAL_DATA, ) -from aiounifi.events import ( - ACCESS_POINT_CONNECTED, - GATEWAY_CONNECTED, - SWITCH_CONNECTED, - WIRED_CLIENT_CONNECTED, - WIRELESS_CLIENT_CONNECTED, - WIRELESS_GUEST_CONNECTED, -) -from aiounifi.websocket import STATE_DISCONNECTED, STATE_RUNNING +from aiounifi.models.event import EventKey +from aiounifi.websocket import WebsocketSignal, WebsocketState import async_timeout from homeassistant.config_entries import ConfigEntry @@ -74,6 +65,7 @@ from .const import ( DEFAULT_TRACK_WIRED_CLIENTS, DOMAIN as UNIFI_DOMAIN, LOGGER, + PLATFORMS, UNIFI_WIRELESS_CLIENTS, ) from .errors import AuthenticationRequired, CannotConnect @@ -81,17 +73,16 @@ from .switch import BLOCK_SWITCH, POE_SWITCH RETRY_TIMER = 15 CHECK_HEARTBEAT_INTERVAL = timedelta(seconds=1) -PLATFORMS = [Platform.DEVICE_TRACKER, Platform.SENSOR, Platform.SWITCH, Platform.UPDATE] CLIENT_CONNECTED = ( - WIRED_CLIENT_CONNECTED, - WIRELESS_CLIENT_CONNECTED, - WIRELESS_GUEST_CONNECTED, + EventKey.WIRED_CLIENT_CONNECTED, + EventKey.WIRELESS_CLIENT_CONNECTED, + EventKey.WIRELESS_GUEST_CONNECTED, ) DEVICE_CONNECTED = ( - ACCESS_POINT_CONNECTED, - GATEWAY_CONNECTED, - SWITCH_CONNECTED, + EventKey.ACCESS_POINT_CONNECTED, + EventKey.GATEWAY_CONNECTED, + EventKey.SWITCH_CONNECTED, ) @@ -204,15 +195,15 @@ class UniFiController: @callback def async_unifi_signalling_callback(self, signal, data): """Handle messages back from UniFi library.""" - if signal == SIGNAL_CONNECTION_STATE: + if signal == WebsocketSignal.CONNECTION_STATE: - if data == STATE_DISCONNECTED and self.available: + if data == WebsocketState.DISCONNECTED and self.available: LOGGER.warning("Lost connection to UniFi Network") - if (data == STATE_RUNNING and not self.available) or ( - data == STATE_DISCONNECTED and self.available + if (data == WebsocketState.RUNNING and not self.available) or ( + data == WebsocketState.DISCONNECTED and self.available ): - self.available = data == STATE_RUNNING + self.available = data == WebsocketState.RUNNING async_dispatcher_send(self.hass, self.signal_reachable) if not self.available: @@ -220,7 +211,7 @@ class UniFiController: else: LOGGER.info("Connected to UniFi Network") - elif signal == SIGNAL_DATA and data: + elif signal == WebsocketSignal.DATA and data: if DATA_EVENT in data: clients_connected = set() @@ -229,16 +220,16 @@ class UniFiController: for event in data[DATA_EVENT]: - if event.event in CLIENT_CONNECTED: + if event.key in CLIENT_CONNECTED: clients_connected.add(event.mac) - if not wireless_clients_connected and event.event in ( - WIRELESS_CLIENT_CONNECTED, - WIRELESS_GUEST_CONNECTED, + if not wireless_clients_connected and event.key in ( + EventKey.WIRELESS_CLIENT_CONNECTED, + EventKey.WIRELESS_GUEST_CONNECTED, ): wireless_clients_connected = True - elif event.event in DEVICE_CONNECTED: + elif event.key in DEVICE_CONNECTED: devices_connected.add(event.mac) if wireless_clients_connected: diff --git a/homeassistant/components/unifi/device_tracker.py b/homeassistant/components/unifi/device_tracker.py index fc9c41ce184..2ae7f2c394f 100644 --- a/homeassistant/components/unifi/device_tracker.py +++ b/homeassistant/components/unifi/device_tracker.py @@ -3,19 +3,8 @@ from datetime import timedelta import logging -from aiounifi.api import SOURCE_DATA, SOURCE_EVENT -from aiounifi.events import ( - ACCESS_POINT_UPGRADED, - GATEWAY_UPGRADED, - SWITCH_UPGRADED, - WIRED_CLIENT_CONNECTED, - WIRELESS_CLIENT_CONNECTED, - WIRELESS_CLIENT_ROAM, - WIRELESS_CLIENT_ROAMRADIO, - WIRELESS_GUEST_CONNECTED, - WIRELESS_GUEST_ROAM, - WIRELESS_GUEST_ROAMRADIO, -) +from aiounifi.interfaces.api_handlers import SOURCE_DATA, SOURCE_EVENT +from aiounifi.models.event import EventKey from homeassistant.components.device_tracker import DOMAIN, SourceType from homeassistant.components.device_tracker.config_entry import ScannerEntity @@ -60,16 +49,14 @@ CLIENT_STATIC_ATTRIBUTES = [ CLIENT_CONNECTED_ALL_ATTRIBUTES = CLIENT_CONNECTED_ATTRIBUTES + CLIENT_STATIC_ATTRIBUTES -DEVICE_UPGRADED = (ACCESS_POINT_UPGRADED, GATEWAY_UPGRADED, SWITCH_UPGRADED) - -WIRED_CONNECTION = (WIRED_CLIENT_CONNECTED,) +WIRED_CONNECTION = (EventKey.WIRED_CLIENT_CONNECTED,) WIRELESS_CONNECTION = ( - WIRELESS_CLIENT_CONNECTED, - WIRELESS_CLIENT_ROAM, - WIRELESS_CLIENT_ROAMRADIO, - WIRELESS_GUEST_CONNECTED, - WIRELESS_GUEST_ROAM, - WIRELESS_GUEST_ROAMRADIO, + EventKey.WIRELESS_CLIENT_CONNECTED, + EventKey.WIRELESS_CLIENT_ROAM, + EventKey.WIRELESS_CLIENT_ROAMRADIO, + EventKey.WIRELESS_GUEST_CONNECTED, + EventKey.WIRELESS_GUEST_ROAM, + EventKey.WIRELESS_GUEST_ROAMRADIO, ) @@ -233,8 +220,8 @@ class UniFiClientTracker(UniFiClientBase, ScannerEntity): and not self._only_listen_to_data_source ): - if (self.is_wired and self.client.event.event in WIRED_CONNECTION) or ( - not self.is_wired and self.client.event.event in WIRELESS_CONNECTION + if (self.is_wired and self.client.event.key in WIRED_CONNECTION) or ( + not self.is_wired and self.client.event.key in WIRELESS_CONNECTION ): self._is_connected = True self.schedule_update = False diff --git a/homeassistant/components/unifi/manifest.json b/homeassistant/components/unifi/manifest.json index 36186b6fed8..9e8a1ef28f3 100644 --- a/homeassistant/components/unifi/manifest.json +++ b/homeassistant/components/unifi/manifest.json @@ -3,7 +3,7 @@ "name": "UniFi Network", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/unifi", - "requirements": ["aiounifi==34"], + "requirements": ["aiounifi==35"], "codeowners": ["@Kane610"], "quality_scale": "platinum", "ssdp": [ diff --git a/homeassistant/components/unifi/services.py b/homeassistant/components/unifi/services.py index fcde48528b3..4574a27016d 100644 --- a/homeassistant/components/unifi/services.py +++ b/homeassistant/components/unifi/services.py @@ -1,5 +1,6 @@ """UniFi Network services.""" +from aiounifi.models.client import ClientReconnectRequest, ClientRemoveRequest import voluptuous as vol from homeassistant.const import ATTR_DEVICE_ID @@ -77,7 +78,7 @@ async def async_reconnect_client(hass, data) -> None: ): continue - await controller.api.clients.reconnect(mac) + await controller.api.request(ClientReconnectRequest.create(mac)) async def async_remove_clients(hass, data) -> None: @@ -109,4 +110,4 @@ async def async_remove_clients(hass, data) -> None: clients_to_remove.append(client.mac) if clients_to_remove: - await controller.api.clients.remove_clients(macs=clients_to_remove) + await controller.api.request(ClientRemoveRequest.create(clients_to_remove)) diff --git a/homeassistant/components/unifi/switch.py b/homeassistant/components/unifi/switch.py index b9f23c31392..72083cdc219 100644 --- a/homeassistant/components/unifi/switch.py +++ b/homeassistant/components/unifi/switch.py @@ -8,13 +8,14 @@ Support for controlling deep packet inspection (DPI) restriction groups. import asyncio from typing import Any -from aiounifi.api import SOURCE_EVENT -from aiounifi.events import ( - WIRED_CLIENT_BLOCKED, - WIRED_CLIENT_UNBLOCKED, - WIRELESS_CLIENT_BLOCKED, - WIRELESS_CLIENT_UNBLOCKED, +from aiounifi.interfaces.api_handlers import SOURCE_EVENT +from aiounifi.models.client import ClientBlockRequest +from aiounifi.models.device import ( + DeviceSetOutletRelayRequest, + DeviceSetPoePortModeRequest, ) +from aiounifi.models.dpi_restriction_app import DPIRestrictionAppEnableRequest +from aiounifi.models.event import EventKey from homeassistant.components.switch import DOMAIN, SwitchDeviceClass, SwitchEntity from homeassistant.config_entries import ConfigEntry @@ -39,8 +40,8 @@ DPI_SWITCH = "dpi" POE_SWITCH = "poe" OUTLET_SWITCH = "outlet" -CLIENT_BLOCKED = (WIRED_CLIENT_BLOCKED, WIRELESS_CLIENT_BLOCKED) -CLIENT_UNBLOCKED = (WIRED_CLIENT_UNBLOCKED, WIRELESS_CLIENT_UNBLOCKED) +CLIENT_BLOCKED = (EventKey.WIRED_CLIENT_BLOCKED, EventKey.WIRELESS_CLIENT_BLOCKED) +CLIENT_UNBLOCKED = (EventKey.WIRED_CLIENT_UNBLOCKED, EventKey.WIRELESS_CLIENT_UNBLOCKED) async def async_setup_entry( @@ -272,11 +273,19 @@ class UniFiPOEClientSwitch(UniFiClient, SwitchEntity, RestoreEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Enable POE for client.""" - await self.device.set_port_poe_mode(self.client.switch_port, self.poe_mode) + await self.controller.api.request( + DeviceSetPoePortModeRequest.create( + self.device, self.client.switch_port, self.poe_mode + ) + ) async def async_turn_off(self, **kwargs: Any) -> None: """Disable POE for client.""" - await self.device.set_port_poe_mode(self.client.switch_port, "off") + await self.controller.api.request( + DeviceSetPoePortModeRequest.create( + self.device, self.client.switch_port, "off" + ) + ) @property def extra_state_attributes(self): @@ -324,9 +333,9 @@ class UniFiBlockClientSwitch(UniFiClient, SwitchEntity): """Update the clients state.""" if ( self.client.last_updated == SOURCE_EVENT - and self.client.event.event in CLIENT_BLOCKED + CLIENT_UNBLOCKED + and self.client.event.key in CLIENT_BLOCKED + CLIENT_UNBLOCKED ): - self._is_blocked = self.client.event.event in CLIENT_BLOCKED + self._is_blocked = self.client.event.key in CLIENT_BLOCKED super().async_update_callback() @@ -337,11 +346,15 @@ class UniFiBlockClientSwitch(UniFiClient, SwitchEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn on connectivity for client.""" - await self.controller.api.clients.unblock(self.client.mac) + await self.controller.api.request( + ClientBlockRequest.create(self.client.mac, False) + ) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off connectivity for client.""" - await self.controller.api.clients.block(self.client.mac) + await self.controller.api.request( + ClientBlockRequest.create(self.client.mac, True) + ) @property def icon(self) -> str: @@ -449,7 +462,9 @@ class UniFiDPIRestrictionSwitch(UniFiBase, SwitchEntity): """Restrict access of apps related to DPI group.""" return await asyncio.gather( *[ - self.controller.api.dpi_apps.enable(app_id) + self.controller.api.request( + DPIRestrictionAppEnableRequest.create(app_id, True) + ) for app_id in self._item.dpiapp_ids ] ) @@ -458,7 +473,9 @@ class UniFiDPIRestrictionSwitch(UniFiBase, SwitchEntity): """Remove restriction of apps related to DPI group.""" return await asyncio.gather( *[ - self.controller.api.dpi_apps.disable(app_id) + self.controller.api.request( + DPIRestrictionAppEnableRequest.create(app_id, False) + ) for app_id in self._item.dpiapp_ids ] ) @@ -509,11 +526,15 @@ class UniFiOutletSwitch(UniFiBase, SwitchEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Enable outlet relay.""" - await self._item.set_outlet_relay_state(self._outlet_index, True) + await self.controller.api.request( + DeviceSetOutletRelayRequest.create(self._item, self._outlet_index, True) + ) async def async_turn_off(self, **kwargs: Any) -> None: """Disable outlet relay.""" - await self._item.set_outlet_relay_state(self._outlet_index, False) + await self.controller.api.request( + DeviceSetOutletRelayRequest.create(self._item, self._outlet_index, False) + ) @property def device_info(self) -> DeviceInfo: diff --git a/homeassistant/components/unifi/update.py b/homeassistant/components/unifi/update.py index 24967e043d9..ecfbe3549bf 100644 --- a/homeassistant/components/unifi/update.py +++ b/homeassistant/components/unifi/update.py @@ -4,6 +4,8 @@ from __future__ import annotations import logging from typing import Any +from aiounifi.models.device import DeviceUpgradeRequest + from homeassistant.components.update import ( DOMAIN, UpdateDeviceClass, @@ -136,4 +138,4 @@ class UniFiDeviceUpdateEntity(UniFiBase, UpdateEntity): self, version: str | None, backup: bool, **kwargs: Any ) -> None: """Install an update.""" - await self.controller.api.devices.upgrade(self.device.mac) + await self.controller.api.request(DeviceUpgradeRequest.create(self.device.mac)) diff --git a/requirements_all.txt b/requirements_all.txt index 9ce72ad763f..ffed2fdba6f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -276,7 +276,7 @@ aiosyncthing==0.5.1 aiotractive==0.5.4 # homeassistant.components.unifi -aiounifi==34 +aiounifi==35 # homeassistant.components.vlc_telnet aiovlc==0.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 9850aab5c12..1b268270ff0 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -251,7 +251,7 @@ aiosyncthing==0.5.1 aiotractive==0.5.4 # homeassistant.components.unifi -aiounifi==34 +aiounifi==35 # homeassistant.components.vlc_telnet aiovlc==0.1.0 diff --git a/tests/components/unifi/test_controller.py b/tests/components/unifi/test_controller.py index e420d031f46..5de99a3f434 100644 --- a/tests/components/unifi/test_controller.py +++ b/tests/components/unifi/test_controller.py @@ -25,13 +25,10 @@ from homeassistant.components.unifi.const import ( DEFAULT_TRACK_DEVICES, DEFAULT_TRACK_WIRED_CLIENTS, DOMAIN as UNIFI_DOMAIN, + PLATFORMS, UNIFI_WIRELESS_CLIENTS, ) -from homeassistant.components.unifi.controller import ( - PLATFORMS, - RETRY_TIMER, - get_unifi_controller, -) +from homeassistant.components.unifi.controller import RETRY_TIMER, get_unifi_controller from homeassistant.components.unifi.errors import AuthenticationRequired, CannotConnect from homeassistant.const import ( CONF_HOST,