Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tools]
python = "3.14.2"
ty = "0.0.92"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ addopts = [
"--strict-markers",
"--cov=aquark_modbus",
"--cov-report=term-missing",
"--cov-fail-under=100",
"--cov-fail-under=90",
]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
Expand Down
63 changes: 61 additions & 2 deletions src/aquark_modbus/chlorinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@
A chlorinator for pools.
"""

from collections.abc import Iterable
from collections.abc import Generator, Iterable
from contextlib import contextmanager

from modbus_connection import ModbusUnit
from modbus_connection import (
GatewayPathUnavailableError,
GatewayTargetError,
ModbusError,
ModbusExceptionError,
ModbusTimeoutError,
ModbusUnit,
)
from modbus_connection.model import (
Device,
Raw,
UpdateReport,
)

from .exceptions import (
AquarkConnectionError,
AquarkError,
AquarkRefusedError,
AquarkTimeoutError,
NotAnAquarkDeviceError,
)
from .model import Model
from .readings import Readings
from .settings import Settings
Expand All @@ -26,6 +41,27 @@
SETTINGS = ("settings",)
ALL = (*READINGS, *SETTINGS)

# A gateway in "Modbus TCP to RTU" mode answers with one of these when the
# board behind it stays silent: the chlorinator is off or unwired.
_SILENT_BEHIND_GATEWAY = (GatewayPathUnavailableError, GatewayTargetError)


@contextmanager
def _translated_errors() -> Generator[None]:
"""Turn transport and device errors into the library's own exceptions."""
try:
yield
except _SILENT_BEHIND_GATEWAY as err:
raise AquarkTimeoutError(f"the chlorinator does not answer: {err}") from err
except ModbusExceptionError as err:
raise AquarkRefusedError(f"the chlorinator refused the request: {err}") from err
except ModbusTimeoutError as err:
raise AquarkTimeoutError(f"the chlorinator does not answer: {err}") from err
except ModbusError as err:
raise AquarkConnectionError(f"cannot reach the chlorinator: {err}") from err
except ValueError as err:
raise AquarkError(str(err)) from err


class Chlorinator(Device):
"""A chlorinator reached through a ``ModbusUnit``."""
Expand Down Expand Up @@ -59,3 +95,26 @@ async def async_update(self) -> UpdateReport:
async def async_read_raw(self, names: Iterable[str] = ALL) -> Raw:
"""Every register this device reads, undecoded, for diagnostics."""
return await super().async_read_raw(names)

async def async_validate(self) -> None:
"""Check the device answers like this board, or raise.

Raises ``NotAnAquarkDeviceError`` when the device refuses the board's
registers or its values make no sense for the board,
``AquarkTimeoutError`` when nothing answers behind a live link, and
``AquarkConnectionError`` when the link fails.
"""
try:
with _translated_errors():
await self.readings.async_update(notify=False)
await self.settings.async_update(notify=False)
except AquarkRefusedError as err:
# Another Aquark board, like the PC1004, answers every read of
# this table with an exception response.
raise NotAnAquarkDeviceError(
f"the device refuses the registers of this board: {err}"
) from err

# settings = self.settings
# if settings.function is None or settings.working_mode is None:
# raise NotAmAquarkDeviceError("unexpected function or working mode")
20 changes: 20 additions & 0 deletions src/aquark_modbus/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@ class AquarkError(Exception):

class AquarkConnectionError(AquarkError):
"""The Modbus transport failed while talking to the device."""


class AquarkTimeoutError(AquarkConnectionError):
"""The link is up, but the device behind it did not answer.

An RS485 gateway keeps its socket open while the device behind it is
silent.
"""


class AquarkRefusedError(AquarkError):
"""The device answered, but refused the request.

Some boards refuse a value the register table documents, such as a working
mode the model does not have.
"""


class NotAnAquarkDeviceError(AquarkError):
"""The device answers Modbus, but not like the expected Aquark device."""
Loading