-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtuya.py
More file actions
44 lines (32 loc) · 1.43 KB
/
tuya.py
File metadata and controls
44 lines (32 loc) · 1.43 KB
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
from tuya_iot import TuyaOpenAPI
# Cloud project authorization info
ACCESS_ID ='your-access-id'
ACCESS_KEY ='your-access-key'
# Select an endpoint base on your project availability zone
# For more info: https://developer.tuya.com/en/docs/iot/api-request?id=Ka4a8uuo1j4t4
ENDPOINT = "https://openapi.tuyaeu.com"
# Project configuration
USERNAME = 'your-username' # email address or phone number
PASSWORD = 'your-password'
DEVICE_ID = 'vdevo163137171462882'
# Initialization of tuya openapi
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.login(USERNAME, PASSWORD)
#### Control the Device with Python ####
# commands = {'commands': [{'code':'switch_1','value': True}]}
# request = openapi.post(f'/v1.0/iot-03/devices/{DEVICE_ID}/commands', commands)
# print(request)
#### Turn the Device on/off Depending on the Temperature ####
location = openapi.get('/v1.0/iot-03/locations/ip?ip=your-ip-address')
print(location)
location = location['result']
latitude, longitude = location['latitude'], location['longitude']
weather_url = f'/v2.0/iot-03/weather/current?lat={latitude}&lon={longitude}'
weather = openapi.get(weather_url)
print(weather)
temperature = weather['result']['current_weather']['temp']
print(temperature)
if float(temperature) >= 30:
commands = {'commands': [{'code':'switch_1','value': True}]}
request = openapi.post(f'/v1.0/iot-03/devices/{DEVICE_ID}/commands', commands)
print(request)