diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..41fa764 --- /dev/null +++ b/mise.toml @@ -0,0 +1,3 @@ +[tools] +python = "3.14.2" +ty = "0.0.92" diff --git a/pyproject.toml b/pyproject.toml index 13657d8..f18a222 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/src/aquark_modbus/chlorinator.py b/src/aquark_modbus/chlorinator.py index d822131..6f521a7 100644 --- a/src/aquark_modbus/chlorinator.py +++ b/src/aquark_modbus/chlorinator.py @@ -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 @@ -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``.""" @@ -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") diff --git a/src/aquark_modbus/exceptions.py b/src/aquark_modbus/exceptions.py index f49c73f..32729a9 100644 --- a/src/aquark_modbus/exceptions.py +++ b/src/aquark_modbus/exceptions.py @@ -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."""