diff --git a/CHANGELOG.md b/CHANGELOG.md index 20ef5d6..5fbc3e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [2024.09.1] - 2024-09-10 Feature Release + +- Write to register support (using params: { value: int } on rpc topic). + ## [2024.03.3] - 2024-03-29 Feature Release - Fix pyproject version diff --git a/modbus2mqtt.py b/modbus2mqtt.py index c0dd201..11df3bf 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -295,7 +295,7 @@ def get_value(self, src): else: val = int(((float(val) - float(self.substract)) / float(self.divide))) - if (self.maxvalue and val > self.maxvalue) or (self.minvalue and val < self.minvalue): + if (self.maxvalue is not None and float(val) > float(self.maxvalue)) or (self.minvalue is not None and float(val) < float(self.minvalue)): return None return val @@ -308,11 +308,31 @@ def get_value(self, src): else: val = int(((int(val) - float(self.substract)) / float(self.divide))) - if (self.maxvalue and val > self.maxvalue) or (self.minvalue and val < self.minvalue): + if (self.maxvalue is not None and float(val) > float(self.maxvalue)) or (self.minvalue is not None and float(val) < float(self.minvalue)): return None return val + def set_value(self, src, params): + unitid = self.unitid + if unitid is None: + unitid = src.unitid + + value = params.get("value", None) + if value is None: + # Can't set unknown state + return False + + addr = self.start + log.debug(f"Writing register at address {addr} with value {value}.") + rr = src.client.write_register(addr, value, slave=unitid) + if not rr: + raise ModbusException("Received empty modbus respone.") + if rr.isError(): + raise ModbusException(f"Received Modbus library error({rr}).") + if isinstance(rr, ExceptionResponse): + raise ModbusException(f"Received Modbus library exception ({rr}).") + class Schema: diff --git a/pyproject.toml b/pyproject.toml index 4a7da8f..3102b84 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "modbus2mqtt" -version = "2024.03.3" +version = "2024.09.1" description = "Gateway between Modbus TCP devices and MQTT." authors = ["Marcus Zoller "] license = "MIT"