-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuyaInterface.py
90 lines (77 loc) · 3.23 KB
/
tuyaInterface.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
import os
import json
import threading
import time
import tinytuya
from devicesData import DevicesData
from logger import logger
TIME_SLEEP = 5
class tuyaInterface:
tuyaDevices = {}
def __init__(self):
#tinytuya.set_debug()
self.threads = []
def start(self):
DevicesData.add_observer(self)
for key, device in DevicesData.data.items():
threadPoll = threading.Thread(target=self.poll,args=(device["desc"],))
self.threads.append(threadPoll)
threadPoll.start()
def onCommandReceived(self,id,command):
logger.info (f"Send {command} to {id} : {self.tuyaDevices[id]}")
ret = None
switchCommandId = DevicesData[id]["desc"]["switchCommandId"]
if (command["switch"] == True):
ret = self.tuyaDevices[id].set_value(switchCommandId, True)
elif (command["switch"] == False):
ret = self.tuyaDevices[id].set_value(switchCommandId, False)
logger.info (f"Command {command} to {id} returns {ret}")
def poll(self, device ):
'''
Start MQTT threads, and then poll a device for status updates.
Params:
device: An instance of Device dataclass
'''
logger.info('Connecting to %s', device['ip'])
try:
tuyaDevice = tinytuya.OutletDevice(device['id'], device['ip'], device['key'])
tuyaDevice.set_version(float(device['version']))
tuyaDevice.set_socketPersistent(True)
self.tuyaDevices[device['id']] = tuyaDevice
status = ""
except Exception as e:
logger.error("======== Exception 1 ========")
logger.error(e)
return
while True:
try:
status = tuyaDevice.status()
for cle, valeur in status['dps'].items():
if cle in device['mapping']:
code=device['mapping'][cle]['code']
valueType=device['mapping'][cle]['type']
if valueType == "Integer":
scale=device['mapping'][cle]['values']['scale']
unit=device['mapping'][cle]['values']['unit']
value=valeur/(10 ** scale)
else:
value=valeur
unit=""
DevicesData[device["id"]]["status"][code] = value
else:
DevicesData[device["id"]]["status"][cle] = valeur
try:
logger.debug(f'STATUS for {device["id"]} : {status}')
except:
continue
except Exception as e:
logger.error("======== Exception 2 ========")
logger.error(tuyaDevice)
logger.error("------ Device -------")
logger.error(device)
logger.error("------ Status -------")
logger.error(status)
logger.error("------ DevicesData -------")
logger.error(DevicesData.data)
logger.error("===========================")
time.sleep(TIME_SLEEP)