Skip to content

Commit

Permalink
send update dps command with heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
vmartinv committed Aug 27, 2021
1 parent 363d763 commit 4a9958e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion custom_components/localtuya/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]}},
},
}

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4a9958e

Please sign in to comment.