UniFi - Try to handle when UniFi erroneously marks offline client as wired (#26960)
* Add controls to catch when client goes offline and UniFi bug marks client as wired * Device trackers shouldn't jump between going away and home * POE control shouldn't add normally wireless clients as POE control switches
This commit is contained in:
1 parent
0eb1d49046
commit
09c5b9feb3
8 files changed
+161
-22
No files matched your search
@@ -1,14 +1,13 @@
|
||||
"""Support for devices connected to UniFi POE."""
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.unifi.config_flow import (
|
||||
get_controller_id_from_config_entry,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .config_flow import get_controller_id_from_config_entry
|
||||
from .const import (
|
||||
ATTR_MANUFACTURER,
|
||||
CONF_BLOCK_CLIENT,
|
||||
@@ -20,9 +19,14 @@ from .const import (
|
||||
CONF_SSID_FILTER,
|
||||
DOMAIN,
|
||||
UNIFI_CONFIG,
|
||||
UNIFI_WIRELESS_CLIENTS,
|
||||
)
|
||||
from .controller import UniFiController
|
||||
|
||||
SAVE_DELAY = 10
|
||||
STORAGE_KEY = "unifi_data"
|
||||
STORAGE_VERSION = 1
|
||||
|
||||
CONF_CONTROLLERS = "controllers"
|
||||
|
||||
CONTROLLER_SCHEMA = vol.Schema(
|
||||
@@ -61,6 +65,9 @@ async def async_setup(hass, config):
|
||||
if DOMAIN in config:
|
||||
hass.data[UNIFI_CONFIG] = config[DOMAIN][CONF_CONTROLLERS]
|
||||
|
||||
hass.data[UNIFI_WIRELESS_CLIENTS] = wireless_clients = UnifiWirelessClients(hass)
|
||||
await wireless_clients.async_load()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@@ -70,9 +77,7 @@ async def async_setup_entry(hass, config_entry):
|
||||
hass.data[DOMAIN] = {}
|
||||
|
||||
controller = UniFiController(hass, config_entry)
|
||||
|
||||
controller_id = get_controller_id_from_config_entry(config_entry)
|
||||
|
||||
hass.data[DOMAIN][controller_id] = controller
|
||||
|
||||
if not await controller.async_setup():
|
||||
@@ -99,3 +104,43 @@ async def async_unload_entry(hass, config_entry):
|
||||
controller_id = get_controller_id_from_config_entry(config_entry)
|
||||
controller = hass.data[DOMAIN].pop(controller_id)
|
||||
return await controller.async_reset()
|
||||
|
||||
|
||||
class UnifiWirelessClients:
|
||||
"""Class to store clients known to be wireless.
|
||||
|
||||
This is needed since wireless devices going offline might get marked as wired by UniFi.
|
||||
"""
|
||||
|
||||
def __init__(self, hass):
|
||||
"""Set up client storage."""
|
||||
self.hass = hass
|
||||
self.data = {}
|
||||
self._store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
|
||||
|
||||
async def async_load(self):
|
||||
"""Load data from file."""
|
||||
data = await self._store.async_load()
|
||||
|
||||
if data is not None:
|
||||
self.data = data
|
||||
|
||||
@callback
|
||||
def get_data(self, config_entry):
|
||||
"""Get data related to a specific controller."""
|
||||
controller_id = get_controller_id_from_config_entry(config_entry)
|
||||
data = self.data.get(controller_id, {"wireless_devices": []})
|
||||
return set(data["wireless_devices"])
|
||||
|
||||
@callback
|
||||
def update_data(self, data, config_entry):
|
||||
"""Update data and schedule to save to file."""
|
||||
controller_id = get_controller_id_from_config_entry(config_entry)
|
||||
self.data[controller_id] = {"wireless_devices": list(data)}
|
||||
|
||||
self._store.async_delay_save(self._data_to_save, SAVE_DELAY)
|
||||
|
||||
@callback
|
||||
def _data_to_save(self):
|
||||
"""Return data of UniFi wireless clients to store in a file."""
|
||||
return self.data
|
||||
@@ -10,6 +10,7 @@ CONF_CONTROLLER = "controller"
|
||||
CONF_SITE_ID = "site"
|
||||
|
||||
UNIFI_CONFIG = "unifi_config"
|
||||
UNIFI_WIRELESS_CLIENTS = "unifi_wireless_clients"
|
||||
|
||||
CONF_BLOCK_CLIENT = "block_client"
|
||||
CONF_DETECTION_TIME = "detection_time"
|
||||
|
||||
@@ -36,6 +36,7 @@ from .const import (
|
||||
DOMAIN,
|
||||
LOGGER,
|
||||
UNIFI_CONFIG,
|
||||
UNIFI_WIRELESS_CLIENTS,
|
||||
)
|
||||
from .errors import AuthenticationRequired, CannotConnect
|
||||
|
||||
@@ -50,6 +51,7 @@ class UniFiController:
|
||||
self.available = True
|
||||
self.api = None
|
||||
self.progress = None
|
||||
self.wireless_clients = None
|
||||
|
||||
self._site_name = None
|
||||
self._site_role = None
|
||||
@@ -128,6 +130,22 @@ class UniFiController:
|
||||
"""Event specific per UniFi entry to signal new options."""
|
||||
return f"unifi-options-{CONTROLLER_ID.format(host=self.host, site=self.site)}"
|
||||
|
||||
def update_wireless_clients(self):
|
||||
"""Update set of known to be wireless clients."""
|
||||
new_wireless_clients = set()
|
||||
|
||||
for client_id in self.api.clients:
|
||||
if (
|
||||
client_id not in self.wireless_clients
|
||||
and not self.api.clients[client_id].is_wired
|
||||
):
|
||||
new_wireless_clients.add(client_id)
|
||||
|
||||
if new_wireless_clients:
|
||||
self.wireless_clients |= new_wireless_clients
|
||||
unifi_wireless_clients = self.hass.data[UNIFI_WIRELESS_CLIENTS]
|
||||
unifi_wireless_clients.update_data(self.wireless_clients, self.config_entry)
|
||||
|
||||
async def request_update(self):
|
||||
"""Request an update."""
|
||||
if self.progress is not None:
|
||||
@@ -170,6 +188,8 @@ class UniFiController:
|
||||
LOGGER.info("Reconnected to controller %s", self.host)
|
||||
self.available = True
|
||||
|
||||
self.update_wireless_clients()
|
||||
|
||||
async_dispatcher_send(self.hass, self.signal_update)
|
||||
|
||||
async def async_setup(self):
|
||||
@@ -197,6 +217,10 @@ class UniFiController:
|
||||
LOGGER.error("Unknown error connecting with UniFi controller: %s", err)
|
||||
return False
|
||||
|
||||
wireless_clients = hass.data[UNIFI_WIRELESS_CLIENTS]
|
||||
self.wireless_clients = wireless_clients.get_data(self.config_entry)
|
||||
self.update_wireless_clients()
|
||||
|
||||
self.import_configuration()
|
||||
|
||||
self.config_entry.add_update_listener(self.async_options_updated)
|
||||
|
||||
@@ -26,7 +26,6 @@ DEVICE_ATTRIBUTES = [
|
||||
"ip",
|
||||
"is_11r",
|
||||
"is_guest",
|
||||
"is_wired",
|
||||
"mac",
|
||||
"name",
|
||||
"noted",
|
||||
@@ -121,6 +120,7 @@ class UniFiClientTracker(ScannerEntity):
|
||||
"""Set up tracked client."""
|
||||
self.client = client
|
||||
self.controller = controller
|
||||
self.is_wired = self.client.mac not in controller.wireless_clients
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self):
|
||||
@@ -129,13 +129,13 @@ class UniFiClientTracker(ScannerEntity):
|
||||
return False
|
||||
|
||||
if (
|
||||
not self.client.is_wired
|
||||
not self.is_wired
|
||||
and self.controller.option_ssid_filter
|
||||
and self.client.essid not in self.controller.option_ssid_filter
|
||||
):
|
||||
return False
|
||||
|
||||
if not self.controller.option_track_wired_clients and self.client.is_wired:
|
||||
if not self.controller.option_track_wired_clients and self.is_wired:
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -145,18 +145,31 @@ class UniFiClientTracker(ScannerEntity):
|
||||
LOGGER.debug("New UniFi client tracker %s (%s)", self.name, self.client.mac)
|
||||
|
||||
async def async_update(self):
|
||||
"""Synchronize state with controller."""
|
||||
"""Synchronize state with controller.
|
||||
|
||||
Make sure to update self.is_wired if client is wireless, there is an issue when clients go offline that they get marked as wired.
|
||||
"""
|
||||
LOGGER.debug(
|
||||
"Updating UniFi tracked client %s (%s)", self.entity_id, self.client.mac
|
||||
)
|
||||
await self.controller.request_update()
|
||||
|
||||
if self.is_wired and self.client.mac in self.controller.wireless_clients:
|
||||
self.is_wired = False
|
||||
|
||||
@property
|
||||
def is_connected(self):
|
||||
"""Return true if the client is connected to the network."""
|
||||
if (
|
||||
dt_util.utcnow() - dt_util.utc_from_timestamp(float(self.client.last_seen))
|
||||
) < self.controller.option_detection_time:
|
||||
"""Return true if the client is connected to the network.
|
||||
|
||||
If is_wired and client.is_wired differ it means that the device is offline and UniFi bug shows device as wired.
|
||||
"""
|
||||
if self.is_wired == self.client.is_wired and (
|
||||
(
|
||||
dt_util.utcnow()
|
||||
- dt_util.utc_from_timestamp(float(self.client.last_seen))
|
||||
)
|
||||
< self.controller.option_detection_time
|
||||
):
|
||||
return True
|
||||
|
||||
return False
|
||||
@@ -195,6 +208,8 @@ class UniFiClientTracker(ScannerEntity):
|
||||
if variable in self.client.raw:
|
||||
attributes[variable] = self.client.raw[variable]
|
||||
|
||||
attributes["is_wired"] = self.is_wired
|
||||
|
||||
return attributes
|
||||
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ def update_items(controller, async_add_entities, switches, switches_off):
|
||||
new_switches.append(switches[block_client_id])
|
||||
LOGGER.debug("New UniFi Block switch %s (%s)", client.hostname, client.mac)
|
||||
|
||||
# control poe
|
||||
# control POE
|
||||
for client_id in controller.api.clients:
|
||||
|
||||
poe_client_id = f"poe-{client_id}"
|
||||
@@ -108,9 +108,10 @@ def update_items(controller, async_add_entities, switches, switches_off):
|
||||
pass
|
||||
# Network device with active POE
|
||||
elif (
|
||||
not client.is_wired
|
||||
client_id in controller.wireless_clients
|
||||
or client.sw_mac not in devices
|
||||
or not devices[client.sw_mac].ports[client.sw_port].port_poe
|
||||
or not devices[client.sw_mac].ports[client.sw_port].poe_enable
|
||||
or controller.mac == client.mac
|
||||
):
|
||||
continue
|
||||
|
||||
Reference in new issue
Block a user