Skip to content

Commit

Permalink
Fix expected values on sub_devices_states #244
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed May 20, 2024
1 parent 3834edb commit 6e345fc
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions custom_components/localtuya/core/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ def __init__(
self.on_connected = on_connected
self.heartbeater: asyncio.Task | None = None
self.dps_cache = {}
self.sub_devices_states = {"online": [], "offline": []} # [cids,...]
self.sub_devices_states = {"online": [], "offline": []}
self._sub_devs_query_task: asyncio.Task | None = None
self.local_nonce = b"0123456789abcdef" # not-so-random random key
self.remote_nonce = b""
Expand Down Expand Up @@ -820,7 +820,7 @@ def error_json(self, number=None, payload=None):
def _msg_subdevs_query(self, decoded_message):
"""
Handle the sub-devices query message.
Message: {"online": [cid1, ...], "offline": [cid2, ...]}
Message: {"online": [cids, ...], "offline": [cids, ...], "nearby": [cids, ...]}
"""

async def _action():
Expand All @@ -841,14 +841,15 @@ async def _action():

if (data := decoded_message.get("data")) and isinstance(data, dict):
devs_states = self.sub_devices_states
updated_states = {}

cached_on_devs, cached_off_devs = devs_states.values()
on_devs, off_devs = data.get("online", []), data.get("offline", [])

data["offline"] = list(set(cached_off_devs + off_devs))
data["online"] = list(set(cached_on_devs + on_devs))
updated_states["offline"] = list(set(cached_off_devs + off_devs))
updated_states["online"] = list(set(cached_on_devs + on_devs))

self.sub_devices_states = data
self.sub_devices_states = updated_states

if self._sub_devs_query_task is not None:
self._sub_devs_query_task.cancel()
Expand Down Expand Up @@ -1156,6 +1157,7 @@ async def set_dps(self, dps, cid=None):
async def subdevices_query(self):
"""Request a list of sub-devices and their status."""
# Return payload: {"online": [cid1, ...], "offline": [cid2, ...]}
# "nearby": [cids, ...] can come in payload.
payload = self._generate_payload(
LAN_EXT_STREAM, rawData={"cids": []}, reqType="subdev_online_stat_query"
)
Expand Down Expand Up @@ -1248,8 +1250,7 @@ def _decode_payload(self, payload):
payload = payload.decode()
except Exception as ex:
self.debug("payload was not string type and decoding failed")
raise DecodeError("payload was not a string: %s" % ex)
# return self.error_json(ERR_JSON, payload)
return self.error_json(ERR_JSON, payload)

if "data unvalid" in payload:
if self.version <= 3.3:
Expand Down

0 comments on commit 6e345fc

Please sign in to comment.