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 daf24cb88..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]}, }, }, } @@ -690,6 +702,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 +729,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): @@ -1251,7 +1266,7 @@ def _generate_payload(self, command, data=None, cid=None, gwId=None, devId=None, and command in payload_dict[self.dev_type] 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