Skip to content

Commit e57e234

Browse files
authored
Merge pull request #14 from dala318/error_interval
Rework error_interval
2 parents 33a501c + 72905dd commit e57e234

2 files changed

Lines changed: 14 additions & 22 deletions

File tree

__init__.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
_LOGGER = logging.getLogger(__name__)
2424

2525
UPDATE_TOPIC = f"{DOMAIN}_update"
26-
SCAN_INTERVAL = timedelta(seconds=10)
27-
ERROR_INTERVAL = timedelta(seconds=300)
28-
MAX_FAILS = 10
26+
ERROR_ITERVAL_MAPPING = [10, 60, 300, 600, 3000, 6000]
2927
NOTIFICATION_ID = "ph803w_device_notification"
3028
NOTIFICATION_TITLE = "PH-803W Device status"
3129

@@ -80,6 +78,7 @@ def __init__(self, hass, device_client: device.Device) -> None:
8078
self.hass = hass
8179
self.device_client = device_client
8280
self.device_client.register_callback(self.dispatcher_new_data)
81+
self.device_client.register_callback(self.reset_fail_counter)
8382
self.host = self.device_client.host
8483
self._shutdown = False
8584
self._fails = 0
@@ -113,30 +112,26 @@ def shutdown(event):
113112
_LOGGER.debug("Graceful shutdown")
114113
return
115114

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-
127115
try:
128116
self.device_client.run(once=False)
129-
except device.DeviceError:
117+
except (device.DeviceError, ConnectionError):
130118
_LOGGER.exception("Failed to read data, attempting to recover")
131119
self.device_client.close()
132120
self._fails += 1
133-
sleep_time = self._fails * ERROR_INTERVAL.total_seconds()
121+
error_mapping = self._fails
122+
if error_mapping >= len(ERROR_ITERVAL_MAPPING):
123+
error_mapping = len(ERROR_ITERVAL_MAPPING) - 1
124+
sleep_time = ERROR_ITERVAL_MAPPING[error_mapping]
134125
_LOGGER.debug(
135126
"Sleeping for fail #%s, in %s seconds", self._fails, sleep_time
136127
)
137128
self.device_client.reset_socket()
138129
time.sleep(sleep_time)
139130

131+
@callback
132+
def reset_fail_counter(self):
133+
self._fails = 0
134+
140135
@callback
141136
def dispatcher_new_data(self):
142137
"""Noyifying HASS that new data is ready to read."""

lib/device.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@ async def run_async(self, once: bool = True) -> bool:
4141

4242
def run(self, once: bool = True) -> bool:
4343
self._loop = True
44+
if self._socket.fileno() == -1:
45+
self.reset_socket()
46+
self._connect()
4447
if once:
45-
if self._socket.fileno() == -1:
46-
self.reset_socket()
47-
self._connect()
4848
return self._run(once)
4949
else:
50-
if self._socket.fileno() == -1:
51-
self.reset_socket()
52-
self._connect()
5350
self._run(once)
5451
return not self._loop
5552

0 commit comments

Comments
 (0)