from sinric import SinricPro from credentials import appKey, deviceIdArr, secretKey, temperatureSensorDeviceId from sinric import SinricProUdp from sinric import eventNames from time import sleep def Events(): while True: client.event_handler.raiseEvent(temperatureSensorDeviceId, 'temperatureHumidityEvent', data={'humidity': 75.3, 'temperature': 24}) sleep(60) # Server will trottle / block IPs sending events too often. pass def onPowerState(did, state): print(did, state) return True, state eventsCallbacks={ "Events": Events } callbacks = { 'powerState': onPowerState } if __name__ == '__main__': client = SinricPro(appKey, deviceIdArr, callbacks,event_callbacks=eventsCallbacks, enable_log=True,restore_states=False,secretKey=secretKey) client.handle_all()
"User-Agent": "PostmanRuntime/7.18.0", "Accept": "*/*", "Cache-Control": "no-cache", "Postman-Token": "d001f97e-0181-4e40-8518-56d8890142ec,256ed8fa-93bb-4a16-ad34-96929283b2b0", "Host": "blynk-cloud.com", "Accept-Encoding": "gzip, deflate", "Connection": "keep-alive", "cache-control": "no-cache", } response = requests.request("GET", url, headers=headers, params=querystring) if __name__ == "__main__": client = SinricPro( appKey, deviceIdArr, callbacks, event_callbacks=event_callback, enable_log=False, restore_states=False, secretKey=secretKey, ) # Set it to True to start logging request Offline Request/Response udp_client = SinricProUdp(callbacks, deviceIdArr, enable_trace=False) client.handle_all(udp_client)
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) # 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 }