-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalclientwrapper.py
73 lines (56 loc) · 2.72 KB
/
localclientwrapper.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
from lib import TV, Thing, SinricPro, SinricProUdp
class ClientWrapper:
""" ClientWrapper for comunicate classes and things.\n
things -> [DimmerSwitch, Switch...] \n
appKey -> d89f1***-****-****-****-************ \n
secretKey -> 44d1d31-1c19-****-****-9bc96c34b5bb-d19f42dd-****-****-****-************"""
logger = None
events = {}
deviceIdArr = []
appKey = ""
secretKey = ""
def __init__(self, things : list, appKey : str, secretKey : str):
for thing in things:
thing.subscribe(self)
self.deviceIdArr.append(thing.get_dev_id())
self.appKey = appKey
self.secretKey = secretKey
def start(self):
''' Start clients and setup connection '''
# Request callbacks
callbs = {}
if len(self.events) > 0:
for key, receivers in self.events.items():
callbs[key] = lambda dev_id, *arg, recv=receivers: self.run_match_dev_id(dev_id,recv,locals()['arg'])
self.client = SinricPro(self.appKey, self.deviceIdArr, callbs, event_callbacks=self.eventsCallbacks, enable_log=False,restore_states=True,secretKey=self.secretKey)
for devId in self.deviceIdArr:
self.client.event_handler.raiseEvent(devId, 'setPowerState',data={'state': 'On'})
# Instantiate client (setup connections)
udp_client = SinricProUdp(callbs, self.deviceIdArr,enable_trace=False) # Set it to True to start logging request Offline Request/Response
self.client.handle_all(udp_client)
self.logger = client.logger
self.logger.success("SinricProOO started!")
def run_match_dev_id(self,dev_id,lista_thing_func,arg):
#print("dev:",dev_id," | arg:",arg," | lista: ",lista_thing_func)
for item in lista_thing_func:
if item['thing'].get_dev_id() == dev_id:
return item['func'](arg)
return False
def inscribe(self, thing, event_name, func):
if not (event_name in self.events):
self.events[event_name] = list()
self.events[event_name].append({'thing':thing,'func':func})
def raiseEvent(self, thing : Thing, event : str, dados):
''' Event listed on https://help.sinric.pro/pages/supported_devices.html '''
self.client.event_handler.raiseEvent(thing.get_dev_id(), event,data=dados)
def Events():
pass
# print("Evento.")
# while True:
# client.event_handler.raiseEvent(device1, 'setPowerState',data={'state': 'On'})
# client.event_handler.raiseEvent(device1, 'setPowerLevel',data={'powerLevel': '95%'})
#sleep(2)
# pass
eventsCallbacks={
"Events": Events
}