Skip to content

Commit

Permalink
Add read_multiplier field
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Pikuła <[email protected]>
  • Loading branch information
MarekPikula committed Aug 2, 2023
1 parent eaea8bc commit 209ae17
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 321 deletions.
9 changes: 9 additions & 0 deletions hamqtt_fs_publisher/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def publish_read(self):
state = self._config.read_path.read_text("utf-8")
if self._config.read_strip:
state = state.strip()
if self._config.read_multiplier is not None:
try:
state = float(state) * self._config.read_multiplier
except ValueError:
logger.error(
"{}: Failed to apply multiplication due to float conversion error.",
self._log_name,
)
logger.debug('{}: Publishing state "{}".', self._log_name, state)
self._entity.publish_state(state)
if not self._stop_event.is_set():
Expand Down Expand Up @@ -88,6 +96,7 @@ def main(config: Path):
logger.info("Parsing configuration...")
config_found = False
config_default_dir = Path("hamqtt_fs_publisher")
# TODO: Search for both config.json and yaml if not defined.
for prefix in (
Path(),
Path("/etc") / config_default_dir,
Expand Down
3 changes: 3 additions & 0 deletions hamqtt_fs_publisher/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ReadMixin(BaseModel): # pylint: disable=R0903
read_strip: bool = Field(
True, description="Perform a strip operation on the read value."
)
read_multiplier: Optional[float] = Field(
None, description="Multiply read value by given amount."
)


class WriteMixin(BaseModel): # pylint: disable=R0903
Expand Down
5 changes: 5 additions & 0 deletions hamqtt_fs_publisher/configuration.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@
"default": true,
"type": "boolean"
},
"read_multiplier": {
"title": "Read Multiplier",
"description": "Multiply read value by given amount.",
"type": "number"
},
"unit": {
"title": "Unit",
"description": "Unit of measurement, example: \"\u00b0C\",",
Expand Down
Loading

0 comments on commit 209ae17

Please sign in to comment.