From 09ce551e661e5265a15375a4035873b2075e0481 Mon Sep 17 00:00:00 2001 From: Alex Albu Date: Sat, 25 Mar 2023 12:26:00 +0200 Subject: [PATCH 1/3] Fix _generate_payload --- custom_components/localtuya/pytuya/__init__.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index 43255c5eb..c258530e0 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -1238,11 +1238,11 @@ def _generate_payload(self, command, data=None, cid=None, gwId=None, devId=None, json_data = command_override = None if self.dev_type in payload_dict and command in payload_dict[self.dev_type]: - if command in payload_dict[self.dev_type]: - json_data = payload_dict[self.dev_type][command] - if command_override in payload_dict[self.dev_type][command]: + if COMMAND in payload_dict[self.dev_type][command]: + json_data = payload_dict[self.dev_type][command][COMMAND] + if COMMAND_OVERRIDE in payload_dict[self.dev_type][command]: command_override = payload_dict[self.dev_type][command][ - command_override + COMMAND_OVERRIDE ] if self.dev_type != DEV_TYPE_0A: @@ -1250,16 +1250,16 @@ def _generate_payload(self, command, data=None, cid=None, gwId=None, devId=None, json_data is None and self.dev_type in payload_dict and command in payload_dict[self.dev_type] - and command in payload_dict[self.dev_type][command] + and COMMAND in payload_dict[self.dev_type][command] ): - json_data = payload_dict[self.dev_type][command][command] + json_data = payload_dict[self.dev_type][command][COMMAND] if ( command_override is None and self.dev_type in payload_dict and command in payload_dict[self.dev_type] - and command_override in payload_dict[self.dev_type][command] + and COMMAND_OVERRIDE in payload_dict[self.dev_type][command] ): - command_override = payload_dict[self.dev_type][command][command_override] + command_override = payload_dict[self.dev_type][command][COMMAND_OVERRIDE] if command_override is None: command_override = command @@ -1310,7 +1310,6 @@ def _generate_payload(self, command, data=None, cid=None, gwId=None, devId=None, elif self.dev_type == DEV_TYPE_0D and command == DP_QUERY: json_data[PROPERTY_DPS] = self.dps_to_request - payload = json.dumps(json_data).replace(" ", "").encode("utf-8") if json_data == "": payload = "" else: From b8e14c2c8d63910386ce6fa7dcacc68df2d30504 Mon Sep 17 00:00:00 2001 From: Alex Albu Date: Mon, 3 Apr 2023 10:08:00 +0300 Subject: [PATCH 2/3] Revert Heartbeat logic to upstream --- custom_components/localtuya/common.py | 4 +++- custom_components/localtuya/pytuya/__init__.py | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index ad90afccc..51a94793d 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -182,7 +182,8 @@ async def _make_connection(self): status = await self._interface.status() if status is None: raise Exception("Failed to retrieve status") - + + self._interface.start_heartbeat() self.status_updated(status) if self._disconnect_task is not None: @@ -340,6 +341,7 @@ async def _make_connection(self): is_gateway=True, ) + self._interface.start_heartbeat() # Re-add and get status of previously added sub-devices # Note this assumes the gateway device has not been tear down for subitem in self._sub_devices.items(): diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index 8a875f099..c4c89f0d5 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -690,6 +690,11 @@ def _status_update(self, msg): def connection_made(self, transport): """Did connect to the device.""" + self.transport = transport + self.on_connected.set_result(True) + + def start_heartbeat(self): + """Start the heartbeat transmissions with the device.""" async def heartbeat_loop(): """Continuously send heart beat updates.""" @@ -712,8 +717,6 @@ async def heartbeat_loop(): self.transport = None transport.close() - self.transport = transport - self.on_connected.set_result(True) self.heartbeater = self.loop.create_task(heartbeat_loop()) def data_received(self, data): From 7367aa5185ddf372218db8ca1f06f2acc672f725 Mon Sep 17 00:00:00 2001 From: Alex Albu Date: Mon, 3 Apr 2023 10:08:24 +0300 Subject: [PATCH 3/3] Fix PAYLOAD_DICT --- custom_components/localtuya/pytuya/__init__.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index c4c89f0d5..82a455e52 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -261,12 +261,24 @@ class DecodeError(Exception): COMMAND_OVERRIDE: CONTROL_NEW, # Uses CONTROL_NEW command COMMAND: {"protocol": 5, "t": "int", "data": ""}, }, - DP_QUERY: {COMMAND_OVERRIDE: DP_QUERY_NEW}, + DP_QUERY: { + COMMAND_OVERRIDE: DP_QUERY_NEW, + COMMAND: {PARAMETER_GW_ID: "", PARAMETER_DEV_ID: "", PARAMETER_UID: "" }, + }, DP_QUERY_NEW: { - COMMAND: {PARAMETER_CID: ""}, + COMMAND: {PARAMETER_DEV_ID: "", PARAMETER_UID: "", PARAMETER_T: ""} }, HEART_BEAT: { - COMMAND: {} + COMMAND: {PARAMETER_GW_ID: "", PARAMETER_DEV_ID: ""} + }, + CONTROL_NEW: { + COMMAND: {PARAMETER_DEV_ID: "", PARAMETER_UID: "", PARAMETER_T: "", PARAMETER_CID: ""} + }, + STATUS: { # Get Status from Device + COMMAND: {PARAMETER_GW_ID: "", PARAMETER_DEV_ID: ""}, + }, + UPDATEDPS: { + COMMAND: {PARAMETER_DP_ID: [18, 19, 20]}, }, }, }