From f032957d0bbc8365f0324f979734e973716782d7 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 14 Oct 2024 18:07:21 +0200 Subject: [PATCH] [switch.tplink] Don't thrown an exception if a device doesn't support current_consumption. --- platypush/plugins/switch/tplink/__init__.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/platypush/plugins/switch/tplink/__init__.py b/platypush/plugins/switch/tplink/__init__.py index d06bc03ba..42f5feabb 100644 --- a/platypush/plugins/switch/tplink/__init__.py +++ b/platypush/plugins/switch/tplink/__init__.py @@ -185,10 +185,20 @@ class SwitchTplinkPlugin(RunnablePlugin, SwitchEntityManager): device = self._get_device(device) return self._set(device, not device.is_on) - @staticmethod - def _serialize(device: SmartDevice) -> dict: + def _current_consumption(self, device: SmartDevice) -> Optional[float]: + try: + return device.current_consumption() + except SmartDeviceException as e: + self.logger.warning( + 'Could not retrieve current consumption for device %s: %s', + device.host, + e, + ) + return None + + def _serialize(self, device: SmartDevice) -> dict: return { - 'current_consumption': device.current_consumption(), + 'current_consumption': self._current_consumption(device), 'id': device.host, 'ip': device.host, 'host': device.host,