From ec10f9cb525b3094ec0bebc3d230738f4e067cdc Mon Sep 17 00:00:00 2001 From: Muhammad Zamroni Date: Tue, 21 May 2024 23:03:42 +0200 Subject: [PATCH 1/2] Handle AttributeError when device is not available --- custom_components/speedport/device_tracker.py | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/custom_components/speedport/device_tracker.py b/custom_components/speedport/device_tracker.py index 4ab3e32..63b3160 100644 --- a/custom_components/speedport/device_tracker.py +++ b/custom_components/speedport/device_tracker.py @@ -61,23 +61,39 @@ def source_type(self) -> SourceType: @property def hostname(self) -> str | None: - """Return the hostname of device.""" - return self._device.name + try: + """Return the hostname of device.""" + return self._device.name + except AttributeError: + """The device is disconnected or unavailable in the router.""" + return None @property def ip_address(self) -> str | None: - """Return the primary ip address of the device.""" - return self._device.ipv4 + try: + """Return the primary ip address of the device.""" + return self._device.ipv4 + except AttributeError: + """The device is disconnected or unavailable in the router.""" + return None @property def is_connected(self) -> bool: - """Return device status.""" - return self._device.connected + try: + """Return device status.""" + return self._device.connected + except AttributeError: + """The device is disconnected or unavailable in the router.""" + return False @property def mac_address(self) -> str: - """Return mac_address.""" - return self._device.mac + try: + """Return mac_address.""" + return self._device.mac + except AttributeError: + """The device is disconnected or unavailable in the router.""" + return "" @property def icon(self) -> str: From 33003998986884f586b824aac40ef760e4da344e Mon Sep 17 00:00:00 2001 From: Muhammad Zamroni Date: Wed, 22 May 2024 00:00:54 +0200 Subject: [PATCH 2/2] Handle AttributeError on device.name --- custom_components/speedport/device_tracker.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/custom_components/speedport/device_tracker.py b/custom_components/speedport/device_tracker.py index 63b3160..438efa2 100644 --- a/custom_components/speedport/device_tracker.py +++ b/custom_components/speedport/device_tracker.py @@ -123,8 +123,12 @@ def extra_state_attributes(self) -> dict[str, str]: @property def name(self) -> str: - """Return device name.""" - return self._device.name + try: + """Return device name.""" + return self._device.name + except AttributeError: + """The device is disconnected or unavailable in the router.""" + return "" @callback def _handle_coordinator_update(self) -> None: