From 700f3859c2ec07a74bbe79f9507686c332fb53c0 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Wed, 2 Feb 2022 19:31:11 +0100 Subject: [PATCH] Guard emeter accesses to avoid keyerrors (#304) Raise an exception to inform the caller that update() is needed --- kasa/smartdevice.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kasa/smartdevice.py b/kasa/smartdevice.py index 30468c45..bf43cf17 100755 --- a/kasa/smartdevice.py +++ b/kasa/smartdevice.py @@ -219,6 +219,8 @@ class SmartDevice: """Raise an exception if there is no emeter.""" if not self.has_emeter: raise SmartDeviceException("Device has no emeter") + if self.emeter_type not in self._last_update: + raise SmartDeviceException("update() required prior accessing emeter") async def _query_helper( self, target: str, cmd: str, arg: Optional[Dict] = None, child_ids=None