Skip to content

Commit

Permalink
feat: Support register write via rpc topic
Browse files Browse the repository at this point in the history
  • Loading branch information
mazocode committed Sep 10, 2024
1 parent 358db51 commit 9561e34
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 22 additions & 2 deletions modbus2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 9561e34

Please sign in to comment.