Improve unifi performance with many devices (#45006)

With 250 clients, there were about 18000 timers updated every
minute. To avoid this, we check which entities should be set
to not_home only once every second.
This commit is contained in:
J. Nick Koston 2021-01-10 09:12:21 -10:00 committed by GitHub
parent 707a8e62f9
commit b450d4c135
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 52 deletions

View file

@ -21,7 +21,6 @@ from homeassistant.components.device_tracker.const import SOURCE_TYPE_ROUTER
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.event import async_track_point_in_utc_time
import homeassistant.util.dt as dt_util
from .const import ATTR_MANUFACTURER, DOMAIN as UNIFI_DOMAIN
@ -53,6 +52,8 @@ CLIENT_STATIC_ATTRIBUTES = [
"oui",
]
CLIENT_CONNECTED_ALL_ATTRIBUTES = CLIENT_CONNECTED_ATTRIBUTES + CLIENT_STATIC_ATTRIBUTES
DEVICE_UPGRADED = (ACCESS_POINT_UPGRADED, GATEWAY_UPGRADED, SWITCH_UPGRADED)
WIRED_CONNECTION = (WIRED_CLIENT_CONNECTED,)
@ -142,7 +143,7 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
super().__init__(client, controller)
self.schedule_update = False
self.cancel_scheduled_update = None
self.disconnected_time = None
self._is_connected = False
if client.last_seen:
self._is_connected = (
@ -154,10 +155,14 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
if self._is_connected:
self.schedule_update = True
async def async_added_to_hass(self) -> None:
"""Watch object when added."""
self.controller.add_disconnected_check(self)
await super().async_added_to_hass()
async def async_will_remove_from_hass(self) -> None:
"""Disconnect object when removed."""
if self.cancel_scheduled_update:
self.cancel_scheduled_update()
self.controller.remove_disconnected_check(self)
await super().async_will_remove_from_hass()
@callback
@ -170,12 +175,10 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
):
self._is_connected = True
self.schedule_update = False
if self.cancel_scheduled_update:
self.cancel_scheduled_update()
self.cancel_scheduled_update = None
self.disconnected_time = None
# Ignore extra scheduled update from wired bug
elif not self.cancel_scheduled_update:
elif not self.disconnected_time:
self.schedule_update = True
elif not self.client.event and self.client.last_updated == SOURCE_DATA:
@ -185,23 +188,16 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
if self.schedule_update:
self.schedule_update = False
if self.cancel_scheduled_update:
self.cancel_scheduled_update()
self.cancel_scheduled_update = async_track_point_in_utc_time(
self.hass,
self._make_disconnected,
dt_util.utcnow() + self.controller.option_detection_time,
self.disconnected_time = (
dt_util.utcnow() + self.controller.option_detection_time
)
super().async_update_callback()
@callback
def _make_disconnected(self, _):
def make_disconnected(self, *_):
"""Mark client as disconnected."""
self._is_connected = False
self.cancel_scheduled_update = None
self.async_write_ha_state()
@property
@ -230,16 +226,16 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
@property
def device_state_attributes(self):
"""Return the client state attributes."""
attributes = {"is_wired": self.is_wired}
raw = self.client.raw
if self.is_connected:
for variable in CLIENT_CONNECTED_ATTRIBUTES:
if variable in self.client.raw:
attributes[variable] = self.client.raw[variable]
attributes = {
k: raw[k] for k in CLIENT_CONNECTED_ALL_ATTRIBUTES if k in raw
}
else:
attributes = {k: raw[k] for k in CLIENT_STATIC_ATTRIBUTES if k in raw}
for variable in CLIENT_STATIC_ATTRIBUTES:
if variable in self.client.raw:
attributes[variable] = self.client.raw[variable]
attributes["is_wired"] = self.is_wired
return attributes
@ -270,17 +266,21 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity):
super().__init__(device, controller)
self._is_connected = device.state == 1
self.cancel_scheduled_update = None
self.disconnected_time = None
@property
def device(self):
"""Wrap item."""
return self._item
async def async_added_to_hass(self) -> None:
"""Watch object when added."""
self.controller.add_disconnected_check(self)
await super().async_added_to_hass()
async def async_will_remove_from_hass(self) -> None:
"""Disconnect device object when removed."""
if self.cancel_scheduled_update:
self.cancel_scheduled_update()
"""Disconnect object when removed."""
self.controller.remove_disconnected_check(self)
await super().async_will_remove_from_hass()
@callback
@ -288,16 +288,9 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity):
"""Update the devices' state."""
if self.device.last_updated == SOURCE_DATA:
self._is_connected = True
if self.cancel_scheduled_update:
self.cancel_scheduled_update()
self.cancel_scheduled_update = async_track_point_in_utc_time(
self.hass,
self._no_heartbeat,
dt_util.utcnow() + timedelta(seconds=self.device.next_interval + 60),
self.disconnected_time = dt_util.utcnow() + timedelta(
seconds=self.device.next_interval + 60
)
elif (
@ -310,10 +303,9 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity):
super().async_update_callback()
@callback
def _no_heartbeat(self, _):
def make_disconnected(self, *_):
"""No heart beat by device."""
self._is_connected = False
self.cancel_scheduled_update = None
self.async_write_ha_state()
@property