mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Improve unifi device tracker performance (#37308)
* Improve unifi device tracker performance The unifi websocket sends an update every second which generates a significant amount of state changed updates. Avoid creating callback functions when they are not going to be used. * make _no_heartbeat/_make_disconnected instance methods * remove extra empty line * revert is_wired change * remove extra line
This commit is contained in:
parent
094f7ee718
commit
f352c51990
@ -167,15 +167,7 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
|
||||
def async_update_callback(self) -> None:
|
||||
"""Update the clients state."""
|
||||
|
||||
@callback
|
||||
def _make_disconnected(now):
|
||||
"""Mark client as disconnected."""
|
||||
self._is_connected = False
|
||||
self.cancel_scheduled_update = None
|
||||
self.async_write_ha_state()
|
||||
|
||||
if self.client.last_updated == SOURCE_EVENT:
|
||||
|
||||
if (self.is_wired and self.client.event.event in WIRED_CONNECTION) or (
|
||||
not self.is_wired and self.client.event.event in WIRELESS_CONNECTION
|
||||
):
|
||||
@ -190,7 +182,6 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
|
||||
self.schedule_update = True
|
||||
|
||||
elif not self.client.event and self.client.last_updated == SOURCE_DATA:
|
||||
|
||||
if self.is_wired == self.client.is_wired:
|
||||
self._is_connected = True
|
||||
self.schedule_update = True
|
||||
@ -203,12 +194,19 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
|
||||
|
||||
self.cancel_scheduled_update = async_track_point_in_utc_time(
|
||||
self.hass,
|
||||
_make_disconnected,
|
||||
self._make_disconnected,
|
||||
dt_util.utcnow() + self.controller.option_detection_time,
|
||||
)
|
||||
|
||||
super().async_update_callback()
|
||||
|
||||
@callback
|
||||
def _make_disconnected(self, _):
|
||||
"""Mark client as disconnected."""
|
||||
self._is_connected = False
|
||||
self.cancel_scheduled_update = None
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def is_connected(self):
|
||||
"""Return true if the client is connected to the network."""
|
||||
@ -239,10 +237,13 @@ class UniFiClientTracker(UniFiClient, ScannerEntity):
|
||||
|
||||
attributes["is_wired"] = self.is_wired
|
||||
|
||||
for variable in CLIENT_STATIC_ATTRIBUTES + CLIENT_CONNECTED_ATTRIBUTES:
|
||||
if self.is_connected:
|
||||
for variable in CLIENT_CONNECTED_ATTRIBUTES:
|
||||
if variable in self.client.raw:
|
||||
attributes[variable] = self.client.raw[variable]
|
||||
|
||||
for variable in CLIENT_STATIC_ATTRIBUTES:
|
||||
if variable in self.client.raw:
|
||||
if not self.is_connected and variable in CLIENT_CONNECTED_ATTRIBUTES:
|
||||
continue
|
||||
attributes[variable] = self.client.raw[variable]
|
||||
|
||||
return attributes
|
||||
@ -291,14 +292,8 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity):
|
||||
def async_update_callback(self):
|
||||
"""Update the devices' state."""
|
||||
|
||||
@callback
|
||||
def _no_heartbeat(now):
|
||||
"""No heart beat by device."""
|
||||
self._is_connected = False
|
||||
self.cancel_scheduled_update = None
|
||||
self.async_write_ha_state()
|
||||
|
||||
if self.device.last_updated == SOURCE_DATA:
|
||||
|
||||
self._is_connected = True
|
||||
|
||||
if self.cancel_scheduled_update:
|
||||
@ -306,7 +301,7 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity):
|
||||
|
||||
self.cancel_scheduled_update = async_track_point_in_utc_time(
|
||||
self.hass,
|
||||
_no_heartbeat,
|
||||
self._no_heartbeat,
|
||||
dt_util.utcnow() + timedelta(seconds=self.device.next_interval + 60),
|
||||
)
|
||||
|
||||
@ -319,6 +314,13 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity):
|
||||
|
||||
super().async_update_callback()
|
||||
|
||||
@callback
|
||||
def _no_heartbeat(self, _):
|
||||
"""No heart beat by device."""
|
||||
self._is_connected = False
|
||||
self.cancel_scheduled_update = None
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def is_connected(self):
|
||||
"""Return true if the device is connected to the network."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user