Skip to content

Commit af9cc78

Browse files
authored
Refactor error handling configuration in DbusService to use constants and update several places accordingly to the code review
1 parent a5efa71 commit af9cc78

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
DTUVARIANT_TEMPLATE = "template"
88
PRODUCTNAME = "henne49_dbus-opendtu"
99
CONNECTION = "TCP/IP (HTTP)"
10+
MODE_TIMEOUT = "timeout"
11+
MODE_RETRYCOUNT = "retrycount"
1012

1113
# Status codes for the DTU
1214
STATUSCODE_STARTUP = 0

dbus_service.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def _read_config_dtu(self, actual_inverter):
222222
self.pollinginterval = int(get_config_value(config, "ESP8266PollingIntervall", "DEFAULT", "", 10000))
223223
self.meter_data = 0
224224
self.httptimeout = get_default_config(config, "HTTPTimeout", 2.5)
225-
self._get_error_handling_config(config)
225+
self._load_error_handling_config(config)
226226

227227
def _read_config_template(self, template_number):
228228
config = self._get_config()
@@ -276,12 +276,12 @@ def _read_config_template(self, template_number):
276276
self.dry_run = is_true(get_default_config(config, "DryRun", False))
277277
self.meter_data = 0
278278
self.httptimeout = get_default_config(config, "HTTPTimeout", 2.5)
279-
self._get_error_handling_config(config)
279+
self._load_error_handling_config(config)
280280

281-
def _get_error_handling_config(self, config):
281+
def _load_error_handling_config(self, config):
282282
'''Loads error handling configuration values from the provided config object.'''
283283

284-
self.error_mode = get_default_config(config, "ErrorMode", "retrycount").strip()
284+
self.error_mode = get_default_config(config, "ErrorMode", constants.MODE_RETRYCOUNT).strip()
285285
self.retry_after_seconds = int(get_default_config(config, "RetryAfterSeconds", 180))
286286
self.min_retries_until_fail = int(get_default_config(config, "MinRetriesUntilFail", 3))
287287
self.error_state_after_seconds = int(get_default_config(config, "ErrorStateAfterSeconds", 0))
@@ -588,7 +588,7 @@ def update(self):
588588
successful = False
589589
now = time.time()
590590
try:
591-
if self.error_mode == "timeout" and self.error_state_after_seconds > 0:
591+
if self.error_mode == constants.MODE_TIMEOUT and self.error_state_after_seconds > 0:
592592
# Set zero values only after ErrorStateAfterSeconds has elapsed since last success
593593
if (not self.last_update_successful and (now - self._last_update) >= self.error_state_after_seconds):
594594
self._handle_reconnect_wait()
@@ -598,11 +598,23 @@ def update(self):
598598
# In normal operation (no error), always call _refresh_data on every update
599599
if self.last_update_successful:
600600
successful = self._refresh_and_update()
601-
elif self.error_mode == "retrycount":
601+
elif self.error_mode == constants.MODE_RETRYCOUNT:
602602
# Classic retry-count-based error handling
603603
if self.failed_update_count >= self.min_retries_until_fail:
604604
self._handle_reconnect_wait()
605-
if self._should_refresh_data(now):
605+
# Determine if we should refresh data based on current state and timing
606+
is_last_update_successful = self.last_update_successful
607+
time_since_last_update = now - self._last_update
608+
is_retry_interval_elapsed = time_since_last_update >= self.retry_after_seconds
609+
is_below_min_retries = self.failed_update_count < self.min_retries_until_fail
610+
611+
should_refresh_data = (
612+
is_last_update_successful or
613+
is_retry_interval_elapsed or
614+
is_below_min_retries
615+
)
616+
617+
if should_refresh_data:
606618
successful = self._refresh_and_update()
607619
except requests.exceptions.RequestException as exception:
608620
logging.warning(f"HTTP Error at _update for inverter "

tests/test_dbus_service.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import json
88
import requests
9+
from constants import MODE_TIMEOUT
910
from dbus_service import DbusService
1011

1112

@@ -354,7 +355,7 @@ def test_statuscode_set_on_reconnect_and_reset(self):
354355

355356
def test_timeout_mode_no_zero_before_timeout(self):
356357
"""If ErrorMode=timeout and error_state_after_seconds=600, before 10min no zero/StatusCode=10 is sent."""
357-
self.service.error_mode = "timeout"
358+
self.service.error_mode = MODE_TIMEOUT
358359
self.service.error_state_after_seconds = 600 # 10 minutes
359360
self.service.last_update_successful = False
360361
self.service._last_update = time.time() - 300 # 5 minutes ago
@@ -367,7 +368,7 @@ def test_timeout_mode_no_zero_before_timeout(self):
367368

368369
def test_timeout_mode_zero_after_timeout(self):
369370
"""If ErrorMode=timeout and error_state_after_seconds=600, after 10min zero/StatusCode=10 is sent."""
370-
self.service.error_mode = "timeout"
371+
self.service.error_mode = MODE_TIMEOUT
371372
self.service.error_state_after_seconds = 600 # 10 minutes
372373
self.service.last_update_successful = False
373374
self.service._last_update = time.time() - 601 # just over 10 minutes ago
@@ -381,7 +382,7 @@ def test_timeout_mode_zero_after_timeout(self):
381382

382383
def test_timeout_mode_timer_resets_on_success(self):
383384
"""If in timeout mode a successful update occurs in between, the timer is reset and no zero values are sent."""
384-
self.service.error_mode = "timeout"
385+
self.service.error_mode = MODE_TIMEOUT
385386
self.service.error_state_after_seconds = 600 # 10 Minuten
386387
self.service.last_update_successful = False
387388
self.service._last_update = time.time() - 601 # Über Timeout, würde Nullwerte senden
@@ -410,7 +411,7 @@ def test_normal_operation_successful_update(self):
410411

411412
def test_normal_operation_successful_update_timeout_mode(self):
412413
"""Test that in timeout mode, normal operation calls all expected methods and resets error state."""
413-
self.service.error_mode = "timeout"
414+
self.service.error_mode = MODE_TIMEOUT
414415
self.service.error_state_after_seconds = 600 # 10 minutes
415416
self.service.failed_update_count = 0
416417
self.service.last_update_successful = True

0 commit comments

Comments
 (0)