From be87df1d9e27ff27a49a5ce80244f815077f39d6 Mon Sep 17 00:00:00 2001 From: Willow Maccagnoni Date: Fri, 21 Jul 2023 12:24:34 +0200 Subject: [PATCH] poll: ignore events without id or state attributes related to https://github.com/willoma/calaos-ha/issues/2 --- pycalaos/client.py | 26 ++++++++++++++++++++++++-- pyproject.toml | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pycalaos/client.py b/pycalaos/client.py index 7340f2c..3311ba6 100644 --- a/pycalaos/client.py +++ b/pycalaos/client.py @@ -163,15 +163,37 @@ def poll(self): ) if len(resp["events"]) > 0: _LOGGER.debug(f"Raw events from polling: {resp['events']}") + events = [] for rawEvent in resp["events"]: try: - item = self.items[rawEvent["data"]["id"]] + eventData = rawEvent["data"] + except: + _LOGGER.error(f"Poll received event without data: {rawEvent}") + continue + + try: + itemID = eventData["id"] + except: + _LOGGER.error(f"Poll received event without ID: {rawEvent}") + continue + + try: + state = eventData["state"] + except: + _LOGGER.debug(f"Poll received event without state: {rawEvent}") + continue + + try: + item = self.items[itemID] except KeyError: + _LOGGER.error(f"Poll received event with unknown ID: {rawEvent}") continue - changed = item.internal_from_event(rawEvent["data"]["state"]) + + changed = item.internal_from_event(state) if changed: events.append(Event(item)) + self._last_poll = now if len(events) > 0: _LOGGER.debug(f"Events: {events}") diff --git a/pyproject.toml b/pyproject.toml index 301cfef..a02e13e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pycalaos" -version = "0.0.25" +version = "0.0.26" authors = [ { name = "Willow Maccagnoni", email = "willow.maccagnoni@gmail.com" }, ]