Skip to content

Commit 5b4cb04

Browse files
committed
Reqork error_interval
1 parent ee30605 commit 5b4cb04

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

__init__.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
UPDATE_TOPIC = f"{DOMAIN}_update"
2626
SCAN_INTERVAL = timedelta(seconds=10)
2727
ERROR_INTERVAL = timedelta(seconds=300)
28-
MAX_FAILS = 10
28+
ERROR_ITERVAL_MAPPING = [10, 60, 300, 600, 3000, 6000]
2929
NOTIFICATION_ID = "ph803w_device_notification"
3030
NOTIFICATION_TITLE = "PH-803W Device status"
3131

@@ -80,6 +80,7 @@ def __init__(self, hass, device_client: device.Device) -> None:
8080
self.hass = hass
8181
self.device_client = device_client
8282
self.device_client.register_callback(self.dispatcher_new_data)
83+
self.device_client.register_callback(self.reset_fail_counter)
8384
self.host = self.device_client.host
8485
self._shutdown = False
8586
self._fails = 0
@@ -113,30 +114,26 @@ def shutdown(event):
113114
_LOGGER.debug("Graceful shutdown")
114115
return
115116

116-
if self._fails > MAX_FAILS:
117-
_LOGGER.error("Failed to reconnect. Thread stopped")
118-
persistent_notification.create(
119-
self.hass,
120-
"Error:<br/>Connection to PH-803W device failed "
121-
"the maximum number of times. Thread has stopped",
122-
title=NOTIFICATION_TITLE,
123-
notification_id=NOTIFICATION_ID,
124-
)
125-
return
126-
127117
try:
128118
self.device_client.run(once=False)
129-
except device.DeviceError:
119+
except (device.DeviceError, ConnectionError):
130120
_LOGGER.exception("Failed to read data, attempting to recover")
131121
self.device_client.close()
132122
self._fails += 1
133-
sleep_time = self._fails * ERROR_INTERVAL.total_seconds()
123+
error_mapping = self._fails
124+
if error_mapping >= len(ERROR_ITERVAL_MAPPING):
125+
error_mapping = len(ERROR_ITERVAL_MAPPING) - 1
126+
sleep_time = timedelta(seconds=ERROR_ITERVAL_MAPPING[error_mapping])
134127
_LOGGER.debug(
135128
"Sleeping for fail #%s, in %s seconds", self._fails, sleep_time
136129
)
137130
self.device_client.reset_socket()
138131
time.sleep(sleep_time)
139132

133+
@callback
134+
def reset_fail_counter(self):
135+
self._fails = 0
136+
140137
@callback
141138
def dispatcher_new_data(self):
142139
"""Noyifying HASS that new data is ready to read."""

0 commit comments

Comments
 (0)