2020-01-31 19:23:25 +00:00
|
|
|
"""Base class for UniFi clients."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2020-03-10 17:27:25 +00:00
|
|
|
from aiounifi.api import SOURCE_EVENT
|
|
|
|
from aiounifi.events import (
|
|
|
|
WIRED_CLIENT_BLOCKED,
|
2020-03-11 16:24:52 +00:00
|
|
|
WIRED_CLIENT_CONNECTED,
|
|
|
|
WIRED_CLIENT_DISCONNECTED,
|
2020-03-10 17:27:25 +00:00
|
|
|
WIRED_CLIENT_UNBLOCKED,
|
|
|
|
WIRELESS_CLIENT_BLOCKED,
|
2020-03-11 16:24:52 +00:00
|
|
|
WIRELESS_CLIENT_CONNECTED,
|
|
|
|
WIRELESS_CLIENT_DISCONNECTED,
|
2020-04-10 13:43:25 +00:00
|
|
|
WIRELESS_CLIENT_ROAM,
|
2020-03-10 17:27:25 +00:00
|
|
|
WIRELESS_CLIENT_UNBLOCKED,
|
|
|
|
)
|
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-03-10 17:27:25 +00:00
|
|
|
CLIENT_BLOCKED = (WIRED_CLIENT_BLOCKED, WIRELESS_CLIENT_BLOCKED)
|
|
|
|
CLIENT_UNBLOCKED = (WIRED_CLIENT_UNBLOCKED, WIRELESS_CLIENT_UNBLOCKED)
|
2020-03-11 16:24:52 +00:00
|
|
|
WIRED_CLIENT = (WIRED_CLIENT_CONNECTED, WIRED_CLIENT_DISCONNECTED)
|
2020-04-10 13:43:25 +00:00
|
|
|
WIRELESS_CLIENT = (
|
|
|
|
WIRELESS_CLIENT_CONNECTED,
|
|
|
|
WIRELESS_CLIENT_DISCONNECTED,
|
|
|
|
WIRELESS_CLIENT_ROAM,
|
|
|
|
)
|
2020-03-10 17:27:25 +00:00
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
|
|
|
|
class UniFiClient(Entity):
|
|
|
|
"""Base class for UniFi clients."""
|
|
|
|
|
|
|
|
def __init__(self, client, controller) -> None:
|
|
|
|
"""Set up client."""
|
|
|
|
self.client = client
|
|
|
|
self.controller = controller
|
2020-03-10 17:27:25 +00:00
|
|
|
|
2020-04-17 06:39:01 +00:00
|
|
|
self._is_wired = self.client.mac not in controller.wireless_clients
|
2020-03-10 17:27:25 +00:00
|
|
|
self.is_blocked = self.client.blocked
|
2020-03-11 16:24:52 +00:00
|
|
|
self.wired_connection = None
|
|
|
|
self.wireless_connection = None
|
2020-01-31 19:23:25 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Client entity created."""
|
2020-04-17 06:39:01 +00:00
|
|
|
LOGGER.debug("New client %s (%s)", self.entity_id, self.client.mac)
|
2020-01-31 19:23:25 +00:00
|
|
|
self.client.register_callback(self.async_update_callback)
|
2020-04-17 06:39:01 +00:00
|
|
|
self.async_on_remove(
|
2020-01-31 19:23:25 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, self.controller.signal_reachable, self.async_update_callback
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Disconnect client object when removed."""
|
|
|
|
self.client.remove_callback(self.async_update_callback)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_callback(self) -> None:
|
|
|
|
"""Update the clients state."""
|
2020-04-17 06:39:01 +00:00
|
|
|
if self._is_wired and self.client.mac in self.controller.wireless_clients:
|
|
|
|
self._is_wired = False
|
2020-03-10 17:27:25 +00:00
|
|
|
|
|
|
|
if self.client.last_updated == SOURCE_EVENT:
|
2020-03-11 16:24:52 +00:00
|
|
|
if self.client.event.event in WIRELESS_CLIENT:
|
2020-04-10 13:43:25 +00:00
|
|
|
self.wireless_connection = self.client.event.event in (
|
|
|
|
WIRELESS_CLIENT_CONNECTED,
|
|
|
|
WIRELESS_CLIENT_ROAM,
|
2020-03-11 16:24:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
elif self.client.event.event in WIRED_CLIENT:
|
|
|
|
self.wired_connection = (
|
|
|
|
self.client.event.event == WIRED_CLIENT_CONNECTED
|
|
|
|
)
|
|
|
|
|
|
|
|
elif self.client.event.event in CLIENT_BLOCKED + CLIENT_UNBLOCKED:
|
2020-03-10 17:27:25 +00:00
|
|
|
self.is_blocked = self.client.event.event in CLIENT_BLOCKED
|
|
|
|
|
2020-04-17 06:39:01 +00:00
|
|
|
LOGGER.debug("Updating client %s (%s)", self.entity_id, self.client.mac)
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2020-01-31 19:23:25 +00:00
|
|
|
|
2020-04-17 06:39:01 +00:00
|
|
|
@property
|
|
|
|
def is_wired(self):
|
|
|
|
"""Return if the client is wired.
|
|
|
|
|
|
|
|
Allows disabling logic to keep track of clients affected by UniFi wired bug marking wireless devices as wired. This is useful when running a network not only containing UniFi APs.
|
|
|
|
"""
|
|
|
|
if self.controller.option_ignore_wired_bug:
|
|
|
|
return self.client.is_wired
|
|
|
|
return self._is_wired
|
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the client."""
|
|
|
|
return self.client.name or self.client.hostname
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if controller is available."""
|
|
|
|
return self.controller.available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self) -> dict:
|
|
|
|
"""Return a client description for device registry."""
|
|
|
|
return {"connections": {(CONNECTION_NETWORK_MAC, self.client.mac)}}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
|
|
|
"""No polling needed."""
|
2020-02-28 18:28:03 +00:00
|
|
|
return True
|