Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README for new Cloud functions, and tighter deviceid error checking #220

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,15 @@ Functions:

Cloud
setregion(apiRegion)
cloudrequest(url, action=[POST if post else GET], post={}, query={})
getdevices(verbose=False)
getstatus(deviceid)
getfunctions(deviceid)
getproperties(deviceid)
getdps(deviceid)
sendcommand(deviceid, commands)
getconnectstatus(deviceid)
getdevicelog(deviceid, start=[now - 1 day], end=[now], evtype="1,2,3,4,5,6,7,8,9,10", size=100, params={})
```

### TinyTuya Error Codes
Expand Down Expand Up @@ -389,6 +392,17 @@ result = c.sendcommand(id,commands)
print("Results\n:", result)
```

Up to one week of device logs can also be pulled from the Cloud. By default getdevicelog() will pull 1 day of logs or 100 log entries, whichever comes first.

```python
import tinytuya
import json

c = tinytuya.Cloud()
r = c.getdevicelog( '00112233445566778899' )
print( json.dumps(r, indent=2) )
```

### Encryption notes

These devices uses AES encryption which is not available in the Python standard library. There are three options:
Expand Down
23 changes: 14 additions & 9 deletions tinytuya/Cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def _getuid(self, deviceid=None):
# Get user ID (UID) for deviceid
if not self.token:
return self.error
if deviceid is None:
if not deviceid:
return error_json(
ERR_PARAMS,
"_getuid() requires deviceID parameter"
Expand Down Expand Up @@ -281,7 +281,7 @@ def cloudrequest(self, url, action=None, post=None, query=None):
Make a generic cloud request and return the results.

Args:
url: Required. The URL to fetch.
url: Required. The URL to fetch, i.e. "/v1.0/devices/0011223344556677/logs"
action: Optional. GET, POST, DELETE, or PUT. Defaults to GET, unless POST data is supplied.
post: Optional. POST body data. Will be fed into json.dumps() before posting.
query: Optional. A dict containing query string key/value pairs.
Expand Down Expand Up @@ -360,7 +360,7 @@ def filter_devices( self, devs, ip_list=None ):
def _getdevice(self, param='status', deviceid=None):
if not self.token:
return self.error
if deviceid is None:
if not deviceid:
return error_json(
ERR_PARAMS,
"Missing DeviceID Parameter"
Expand Down Expand Up @@ -398,7 +398,7 @@ def getdps(self, deviceid=None):
"""
if not self.token:
return self.error
if deviceid is None:
if not deviceid:
return error_json(
ERR_PARAMS,
"Missing DeviceID Parameter"
Expand All @@ -418,10 +418,10 @@ def sendcommand(self, deviceid=None, commands=None):
"""
if not self.token:
return self.error
if deviceid is None or commands is None:
if (not deviceid) or (not commands):
return error_json(
ERR_PARAMS,
"Missing DeviceID and Command Parameters"
"Missing DeviceID and/or Command Parameters"
)
uri = 'iot-03/devices/%s/commands' % (deviceid)
response_dict = self._tuyaplatform(uri,action='POST',post=commands)
Expand All @@ -438,7 +438,7 @@ def getconnectstatus(self, deviceid=None):
"""
if not self.token:
return self.error
if deviceid is None:
if not deviceid:
return error_json(
ERR_PARAMS,
"Missing DeviceID Parameter"
Expand All @@ -452,7 +452,7 @@ def getconnectstatus(self, deviceid=None):
)
return(response_dict["result"]["online"])

def getdevicelog(self, devid, start=None, end=None, evtype=None, size=100, params={}):
def getdevicelog(self, deviceid=None, start=None, end=None, evtype=None, size=100, params={}):
"""
Get the logs for a device.

Expand All @@ -471,6 +471,11 @@ def getdevicelog(self, devid, start=None, end=None, evtype=None, size=100, param
Returns:
Response from server
"""
if not deviceid:
return error_json(
ERR_PARAMS,
"Missing DeviceID Parameter"
)
if not end:
end = int(time.mktime(time.gmtime()))
if not start:
Expand All @@ -493,4 +498,4 @@ def getdevicelog(self, devid, start=None, end=None, evtype=None, size=100, param
if 'query_type' not in params:
params['query_type'] = 1

return self.cloudrequest( '/v1.0/devices/%s/logs' % devid, query=params)
return self.cloudrequest( '/v1.0/devices/%s/logs' % deviceid, query=params)