Skip to content

Commit

Permalink
fix erroneous call to internal_set_state
Browse files Browse the repository at this point in the history
  • Loading branch information
willoma committed Oct 16, 2023
1 parent 31085a6 commit bdbe6d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 47 deletions.
82 changes: 36 additions & 46 deletions pycalaos/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
import ssl
import time
import urllib.request

from .item import new_item
Expand Down Expand Up @@ -71,29 +70,6 @@ def send(self, request):
with urllib.request.urlopen(req, context=self._context) as response:
return json.load(response)

def _poll_events_to_map(pollEvents):
events = {}
for rawEvent in pollEvents:
try:
eventData = rawEvent["data"]
except:
_LOGGER.debug(f"Poll received event without data: {rawEvent}")
continue

try:
key = eventData["id"]
except:
_LOGGER.debug(f"Poll received event without ID: {rawEvent}")
continue

try:
value = eventData["state"]
except:
_LOGGER.debug(f"Poll received event without state: {rawEvent}")
continue

events[key] = value
return events

class Client:
"""A Calaos client"""
Expand Down Expand Up @@ -151,9 +127,17 @@ def _register_polling(self):
self._polling_id = resp["uuid"]
return self.update_all()

def _update_from_map(self, eventsTuples):
def update_all(self):
"""Check all states and return events
Return events for states changes (list of pycalaos.item.common.Event)
"""
_LOGGER.debug("Getting all states from known items")
resp = self._conn.send(
{"action": "get_state", "items": list(self.items.keys())}
)
events = []
for kv in eventsTuples:
for kv in resp.items():
try:
item = self.items[kv[0]]
except KeyError:
Expand All @@ -162,40 +146,46 @@ def _update_from_map(self, eventsTuples):

changed = item.internal_set_state(kv[1])
if changed:
events.append(Event(item))
events.append(Event(self.items[kv[0]]))
return events

def update_all(self):
"""Check all states and return events
Return events for states changes (list of pycalaos.item.common.Event)
"""
_LOGGER.debug("Getting all states from known items")
resp = self._conn.send(
{"action": "get_state", "items": list(self.items.keys())}
)
return self._update_from_map(resp.items())

def poll(self):
"""Change items states and return all events since the last poll
Return events for states changes (list of pycalaos.item.common.Event)
"""
if self._polling_id is None:
_LOGGER.debug("Registering to the polling")
events = self._register_polling()
return self._register_polling()
else:
resp = self._conn.send(
{"action": "poll_listen", "type": "get", "uuid": self._polling_id}
)
if resp["success"] == "true":
_LOGGER.debug("Polling new values")
events = self._update_from_map(
_poll_events_to_map(resp["events"])
)
else:
if resp["success"] != "true":
_LOGGER.debug("Polling failed, re-registering")
events = self._register_polling()
return self._register_polling()

events = []
for rawEvent in resp["events"]:
try:
eventData = rawEvent["data"]
itemID = eventData["id"]
state = eventData["state"]
except:
_LOGGER.debug(f"Bogus event: {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(state)
if changed:
events.append(Event(item))
if len(events) > 0:
_LOGGER.debug(f"Events: {events}")
return events

@property
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pycalaos"
version = "0.0.27"
version = "0.0.28"
authors = [
{ name = "Willow Maccagnoni", email = "willow.maccagnoni@gmail.com" },
]
Expand Down

0 comments on commit bdbe6d6

Please sign in to comment.