From 466fff98b0eab0eb18240842b1ac0e1bbc09771d Mon Sep 17 00:00:00 2001 From: imdhanush Date: Sun, 3 Nov 2019 17:53:49 +0530 Subject: [PATCH] Pass deviceId Array instead of deviceId String --- setup.py | 2 +- sinric/__init__.py | 1 - sinric/_cbhandler.py | 29 ++++++++++++++++++++++++----- sinric/_events.py | 26 ++++++++++++++++++++++++-- sinric/_sinricpro.py | 2 +- sinric/_sinricprosocket.py | 2 +- sinric/_sinricproudp.py | 4 ++-- 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/setup.py b/setup.py index 6c55c21..1586629 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ if sys.version_info < (3,7): sys.exit('Sorry, Python < 3.7 is not supported') -VERSION = "2.1.0" +VERSION = "2.1.1" with open('README.rst', 'r') as f: long_description = f.read() diff --git a/sinric/__init__.py b/sinric/__init__.py index 940c48b..976aed1 100644 --- a/sinric/__init__.py +++ b/sinric/__init__.py @@ -7,5 +7,4 @@ from ._sinricpro import SinricPro from ._sinricproudp import SinricProUdp -from ._mainqueue import queue from ._events import Events, eventNames diff --git a/sinric/_cbhandler.py b/sinric/_cbhandler.py index 85b3f08..8979183 100644 --- a/sinric/_cbhandler.py +++ b/sinric/_cbhandler.py @@ -17,11 +17,8 @@ from ._tvcontorller import TvController from ._speakerController import SpeakerController from json import dumps, load, dump -from time import time +from time import time, sleep from uuid import uuid4 -from base64 import b64encode, b64decode -import hmac as sinricHmac -from hashlib import sha256 from ._dataTracker import DataTracker from ._lockController import LockStateController from ._signature import Signature @@ -289,7 +286,10 @@ def jsnHandle(action, resp, dataDict) -> dict: assert (self.verifySignature(jsn.get('payload'), jsn.get("signature").get("HMAC"))) resp, value = await self.targetTemperature(jsn, self.callbacks.get('targetTemperature')) response = jsnHandle(action="targetTemperature", resp=resp, dataDict={ - "duration": "" + "schedule": { + "duration": "PT4H" + }, + "temperature": value }) if self.enable_track: self.data_tracker.writeData('temperature', value) @@ -551,74 +551,93 @@ def jsnHandle(action, resp, dataDict) -> dict: self.logger.info('Sending Doorbell Event Response') await connection.send(dumps(jsn)) + elif Trace == 'temp_hum_event_response': self.logger.info('Sending temperature humidity response') await connection.send(dumps(jsn)) + elif Trace == 'setpowerstate_event_response': self.logger.info('Sending setpowerstate_event_response') await connection.send(dumps(jsn)) + elif Trace == 'setPowerLevel_event_response': self.logger.info('Sending setPowerLevel_event_response') await connection.send(dumps(jsn)) + elif Trace == 'setBrightness_event_response': self.logger.info('Sending setBrightness_event_response') await connection.send(dumps(jsn)) + elif Trace == 'setColor_event_response': self.logger.info('Sending setColor_event_response') await connection.send(dumps(jsn)) + elif Trace == 'setColorTemperature_event_response': self.logger.info('Sending setColorTemperature_event_response') await connection.send(dumps(jsn)) + elif Trace == 'setThermostatMode_event_response': self.logger.info('Sending setThermostatMode_event_response') await connection.send(dumps(jsn)) + elif Trace == 'setRangeValue_event_response': self.logger.info('Sending setRangeValue_event_response') await connection.send(dumps(jsn)) + elif Trace == 'motion_event_response': self.logger.info('Sending motion_event_response') await connection.send(dumps(jsn)) + elif Trace == 'contact_event_response': self.logger.info('Sending contact_event_response') await connection.send(dumps(jsn)) + elif Trace == 'set_volume_event_response': self.logger.info('Sending set_volume_event_response') await connection.send(dumps(jsn)) + elif Trace == 'select_input_event_response': self.logger.info('Sending select_input_event_response') await connection.send(dumps(jsn)) + elif Trace == 'media_control_event_response': self.logger.info('Sending media_control_event_response') await connection.send(dumps(jsn)) + elif Trace == 'change_channel_event_response': self.logger.info('Sending change_channel_event_response') await connection.send(dumps(jsn)) + elif Trace == 'set_bands_event_response': self.logger.info('Sending set_bands_event_response') await connection.send(dumps(jsn)) + + elif Trace == 'set_mode_event_response': self.logger.info('Sending set_mode_event_response') await connection.send(dumps(jsn)) + elif Trace == 'set_lock_event_response': self.logger.info('Sending set_lock_event_response') await connection.send(dumps(jsn)) + elif Trace == 'reset_bands_event_response': self.logger.info('Sending reset_bands_event_response') await connection.send(dumps(jsn)) diff --git a/sinric/_events.py b/sinric/_events.py index acbaad5..3bd76e7 100644 --- a/sinric/_events.py +++ b/sinric/_events.py @@ -5,7 +5,7 @@ * This file is part of the Sinric Pro (https://github.com/sinricpro/) """ -from time import time +from time import time, sleep from ._mainqueue import queue from ._jsoncommands import JSON_COMMANDS import uuid @@ -21,6 +21,7 @@ class Events(Signature): def __init__(self, connection, logger=None, secretKey=""): self.connection = connection + self.eventSleepTime = 2 self.logger = logger self.secretKey = secretKey Signature.__init__(self, self.secretKey) @@ -43,7 +44,7 @@ def jsnHandle(action, deviceId, value) -> dict: }, "createdAt": int(time()), "deviceId": deviceId, - "replyToken": str(uuid), + "replyToken": str(uuid.uuid4()), "type": "event", "value": value } @@ -55,6 +56,8 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setPowerState", deviceId, {"state": data.get("state", "Off")}), 'setpowerstate_event_response']) + sleep(self.eventSleepTime) + elif event_name == JSON_COMMANDS.get('SETPOWERLEVEL'): self.logger.info('setPowerLevel event raised') @@ -62,6 +65,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setPowerLevel", deviceId, { "powerLevel": data.get('powerLevel') }), 'setPowerLevel_event_response']) + sleep(self.eventSleepTime) elif event_name == JSON_COMMANDS.get('SETBRIGHTNESS'): self.logger.info('setBrightness event raised') @@ -69,6 +73,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setBrightness", deviceId, { "powerLevel": data.get('brightness') }), 'setBrightness_event_response']) + sleep(self.eventSleepTime) elif event_name == JSON_COMMANDS.get('SETCOLOR'): self.logger.info('setColor event raised') @@ -80,6 +85,7 @@ def jsnHandle(action, deviceId, value) -> dict: "b": data.get('b') } }), 'setColor_event_response']) + sleep(self.eventSleepTime) elif event_name == JSON_COMMANDS.get('SETCOLORTEMPERATURE'): @@ -88,6 +94,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setColorTemperature", deviceId, { "colorTemperature": 2400 }), 'setColorTemperature_event_response']) + sleep(self.eventSleepTime) ##########################DOOR BELL EVENT#################################### @@ -96,6 +103,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("DoorbellPress", deviceId, { "state": "pressed" }), 'doorbell_event_response']) + sleep(self.eventSleepTime) elif event_name == 'temperatureHumidityEvent': self.logger.info('Raised TH event') @@ -103,12 +111,14 @@ def jsnHandle(action, deviceId, value) -> dict: "temperature": data.get('temperature'), "humidity": data.get('humidity') }), 'temp_hum_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setThermostatMode': self.logger.info("Raised Thermostat event") queue.put([jsnHandle("setThermostatMode", deviceId, { "thermostatMode": data.get('Mode') }), 'setThermostatMode_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setRangeValue': self.logger.info('Raised Range value event') @@ -116,6 +126,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setRangeValue", deviceId, { "rangeValue": data.get('rangeValue') }), 'setRangeValue_event_response']) + sleep(self.eventSleepTime) elif event_name == 'motion': self.logger.info('Raised motion event') @@ -123,6 +134,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("motion", deviceId, { "state": data.get('state') }), 'motion_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setContactState': @@ -131,6 +143,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setContactState", deviceId, { "state": data.get('state') }), 'contact_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setVolume': @@ -139,6 +152,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setVolume", deviceId, { "volume": data.get('volume') }), 'set_volume_event_response']) + sleep(self.eventSleepTime) elif event_name == 'selectInput': self.logger.info('Raised select input event') @@ -146,6 +160,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("selectInput", deviceId, { "input": data.get('input') }), 'select_input_event_response']) + sleep(self.eventSleepTime) elif event_name == 'mediaControl': @@ -154,6 +169,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("mediaControl", deviceId, { "control": data.get('control') }), 'media_control_event_response']) + sleep(self.eventSleepTime) elif event_name == 'changeChannel': @@ -164,6 +180,7 @@ def jsnHandle(action, deviceId, value) -> dict: "name": data.get('name') } }), 'change_channel_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setBands': self.logger.info('Set Bands event raised') @@ -175,12 +192,14 @@ def jsnHandle(action, deviceId, value) -> dict: } ] }), 'set_bands_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setMode': self.logger.info('Set Mode event raised') queue.put([jsnHandle("setMode", deviceId, { "mode": data.get('mode') }), 'set_mode_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setLockState': self.logger.info('setLockState event raised') @@ -188,6 +207,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setLockState", deviceId, { "state": data.get('state') }), 'set_lock_event_response']) + sleep(self.eventSleepTime) elif event_name == 'resetBands': self.logger.info('resetBands event raised') @@ -207,6 +227,7 @@ def jsnHandle(action, deviceId, value) -> dict: "level": 0 }] }), 'reset_bands_event_response']) + sleep(self.eventSleepTime) elif event_name == 'setMute': self.logger.info('setMute event raised') @@ -214,6 +235,7 @@ def jsnHandle(action, deviceId, value) -> dict: queue.put([jsnHandle("setMute", deviceId, { "mute": data.get('mute', False) }), 'reset_bands_event_response']) + sleep(self.eventSleepTime) except Exception: self.logger.exception('Error Occurred') diff --git a/sinric/_sinricpro.py b/sinric/_sinricpro.py index c89179a..7d2063e 100644 --- a/sinric/_sinricpro.py +++ b/sinric/_sinricpro.py @@ -35,7 +35,7 @@ def __init__(self, api, deviceid, request_callbacks, event_callbacks=None, enabl sys.exit(0) def verifyDeviceIdArr(self,deviceIdArr): - Arr = deviceIdArr.split(';') + Arr = deviceIdArr for i in Arr: res = re.findall(r'^[a-fA-F0-9]{24}$',i) if len(res) == 0: diff --git a/sinric/_sinricprosocket.py b/sinric/_sinricprosocket.py index d27125b..1927cf9 100644 --- a/sinric/_sinricprosocket.py +++ b/sinric/_sinricprosocket.py @@ -30,7 +30,7 @@ def __init__(self, appKey, deviceId, callbacks, enable_trace=False, logger=None, async def connect(self): # Producer self.connection = await websockets.client.connect('ws://ws.sinric.pro', extra_headers={'appkey': self.appKey, - 'deviceids': self.deviceIds, + 'deviceids': ';'.join(self.deviceIds), 'platform': 'python'}, ping_interval=30000, ping_timeout=10000) if self.connection.open: diff --git a/sinric/_sinricproudp.py b/sinric/_sinricproudp.py index 64d79ad..4377ee7 100644 --- a/sinric/_sinricproudp.py +++ b/sinric/_sinricproudp.py @@ -12,8 +12,8 @@ class SinricProUdp: - def __init__(self, callbacks1, deviceIdArr,enable_trace=False): - self.callbacks = callbacks1 + def __init__(self, callbacks_udp, deviceIdArr,enable_trace=False): + self.callbacks = callbacks_udp self.deviceIdArr = deviceIdArr self.enablePrint = enable_trace self.udp_ip = '224.9.9.9'