forked from jasonacox/tinytuya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_send_receive.py
53 lines (40 loc) · 1.48 KB
/
async_send_receive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# TinyTuya Example
# -*- coding: utf-8 -*-
"""
TinyTuya - Example showing async persistent connection to device with
continual loop watching for device updates.
Author: Jason A. Cox
For more information see https://github.com/jasonacox/tinytuya
"""
import tinytuya
# tinytuya.set_debug(True)
d = tinytuya.OutletDevice('DEVICEID', 'DEVICEIP', 'DEVICEKEY')
d.set_version(3.3)
d.set_socketPersistent(True)
print(" > Send Request for Status < ")
payload = d.generate_payload(tinytuya.DP_QUERY)
d.send(payload)
print(" > Begin Monitor Loop <")
while(True):
# See if any data is available
data = d.receive()
print('Received Payload: %r' % data)
# Send keyalive heartbeat
print(" > Send Heartbeat Ping < ")
payload = d.generate_payload(tinytuya.HEART_BEAT)
d.send(payload)
# Option - Some plugs require an UPDATEDPS command to update their power data points
# print(" > Send Request for Status < ")
# payload = d.generate_payload(tinytuya.DP_QUERY)
# d.send(payload)
# # See if any data is available
# data = d.receive()
# print('Received Payload: %r' % data)
# print(" > Send DPS Update Request < ")
# payload = d.generate_payload(tinytuya.UPDATEDPS,['18','19','20'])
# Some Tuya devices will not accept the DPS index values for UPDATEDPS - try:
# payload = d.generate_payload(tinytuya.UPDATEDPS)
# d.send(payload)
# # See if any data is available
# data = d.receive()
# print('Received Payload: %r' % data)