Http timeout = 5 sec and better retry mechanism

This commit is contained in:
2025-11-02 12:03:08 +01:00
parent 164f0c77ff
commit b01c09559e

View File

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