Skip to content

Commit e76a59e

Browse files
authored
Merge pull request #336 from uzlonewolf/misc
Misc minor updates
2 parents 7622884 + bf1eac1 commit e76a59e

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

examples/async_send_receive.py

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
For more information see https://github.com/jasonacox/tinytuya
99
1010
"""
11+
import time
1112
import tinytuya
1213

1314
# tinytuya.set_debug(True)
@@ -16,38 +17,49 @@
1617
d.set_version(3.3)
1718
d.set_socketPersistent(True)
1819

20+
# Devices will close the connection if they do not receve data every 30 seconds
21+
# Sending heartbeat packets every 9 seconds gives some wiggle room for lost packets or loop lag
22+
PING_TIME = 9
23+
24+
# Option - also poll
25+
POLL_TIME = 60
26+
1927
print(" > Send Request for Status < ")
20-
payload = d.generate_payload(tinytuya.DP_QUERY)
21-
d.send(payload)
28+
d.status(nowait=True)
2229

2330
print(" > Begin Monitor Loop <")
31+
pingtime = time.time() + PING_TIME
32+
polltime = time.time() + POLL_TIME
2433
while(True):
2534
# See if any data is available
2635
data = d.receive()
27-
print('Received Payload: %r' % data)
28-
29-
# Send keyalive heartbeat
30-
print(" > Send Heartbeat Ping < ")
31-
payload = d.generate_payload(tinytuya.HEART_BEAT)
32-
d.send(payload)
33-
34-
# Option - Some plugs require an UPDATEDPS command to update their power data points
35-
36-
# print(" > Send Request for Status < ")
37-
# payload = d.generate_payload(tinytuya.DP_QUERY)
38-
# d.send(payload)
39-
40-
# # See if any data is available
41-
# data = d.receive()
42-
# print('Received Payload: %r' % data)
43-
44-
# print(" > Send DPS Update Request < ")
45-
# payload = d.generate_payload(tinytuya.UPDATEDPS,['18','19','20'])
46-
# Some Tuya devices will not accept the DPS index values for UPDATEDPS - try:
47-
# payload = d.generate_payload(tinytuya.UPDATEDPS)
48-
# d.send(payload)
49-
50-
# # See if any data is available
51-
# data = d.receive()
52-
# print('Received Payload: %r' % data)
53-
36+
if data:
37+
print('Received Payload: %r' % data)
38+
39+
if( pingtime <= time.time() ):
40+
pingtime = time.time() + PING_TIME
41+
# Send keep-alive heartbeat
42+
print(" > Send Heartbeat Ping < ")
43+
d.heartbeat(nowait=True)
44+
45+
# Option - Poll for status
46+
if( polltime <= time.time() ):
47+
polltime = time.time() + POLL_TIME
48+
49+
# Option - Some plugs require an UPDATEDPS command to update their power data points
50+
if False:
51+
print(" > Send DPS Update Request < ")
52+
53+
# # Some Tuya devices require a list of DPs to update
54+
# payload = d.generate_payload(tinytuya.UPDATEDPS,['18','19','20'])
55+
# data = d.send(payload)
56+
# print('Received Payload: %r' % data)
57+
58+
# # Other devices will not accept the DPS index values for UPDATEDPS - try:
59+
# payload = d.generate_payload(tinytuya.UPDATEDPS)
60+
# data = d.send(payload)
61+
# print('Received Payload: %r' % data)
62+
63+
print(" > Send Request for Status < ")
64+
data = d.status()
65+
print('Received Payload: %r' % data)

examples/getstatus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626

2727
# Get the status of the device
2828
# e.g. {'devId': '0071299988f9376255b', 'dps': {'1': True, '3': 208, '101': False}}
29-
data = d.get_status()
30-
print(data)
29+
data = d.status()
30+
print(data)

tinytuya/Cloud.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def setregion(self, apiRegion=None):
127127
if self.apiRegion == "in":
128128
self.urlhost = "openapi.tuyain.com" # India Datacenter
129129

130-
def _tuyaplatform(self, uri, action='GET', post=None, ver='v1.0', recursive=False, query=None, content_type='application/json'):
130+
def _tuyaplatform(self, uri, action='GET', post=None, ver='v1.0', recursive=False, query=None, content_type=None):
131131
"""
132132
Handle GET and POST requests to Tuya Cloud
133133
"""
@@ -141,12 +141,14 @@ def _tuyaplatform(self, uri, action='GET', post=None, ver='v1.0', recursive=Fals
141141
headers = {}
142142
body = {}
143143
sign_url = url
144-
if content_type:
145-
headers['Content-type'] = content_type
146144
if post is not None:
147145
body = json.dumps(post)
148146
if action not in ('GET', 'POST', 'PUT', 'DELETE'):
149147
action = 'POST' if post else 'GET'
148+
if action == 'POST' and content_type is None:
149+
content_type = 'application/json'
150+
if content_type:
151+
headers['Content-type'] = content_type
150152
if query:
151153
# note: signature must be calculated before URL-encoding!
152154
if type(query) == str:

tinytuya/scanner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
max_parallel = 300
6969
connect_timeout = 3
7070

71-
devinfo_keys = ('ip', 'mac', 'name', 'key', 'gwId', 'active', 'ablilty', 'encrypt', 'productKey', 'version', 'token', 'wf_cfg' )
71+
devinfo_keys = ('ip', 'mac', 'name', 'key', 'gwId', 'active', 'ability', 'encrypt', 'productKey', 'version', 'token', 'wf_cfg' )
7272
# id ver
7373

7474
TermColors = namedtuple("TermColors", "bold, subbold, normal, dim, alert, alertdim, cyan, red, yellow")
@@ -170,6 +170,10 @@ def __init__( self, ip, deviceinfo, options, debug ):
170170

171171
if not deviceinfo:
172172
deviceinfo = {}
173+
# some devices report "ability" but most have this as the typo "ablilty"
174+
if 'ablilty' in deviceinfo and 'ability' not in deviceinfo:
175+
deviceinfo['ability'] = deviceinfo['ablilty']
176+
del deviceinfo['ablilty']
173177
self.deviceinfo = deviceinfo
174178
for k in devinfo_keys:
175179
if k not in deviceinfo:

0 commit comments

Comments
 (0)