From d59ee110ec49834e2664b9550eb3b712be2f7461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Villagra?= Date: Fri, 27 Aug 2021 02:04:48 +0100 Subject: [PATCH] send update dps command with heartbeat --- .../localtuya/pytuya/__init__.py | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index 789091c7b..2c613b426 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -61,6 +61,7 @@ SET = "set" STATUS = "status" HEARTBEAT = "heartbeat" +UPDATEDPS = "updatedps" # Request refresh of DPS PROTOCOL_VERSION_BYTES_31 = b"3.1" PROTOCOL_VERSION_BYTES_33 = b"3.3" @@ -90,11 +91,13 @@ STATUS: {"hexByte": 0x0A, "command": {"gwId": "", "devId": ""}}, SET: {"hexByte": 0x07, "command": {"devId": "", "uid": "", "t": ""}}, HEARTBEAT: {"hexByte": 0x09, "command": {}}, + UPDATEDPS: {"hexByte": 0x12, "command": {"dpId": [18, 19, 20]}}, }, "type_0d": { STATUS: {"hexByte": 0x0D, "command": {"devId": "", "uid": "", "t": ""}}, SET: {"hexByte": 0x07, "command": {"devId": "", "uid": "", "t": ""}}, HEARTBEAT: {"hexByte": 0x09, "command": {}}, + UPDATEDPS: {"hexByte": 0x12, "command": {"dpId": [18, 19, 20]}}, }, } @@ -379,6 +382,7 @@ async def heartbeat_loop(): while True: try: await self.heartbeat() + await self.updatedps() await asyncio.sleep(HEARTBEAT_INTERVAL) except asyncio.CancelledError: self.debug("Stopped heartbeat loop") @@ -478,6 +482,16 @@ async def heartbeat(self): """Send a heartbeat message.""" return await self.exchange(HEARTBEAT) + async def updatedps(self): + """ + Request device to update index. + Args: + index(array): list of dps to update (ex. [4, 5, 6, 18, 19, 20]) + """ + self.debug('updatedps() entry (dev_type is %s)', self.dev_type) + payload = self._generate_payload(UPDATEDPS) + self.transport.write(payload) + async def set_dp(self, value, dp_index): """ Set value (may be any type: bool, int or string) of any dps index. @@ -582,7 +596,10 @@ def _generate_payload(self, command, data=None): json_data["t"] = str(int(time.time())) if data is not None: - json_data["dps"] = data + if "dpId" in json_data: + json_data["dpId"] = data + else: + json_data["dps"] = data elif command_hb == 0x0D: json_data["dps"] = self.dps_to_request @@ -591,7 +608,7 @@ def _generate_payload(self, command, data=None): if self.version == 3.3: payload = self.cipher.encrypt(payload, False) - if command_hb != 0x0A: + if command_hb != 0x0A and command_hb != 0x12: # add the 3.3 header payload = PROTOCOL_33_HEADER + payload elif command == SET: