From 62feeb9e41b8192a9c35b489acbc1d64aeeb45ee Mon Sep 17 00:00:00 2001 From: xZetsubou Date: Fri, 13 Oct 2023 12:19:55 +0300 Subject: [PATCH] Fix `3.3` Sub devices control. --- .../localtuya/core/pytuya/__init__.py | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/custom_components/localtuya/core/pytuya/__init__.py b/custom_components/localtuya/core/pytuya/__init__.py index 47ab19188..ea14a596d 100644 --- a/custom_components/localtuya/core/pytuya/__init__.py +++ b/custom_components/localtuya/core/pytuya/__init__.py @@ -200,7 +200,7 @@ class DecodeError(Exception): # Default Device "type_0a": { AP_CONFIG: { # [BETA] Set Control Values on Device - "command": {"gwId": "", "devId": "", "uid": "", "t": ""}, + "command": {"gwId": "", "devId": "", "uid": "", "t": "", "cid": ""}, }, CONTROL: { # Set Control Values on Device "command": {"devId": "", "uid": "", "t": "", "cid": ""}, @@ -1384,28 +1384,26 @@ def _generate_payload( else: json_data["uid"] = self.id if "cid" in json_data: - if nodeId is not None: - json_data["cid"] = nodeId - elif self.node_id != "" and self.node_id is not None: - json_data["cid"] = self.node_id + if cid := nodeId or self.node_id: + json_data["cid"] = cid + # for <= 3.3 we don't need `gwID`, `devID` and `uid` in payload. + for k in ["gwId", "devId", "uid"]: + if k in json_data: + json_data.pop(k) else: - # Remove "CID" if Device isn't sub. del json_data["cid"] - # "cid" For 3.4 and 3.5 versions. - if "data" in json_data: - if "cid" in json_data["data"]: - if nodeId is not None: - json_data["data"]["cid"] = self.node_id - elif self.node_id != "" and self.node_id is not None: - json_data["data"]["cid"] = self.node_id - else: - # Remove "CID" if Device isn't sub. - del json_data["data"]["cid"] + if "data" in json_data and "cid" in json_data["data"]: + # "cid" in inside "data" For 3.4 and 3.5 versions. + if cid := nodeId or self.node_id: + json_data["data"]["cid"] = cid + else: + del json_data["data"]["cid"] if "t" in json_data: if json_data["t"] == "int": json_data["t"] = int(time.time()) else: json_data["t"] = str(int(time.time())) + if data is not None: if "dpId" in json_data: json_data["dpId"] = data