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
|
||||
Reference in new issue
Block a user