From c4bda51c731e0771d4c8c112f23ec38d04ddee25 Mon Sep 17 00:00:00 2001 From: Michael Hill Date: Mon, 29 Oct 2018 14:32:03 -0400 Subject: [PATCH 1/3] Added retry logic to improve state change success --- pytuya/__init__.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pytuya/__init__.py b/pytuya/__init__.py index 737b420..91611d5 100644 --- a/pytuya/__init__.py +++ b/pytuya/__init__.py @@ -163,14 +163,18 @@ def _send_receive(self, payload): Args: payload(bytes): Data to send. """ - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - s.settimeout(self.connection_timeout) - s.connect((self.address, self.port)) - s.send(payload) - data = s.recv(1024) - s.close() - return data + for i in range(3): + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + s.settimeout(self.connection_timeout) + s.connect((self.address, self.port)) + s.send(payload) + data = s.recv(1024) + s.close() + return data + except socket.error: + continue def generate_payload(self, command, data=None): """ From cb236da4aa38fa398f238adc1e12003e72114094 Mon Sep 17 00:00:00 2001 From: Michael Hill Date: Mon, 29 Oct 2018 14:53:03 -0400 Subject: [PATCH 2/3] Added if/else to exception to raise error --- pytuya/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pytuya/__init__.py b/pytuya/__init__.py index 91611d5..2660433 100644 --- a/pytuya/__init__.py +++ b/pytuya/__init__.py @@ -173,8 +173,11 @@ def _send_receive(self, payload): data = s.recv(1024) s.close() return data - except socket.error: - continue + except socket.error as e: + if i == 2: + raise ConnectionError(e) + else: + continue def generate_payload(self, command, data=None): """ From f624ef03490995c6780ffa7edccfccc9d885a3a8 Mon Sep 17 00:00:00 2001 From: Michael Hill Date: Tue, 30 Oct 2018 12:35:16 -0400 Subject: [PATCH 3/3] Moved retry logic to set_status function --- pytuya/__init__.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/pytuya/__init__.py b/pytuya/__init__.py index 2660433..5319aff 100644 --- a/pytuya/__init__.py +++ b/pytuya/__init__.py @@ -163,21 +163,14 @@ def _send_receive(self, payload): Args: payload(bytes): Data to send. """ - for i in range(3): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - s.settimeout(self.connection_timeout) - s.connect((self.address, self.port)) - s.send(payload) - data = s.recv(1024) - s.close() - return data - except socket.error as e: - if i == 2: - raise ConnectionError(e) - else: - continue + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + s.settimeout(self.connection_timeout) + s.connect((self.address, self.port)) + s.send(payload) + data = s.recv(1024) + s.close() + return data def generate_payload(self, command, data=None): """ @@ -305,9 +298,15 @@ def set_status(self, on, switch=1): switch = str(switch) # index and payload is a string payload = self.generate_payload(SET, {switch:on}) #print('payload %r' % payload) - - data = self._send_receive(payload) - log.debug('set_status received data=%r', data) + for i in range(3): + try: + data = self._send_receive(payload) + log.debug('set_status received data=%r', data) + except Exception: + if i == 2: + raise Exception('set_status failed after 3 consecutive attempts') + else: + continue return data