Http timeout = 5 sec and better retry mechanism
This commit is contained in:
47
plugin.py
47
plugin.py
@@ -4,7 +4,7 @@
|
||||
#
|
||||
#pylint: disable=line-too-long,broad-exception-caught,possibly-used-before-assignment
|
||||
"""
|
||||
<plugin key="HeatzyEx" name="Heatzy Pilote Ex" author="fjumelle" version="2.1.0" wikilink="" externallink="">
|
||||
<plugin key="HeatzyEx" name="Heatzy Pilote Ex" author="fjumelle" version="2.2.0" wikilink="" externallink="">
|
||||
<description>
|
||||
<h2>Heatzy Pilote</h2><br/>
|
||||
Implementation of Heatzy Pilote as a Domoticz Plugin.<br/>
|
||||
@@ -77,6 +77,9 @@ class HeatzyUnit(IntEnum):
|
||||
|
||||
class BasePlugin:
|
||||
"""Class for plugin"""
|
||||
_HTTP_TIMEOUT = 5
|
||||
_MAX_RETRY_PER_DEVICE = 3
|
||||
|
||||
debug = False
|
||||
token = ""
|
||||
token_expire_at = 0
|
||||
@@ -86,8 +89,7 @@ class BasePlugin:
|
||||
pooling = 30
|
||||
pooling_steps = 1
|
||||
pooling_current_step = 1
|
||||
max_retry = 6
|
||||
retry = max_retry
|
||||
retry = 0
|
||||
|
||||
def __init__(self):
|
||||
return
|
||||
@@ -119,6 +121,9 @@ class BasePlugin:
|
||||
# Get Devide Id
|
||||
self.did = self.get_heatzy_devices()
|
||||
|
||||
# max retry per device
|
||||
self.retry = self._MAX_RETRY_PER_DEVICE * len(self.did)
|
||||
|
||||
# Create the child devices if these do not exist yet
|
||||
for deviceid in self.did:
|
||||
if deviceid not in Devices:
|
||||
@@ -154,11 +159,11 @@ class BasePlugin:
|
||||
def on_heartbeat(self):
|
||||
"""Time to heartbeat :)"""
|
||||
if self.pooling_current_step >= self.pooling_steps:
|
||||
Domoticz.Debug(f"Retry counter:{self.retry}")
|
||||
Domoticz.Debug(f"Retry counter: {self.retry}")
|
||||
if self.retry < 0:
|
||||
Domoticz.Status("No connection to Heatzy API ==> Device disabled for 15 minutes")
|
||||
self.pooling_current_step = - 15 * 60 // self.pooling + self.pooling_steps
|
||||
self.retry = self.max_retry
|
||||
self.reset_retry()
|
||||
#Force refresh token/did
|
||||
Domoticz.Status("Force refresh token and device id.")
|
||||
self.token = ""
|
||||
@@ -194,7 +199,7 @@ class BasePlugin:
|
||||
try:
|
||||
time.sleep(0.5)
|
||||
url = 'https://euapi.gizwits.com/app/login'
|
||||
response = requests.post(url, headers=headers, data=data, timeout=3).json()
|
||||
response = requests.post(url, headers=headers, data=data, timeout=self._HTTP_TIMEOUT).json()
|
||||
except Exception as exc:
|
||||
Domoticz.Error("Cannot open connection to Heatzy API to get the token: " + str(exc))
|
||||
#Domoticz.Error("URL: " + str(url))
|
||||
@@ -211,7 +216,7 @@ class BasePlugin:
|
||||
self.token_expire_at = response['expire_at']
|
||||
Domoticz.Status("Token from Heatzy API: " + self.token)
|
||||
#Reset retry counter
|
||||
self.retry = self.max_retry
|
||||
self.reset_retry()
|
||||
else:
|
||||
error_code = "Unknown" if 'error_code' not in response else response['error_code']
|
||||
error_message = "Unknown" if 'error_message' not in response else response['error_message']
|
||||
@@ -240,7 +245,7 @@ class BasePlugin:
|
||||
params = (('limit', '20'), ('skip', '0'),)
|
||||
url = 'https://euapi.gizwits.com/app/bindings'
|
||||
try:
|
||||
response = requests.get(url, headers=headers, params=params, timeout=3).json()
|
||||
response = requests.get(url, headers=headers, params=params, timeout=self._HTTP_TIMEOUT).json()
|
||||
except Exception as exc:
|
||||
Domoticz.Error("Cannot open connection to Heatzy API to get the device id: " + str(exc))
|
||||
#Domoticz.Error("URL: " + str(url))
|
||||
@@ -289,19 +294,17 @@ class BasePlugin:
|
||||
|
||||
url = f"https://euapi.gizwits.com/app/devdata/{did}/latest"
|
||||
try:
|
||||
response = requests.get(url, headers=headers, timeout=3).json()
|
||||
response = requests.get(url, headers=headers, timeout=self._HTTP_TIMEOUT).json()
|
||||
except Exception as exc:
|
||||
#Decrease retry
|
||||
self.retry = self.retry - 1
|
||||
|
||||
if self.retry < self.max_retry//2:
|
||||
Domoticz.Error(f"Cannot open connection to Heatzy API to get the mode for {alias}: {exc}")
|
||||
#Domoticz.Error("URL: " + str(url))
|
||||
#Domoticz.Error("Headers: " + str(headers))
|
||||
if 'response' in locals() and response != "":
|
||||
Domoticz.Error("Response: " + str(response))
|
||||
else:
|
||||
Domoticz.Debug(f"Cannot open connection to Heatzy API to get the mode for {alias}: {exc}")
|
||||
Domoticz.Error(f"Cannot open connection to Heatzy API to get the mode for {alias} (retry={self.retry}): {exc}")
|
||||
#Domoticz.Error("URL: " + str(url))
|
||||
#Domoticz.Error("Headers: " + str(headers))
|
||||
if 'response' in locals() and response != "":
|
||||
Domoticz.Error("Response: " + str(response))
|
||||
|
||||
continue
|
||||
|
||||
Domoticz.Debug(f"Get Mode Response for {alias}: {response}")
|
||||
@@ -326,7 +329,7 @@ class BasePlugin:
|
||||
Domoticz.Debug(f"Current Heatzy Mode: {HEATZY_MODE_NAME[mode]} ({alias})")
|
||||
|
||||
#Reset retry counter
|
||||
self.retry = self.max_retry
|
||||
self.reset_retry()
|
||||
|
||||
if Devices[deviceid].Units[HeatzyUnit.SELECTOR].nValue != HEATZY_MODE_VALUE[mode]:
|
||||
Domoticz.Status(f"New Heatzy Mode: {HEATZY_MODE_NAME[mode]} ({alias})")
|
||||
@@ -394,7 +397,7 @@ class BasePlugin:
|
||||
did = self.did[deviceid]["did"]
|
||||
url = f"https://euapi.gizwits.com/app/control/{did}"
|
||||
try:
|
||||
response = requests.post(url, headers=headers, data=data, timeout=3).json()
|
||||
response = requests.post(url, headers=headers, data=data, timeout=self._HTTP_TIMEOUT).json()
|
||||
except Exception as exc:
|
||||
Domoticz.Error("Cannot open connection to Heatzy API to set the mode: " + str(exc))
|
||||
#Domoticz.Error("URL: " + str(url))
|
||||
@@ -437,7 +440,7 @@ class BasePlugin:
|
||||
return
|
||||
|
||||
#Device is correctly running ==> we reset the retry counter
|
||||
self.retry = self.max_retry
|
||||
self.reset_retry()
|
||||
|
||||
def on_off(self, deviceid, command):
|
||||
"""Toggle device on/off"""
|
||||
@@ -451,6 +454,10 @@ class BasePlugin:
|
||||
#Because of issue with the equipment (Off do not work...)
|
||||
self.set_mode(deviceid, HEATZY_MODE_VALUE['FROSTFREE'])
|
||||
|
||||
def reset_retry(self):
|
||||
"""Reset the retry counter"""
|
||||
self.retry = self._MAX_RETRY_PER_DEVICE * len(self.did)
|
||||
|
||||
_plugin = BasePlugin()
|
||||
|
||||
def onStart(): #NOSONAR #pylint: disable=invalid-name
|
||||
|
||||
Reference in New Issue
Block a user