Skip to content

Commit 48c94ab

Browse files
committed
Simplify clamping logic.
1 parent 83c149a commit 48c94ab

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

modbus4mqtt/modbus4mqtt.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def setup_modbus(self):
8888
variant=self.config.get('variant', None),
8989
# Allow the use of the deprecated "scan_batching" config option for backwards compatibility
9090
read_batching=self.config.get('read_batching', self.config.get('scan_batching', None)),
91+
write_batching=self.config.get('write_batching', None),
9192
word_order=word_order)
9293
# Tells the modbus interface about the registers we consider interesting.
9394
for register in self.registers:
@@ -331,11 +332,17 @@ def _validate_registers(registers):
331332
raise ValueError("Bad YAML configuration. pub_topic '{}' has conflicting retain settings."
332333
.format(topic))
333334

335+
334336
def _load_modbus_config(self, path):
335337
yaml = YAML(typ='safe')
336338
result = yaml.load(open(path, 'r').read())
337339
registers = [register for register in result['registers'] if 'pub_topic' in register]
338340
mqtt_interface._validate_registers(registers)
341+
342+
if 'scan_batching' in result:
343+
logging.warning("As of modbus4mqtt v1.0.0, the 'scan_batching' config option is deprecated. "
344+
"It has been replaced with 'read_batching' and 'write_batching'."
345+
"However, to be nice, modbus4mqtt will still accept 'scan_batching' for now.")
339346
return result
340347

341348
def loop_forever(self):

modbus4mqtt/modbus_interface.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,13 @@ def __init__(self,
4949
self._read_batching: int = DEFAULT_READ_BATCHING
5050
self._write_batching: int = DEFAULT_WRITE_BATCHING
5151
self._word_order: WordOrder = word_order
52-
if read_batching is not None:
53-
if read_batching < MIN_BATCHING:
54-
logging.warning("Bad value for read_batching: {}. Enforcing minimum value of {}".format(read_batching, MIN_BATCHING))
55-
self._read_batching = MIN_BATCHING
56-
elif read_batching > MAX_BATCHING:
57-
logging.warning("Bad value for read_batching: {}. Enforcing maximum value of {}".format(read_batching, MAX_BATCHING))
58-
self._read_batching = MAX_BATCHING
59-
else:
60-
self._read_batching = read_batching
61-
if write_batching is not None:
62-
if write_batching < MIN_BATCHING:
63-
logging.warning("Bad value for write_batching: {}. Enforcing minimum value of {}".format(write_batching, MIN_BATCHING))
64-
self._write_batching = MIN_BATCHING
65-
elif write_batching > MAX_BATCHING:
66-
logging.warning("Bad value for write_batching: {}. Enforcing maximum value of {}".format(write_batching, MAX_BATCHING))
67-
self._write_batching = MAX_BATCHING
68-
else:
69-
self._write_batching = write_batching
70-
if self._write_mode == WriteMode.Single:
52+
if read_batching is not None and not (MIN_BATCHING <= read_batching <= MAX_BATCHING):
53+
logging.warning(f"Bad value for read_batching: {read_batching}. Enforcing limits of {MIN_BATCHING} to {MAX_BATCHING}.")
54+
self._read_batching = max(MIN_BATCHING, min(MAX_BATCHING, read_batching))
55+
if write_batching is not None and not (MIN_BATCHING <= write_batching <= MAX_BATCHING):
56+
logging.warning(f"Bad value for write_batching: {write_batching}. Enforcing limits of {MIN_BATCHING} to {MAX_BATCHING}.")
57+
self._write_batching = max(MIN_BATCHING, min(MAX_BATCHING, write_batching))
58+
if self._write_mode == WriteMode.Single and self._write_batching != 1:
7159
logging.warning("Overriding write batching to 1 due to single write mode.")
7260
self._write_batching = 1
7361
self._tables: dict[str, ModbusTable] = {

0 commit comments

Comments
 (0)