-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot-tuya.py
executable file
·111 lines (93 loc) · 3.59 KB
/
iot-tuya.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# coding: utf-8
import logging
from tuya_iot import TuyaOpenAPI, TUYA_LOGGER
import json
import os
import dotenv
import sys
EXIT_OK = 0
EXIT_AUTHENTICATION_FAILED = 1
EXIT_SYNTAX_ERROR = 2
EXIT_LIST_HOMES_FAILED = 3
EXIT_HOME_MISSING = 4
EXIT_LIST_SCENES_FAILED = 5
EXIT_SCENE_MISSING = 6
EXIT_TRIGGER_SCENE_FAILED = 7
def main() -> int:
dotenv.load_dotenv()
for key in [ 'endpoint_url',
'access_id',
'access_secret',
'username',
'password',
'country_code',
'schema' ]:
if not os.environ.get('iot_tuya_' + key):
print( f"Please specify missing {'iot_tuya_' + key} in your .env file.",
file=sys.stderr)
return EXIT_AUTHENTICATION_FAILED
# Init
# TUYA_LOGGER.setLevel(logging.DEBUG)
openapi = TuyaOpenAPI(
os.environ.get('iot_tuya_endpoint_url'),
os.environ.get('iot_tuya_access_id'),
os.environ.get('iot_tuya_access_secret'))
response = openapi.connect(
os.environ.get('iot_tuya_username'),
os.environ.get('iot_tuya_password'),
os.environ.get('iot_tuya_country_code'),
os.environ.get('iot_tuya_schema'))
if not response['success']:
print('Authentication failed: %s' % response['msg'], file=sys.stderr)
return EXIT_AUTHENTICATION_FAILED
uid = response['result']['uid']
try:
cmd = sys.argv[1]
except IndexError:
cmd = ''
if not cmd in [ 'homes', 'scenes', 'on', 'off' ]:
print('Syntax: iot-tuya.py (on|off|homes|scenes)', file=sys.stderr)
return EXIT_SYNTAX_ERROR;
if cmd == 'homes':
response = openapi.get('/v1.0/users/{uid}/homes'.format(uid = uid))
if not response['success']:
print('Error listing homes: %s' % response['msg'], file=sys.stderr)
return EXIT_LIST_HOMES_FAILED
print('Homes:')
for home in response['result']:
print('%s: %s' % (home['name'], home['home_id']))
return EXIT_OK
home_id = os.environ.get('iot_tuya_home')
if cmd == 'scenes':
if not home_id:
print('Set iot_tuya_home in .env first, in order to list scenes!', file=sys.stderr)
return EXIT_HOME_MISSING
response = openapi.get('/v1.1/homes/{home_id}/scenes'.format(home_id = home_id))
if not response['success']:
print('Error listing scenes: %s' % response['msg'], file=sys.stderr)
return EXIT_LIST_SCENES_FAILED
for scene in response['result']:
print('%s: %s' % (scene['name'], scene['scene_id']))
return EXIT_OK
if not home_id:
print('Set iot_tuya_home in .env first, in order to trigger scenes!', file=sys.stderr)
return EXIT_HOME_MISSING
if cmd == 'on':
scene_id = os.environ.get('iot_tuya_scene_on')
if not scene_id:
print('Set iot_tuya_scene_on in .env first!', file=sys.stderr)
return EXIT_SCENE_MISSING
elif cmd == 'off':
scene_id = os.environ.get('iot_tuya_scene_off')
if not scene_id:
print('Set iot_tuya_scene_off in .env first!', file=sys.stderr)
return EXIT_SCENE_MISSING
response = openapi.post('/v1.0/homes/{home_id}/scenes/{scene_id}/trigger'.format(home_id = home_id, scene_id = scene_id))
if not response['success']:
print('Error triggering scene: %s' % response['msg'], file=sys.stderr)
return EXIT_TRIGGER_SCENE_FAILED
print('iot.tuya trigger succeeded.')
return EXIT_OK
if __name__ == '__main__':
sys.exit(main())