From e387e6d3322c62d6c8094531e9c07994f9b71a36 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 26 Apr 2022 22:49:03 +0200 Subject: [PATCH] Add logging to UniFi device tracker for help debugging client status (#70824) * Add logging to UniFi device tracker for help debugging client status * Add if _LOGGER.isEnabledFor(logging.DEBUG) --- .../components/unifi/device_tracker.py | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/unifi/device_tracker.py b/homeassistant/components/unifi/device_tracker.py index 60ea4b3284b..0ad1c920fa7 100644 --- a/homeassistant/components/unifi/device_tracker.py +++ b/homeassistant/components/unifi/device_tracker.py @@ -1,5 +1,7 @@ """Track both clients and devices using UniFi Network.""" + from datetime import timedelta +import logging from aiounifi.api import SOURCE_DATA from aiounifi.events import ( @@ -28,6 +30,8 @@ from .const import DOMAIN as UNIFI_DOMAIN from .unifi_client import UniFiClient from .unifi_entity_base import UniFiBase +LOGGER = logging.getLogger(__name__) + CLIENT_TRACKER = "client" DEVICE_TRACKER = "device" @@ -150,16 +154,33 @@ class UniFiClientTracker(UniFiClient, ScannerEntity): """Set up tracked client.""" super().__init__(client, controller) - self.heartbeat_check = False self._controller_connection_state_changed = False - self._last_seen = client.last_seen or 0 + last_seen = client.last_seen or 0 self.schedule_update = self._is_connected = ( self.is_wired == client.is_wired - and dt_util.utcnow() - dt_util.utc_from_timestamp(float(self._last_seen)) + and dt_util.utcnow() - dt_util.utc_from_timestamp(float(last_seen)) < controller.option_detection_time ) + @callback + def _async_log_debug_data(self, method: str) -> None: + """Print debug data about entity.""" + if not LOGGER.isEnabledFor(logging.DEBUG): + return + last_seen = self.client.last_seen or 0 + LOGGER.debug( + "%s [%s, %s] [%s %s] [%s] %s (%s)", + method, + self.entity_id, + self.client.mac, + self.schedule_update, + self._is_connected, + dt_util.utc_from_timestamp(float(last_seen)), + dt_util.utcnow() - dt_util.utc_from_timestamp(float(last_seen)), + last_seen, + ) + async def async_added_to_hass(self) -> None: """Watch object when added.""" self.async_on_remove( @@ -170,6 +191,7 @@ class UniFiClientTracker(UniFiClient, ScannerEntity): ) ) await super().async_added_to_hass() + self._async_log_debug_data("added_to_hass") async def async_will_remove_from_hass(self) -> None: """Disconnect object when removed.""" @@ -200,16 +222,16 @@ class UniFiClientTracker(UniFiClient, ScannerEntity): self.client.last_updated == SOURCE_DATA and self.is_wired == self.client.is_wired ): - self._last_seen = self.client.last_seen self._is_connected = True self.schedule_update = True + self._async_log_debug_data("update_callback") + if self.schedule_update: self.schedule_update = False self.controller.async_heartbeat( self.unique_id, dt_util.utcnow() + self.controller.option_detection_time ) - self.heartbeat_check = True super().async_update_callback() @@ -218,6 +240,7 @@ class UniFiClientTracker(UniFiClient, ScannerEntity): """No heart beat by device.""" self._is_connected = False self.async_write_ha_state() + self._async_log_debug_data("make_disconnected") @property def device_info(self) -> None: