-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathdevice.py
67 lines (51 loc) · 1.7 KB
/
device.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
import logging
from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD
from tuya_iot import (
TuyaOpenAPI,
AuthType,
TuyaOpenMQ,
TuyaDeviceManager,
TuyaHomeManager,
TuyaDeviceListener,
TuyaDevice,
TuyaTokenInfo,
TUYA_LOGGER
)
TUYA_LOGGER.setLevel(logging.DEBUG)
# Init
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.CUSTOM)
openapi.connect(USERNAME, PASSWORD)
openmq = TuyaOpenMQ(openapi)
openmq.start()
print("device test-> ", openapi.token_info.uid)
# Get device list
# assetManager = TuyaAssetManager(openapi)
# devIds = assetManager.getDeviceList(ASSET_ID)
# Update device status
deviceManager = TuyaDeviceManager(openapi, openmq)
homeManager = TuyaHomeManager(openapi, openmq, deviceManager)
homeManager.update_device_cache()
# # deviceManager.updateDeviceCaches(devIds)
# device = deviceManager.deviceMap.get(DEVICE_ID)
class tuyaDeviceListener(TuyaDeviceListener):
def update_device(self, device: TuyaDevice):
print("_update-->", device)
def add_device(self, device: TuyaDevice):
print("_add-->", device)
def remove_device(self, device_id: str):
pass
deviceManager.add_device_listener(tuyaDeviceListener())
# Turn on the light
# deviceManager.sendCommands(device.id, [{'code': 'switch_led', 'value': True}])
# time.sleep(1)
# print('status: ', device.status)
# # Turn off the light
# deviceManager.sendCommands(device.id, [{'code': 'switch_led', 'value': False}])
# time.sleep(1)
# print('status: ', device.status)
flag = True
while True:
input()
flag = not flag
commands = {'commands': [{'code': 'switch_led', 'value': flag}]}
openapi.post('/v1.0/iot-03/devices/{}/commands'.format(DEVICE_ID), commands)