diff --git a/homeassistant/components/unifi/controller.py b/homeassistant/components/unifi/controller.py index 7da36131058..a6981aeddee 100644 --- a/homeassistant/components/unifi/controller.py +++ b/homeassistant/components/unifi/controller.py @@ -162,7 +162,7 @@ class UniFiController: WIRELESS_GUEST_CONNECTED, ): self.update_wireless_clients() - elif data.get("clients") or data.get("devices"): + elif "clients" in data or "devices" in data: async_dispatcher_send(self.hass, self.signal_update) @property diff --git a/homeassistant/components/unifi/manifest.json b/homeassistant/components/unifi/manifest.json index 85633ebf131..01aa245f608 100644 --- a/homeassistant/components/unifi/manifest.json +++ b/homeassistant/components/unifi/manifest.json @@ -4,7 +4,7 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/unifi", "requirements": [ - "aiounifi==14" + "aiounifi==15" ], "dependencies": [], "codeowners": [ diff --git a/homeassistant/components/unifi/switch.py b/homeassistant/components/unifi/switch.py index 84e85188ede..0df019de02c 100644 --- a/homeassistant/components/unifi/switch.py +++ b/homeassistant/components/unifi/switch.py @@ -262,7 +262,7 @@ class UniFiPOEClientSwitch(UniFiClient, SwitchDevice, RestoreEntity): """Shortcut to the switch port that client is connected to.""" try: return self.device.ports[self.client.sw_port] - except TypeError: + except (AttributeError, KeyError, TypeError): LOGGER.warning( "Entity %s reports faulty device %s or port %s", self.entity_id, @@ -282,7 +282,7 @@ class UniFiBlockClientSwitch(UniFiClient, SwitchDevice): @property def is_on(self): """Return true if client is allowed to connect.""" - return not self.client.blocked + return not self.is_blocked async def async_turn_on(self, **kwargs): """Turn on connectivity for client.""" @@ -291,3 +291,10 @@ class UniFiBlockClientSwitch(UniFiClient, SwitchDevice): async def async_turn_off(self, **kwargs): """Turn off connectivity for client.""" await self.controller.api.clients.async_block(self.client.mac) + + @property + def icon(self): + """Return the icon to use in the frontend.""" + if self.is_blocked: + return "mdi:network-off" + return "mdi:network" diff --git a/homeassistant/components/unifi/unifi_client.py b/homeassistant/components/unifi/unifi_client.py index f9e77d47c0e..4c1ce402e7f 100644 --- a/homeassistant/components/unifi/unifi_client.py +++ b/homeassistant/components/unifi/unifi_client.py @@ -2,6 +2,14 @@ import logging +from aiounifi.api import SOURCE_EVENT +from aiounifi.events import ( + WIRED_CLIENT_BLOCKED, + WIRED_CLIENT_UNBLOCKED, + WIRELESS_CLIENT_BLOCKED, + WIRELESS_CLIENT_UNBLOCKED, +) + from homeassistant.core import callback from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -9,6 +17,9 @@ from homeassistant.helpers.entity import Entity LOGGER = logging.getLogger(__name__) +CLIENT_BLOCKED = (WIRED_CLIENT_BLOCKED, WIRELESS_CLIENT_BLOCKED) +CLIENT_UNBLOCKED = (WIRED_CLIENT_UNBLOCKED, WIRELESS_CLIENT_UNBLOCKED) + class UniFiClient(Entity): """Base class for UniFi clients.""" @@ -18,7 +29,9 @@ class UniFiClient(Entity): self.client = client self.controller = controller self.listeners = [] + self.is_wired = self.client.mac not in controller.wireless_clients + self.is_blocked = self.client.blocked async def async_added_to_hass(self) -> None: """Client entity created.""" @@ -41,6 +54,12 @@ class UniFiClient(Entity): """Update the clients state.""" if self.is_wired and self.client.mac in self.controller.wireless_clients: self.is_wired = False + + if self.client.last_updated == SOURCE_EVENT: + + if self.client.event.event in CLIENT_BLOCKED + CLIENT_UNBLOCKED: + self.is_blocked = self.client.event.event in CLIENT_BLOCKED + LOGGER.debug("Updating client %s %s", self.entity_id, self.client.mac) self.async_schedule_update_ha_state() diff --git a/requirements_all.txt b/requirements_all.txt index dfbe9ef8c4e..678bf3ba674 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -203,7 +203,7 @@ aiopylgtv==0.3.3 aioswitcher==2019.4.26 # homeassistant.components.unifi -aiounifi==14 +aiounifi==15 # homeassistant.components.wwlln aiowwlln==2.0.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 0580e954e10..00e04b8fd99 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -81,7 +81,7 @@ aiopylgtv==0.3.3 aioswitcher==2019.4.26 # homeassistant.components.unifi -aiounifi==14 +aiounifi==15 # homeassistant.components.wwlln aiowwlln==2.0.2