From 39d5b0905c1ee773b827f0543106b4c0cb9de625 Mon Sep 17 00:00:00 2001 From: tixi Date: Wed, 3 Oct 2018 15:24:16 +0200 Subject: [PATCH] bug msg disconnect fix --- plugin.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/plugin.py b/plugin.py index cadc1b3..d3494ff 100644 --- a/plugin.py +++ b/plugin.py @@ -26,7 +26,7 @@ ######################################################################################## """ - + @@ -49,15 +49,16 @@ class BasePlugin: __UNIT = 1 __HB_BASE_FREQ = 2 + __VALID_CMD = ('status','On','Off') def __init__(self): - self.__address = None #ip address of the smartplug - self.__devID = None #devID of the smartplug - self.__localKey = None #localKey of the smartplug - self.__device = None #pytuya object of the smartplug - self.__runAgain = self.__HB_BASE_FREQ #heartbeat frequency (20 seconds) - self.__connection = None #connection to the tuya plug - self.__last_cmd = None #last command (None/"On"/"Off") + self.__address = None #IP address of the smartplug + self.__devID = None #devID of the smartplug + self.__localKey = None #localKey of the smartplug + self.__device = None #pytuya object of the smartplug + self.__runAgain = self.__HB_BASE_FREQ #heartbeat frequency (20 seconds) + self.__connection = None #connection to the tuya plug + self.__last_cmd = None #last command (None/'On'/'Off'/'status') return @@ -96,7 +97,8 @@ class BasePlugin: if(self.__last_cmd != None): self.__command_to_execute(self.__last_cmd) else: - self.__connection.Disconnect() + if(self.__connection.Connected()): + self.__connection.Disconnect() self.__connection.Connect() @@ -161,6 +163,10 @@ class BasePlugin: def __command_to_execute(self,Command): + if(Command not in self.__VALID_CMD): + Domoticz.Error("Undefined command: " + Command) + return + if(Command == 'status'): if(self.__last_cmd == None): self.__last_cmd = Command @@ -176,12 +182,8 @@ class BasePlugin: payload = self.__device.generate_payload('set', {'1':False}) self.__connection.Send(payload) status_request = True - elif(Command == 'status'): + else: #(Command == 'status') status_request = True - else: - Domoticz.Error("Unknow Command received") - self.__last_cmd = None - status_request = False if(status_request): payload=self.__device.generate_payload('status') @@ -195,9 +197,7 @@ class BasePlugin: def onDisconnect(self, Connection): - Domoticz.Error("Disconnected from: "+Connection.Address+":"+Connection.Port) - #if (Connection == self.__connection): - #self.__connection.Connect() + Domoticz.Debug("Disconnected from: "+Connection.Address+":"+Connection.Port) def onHeartbeat(self): self.__runAgain -= 1 @@ -209,7 +209,8 @@ class BasePlugin: def onStop(self): self.__device = None self.__last_cmd = None - self.__connection.Disconnect() + if(self.__connection.Connected()): + self.__connection.Disconnect() self.__connection = None global _plugin