Skip to content

Commit

Permalink
Merge pull request #49 from alexualbu/3.7.0
Browse files Browse the repository at this point in the history
3.7.0 Fix PAYLOAD_DICT and revert Heartbeat logic to upstream
  • Loading branch information
leeyuentuen authored Apr 3, 2023
2 parents f521b1b + 7367aa5 commit 1f1a855
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
4 changes: 3 additions & 1 deletion custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down
27 changes: 21 additions & 6 deletions custom_components/localtuya/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]},
},
},
}
Expand Down Expand Up @@ -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."""
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1f1a855

Please sign in to comment.