From 89c2fb83e9d7aa137399456048fff3a0e582293f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Mon, 16 Sep 2024 14:03:50 +0000 Subject: [PATCH] Added channel_to_valve to lvmopstools.devices.ion --- CHANGELOG.md | 7 +++++++ docs/sphinx/api.rst | 1 + src/lvmopstools/devices/thermistors.py | 28 +++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67d38af..b10ede4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Next version + +### 🚀 New + +* Added `channel_to_valve` mapping function to `lvmopstools.devices.ion`. + + ## 0.3.7 - September 15, 2024 ### ✨ Improved diff --git a/docs/sphinx/api.rst b/docs/sphinx/api.rst index e33a7df..347fca7 100644 --- a/docs/sphinx/api.rst +++ b/docs/sphinx/api.rst @@ -42,6 +42,7 @@ Thermistors ^^^^^^^^^^^ .. autofunction:: lvmopstools.devices.thermistors.read_thermistors +.. autofunction:: lvmopstools.devices.thermistors.channel_to_valve NPS ^^^ diff --git a/src/lvmopstools/devices/thermistors.py b/src/lvmopstools/devices/thermistors.py index b8e2bee..2510202 100644 --- a/src/lvmopstools/devices/thermistors.py +++ b/src/lvmopstools/devices/thermistors.py @@ -11,13 +11,39 @@ import asyncio import re +from typing import Literal, overload + import asyncudp from lvmopstools import config from lvmopstools.retrier import Retrier -__all__ = ["read_thermistors"] +__all__ = ["read_thermistors", "channel_to_valve"] + + +@overload +def channel_to_valve(reverse: Literal[False] = False) -> dict[int, str]: ... + + +@overload +def channel_to_valve(reverse: Literal[True] = True) -> dict[str, int]: ... + + +def channel_to_valve(reverse: bool = False) -> dict[int, str] | dict[str, int]: + """Returns a mapping of thermistor channels to valve names. + + With ``reverse`` returns the inverse mapping, valve to device channel. + + """ + + valve_to_channel = config["devices.thermistors.channels"] + + if reverse: + return valve_to_channel + + channel_to_valve = {valve_to_channel[valve]: valve for valve in valve_to_channel} + return channel_to_valve @Retrier(max_attempts=3, delay=1)