Skip to content

Critical/warning alert functions placed in wrong class #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
BZab opened this issue Jan 9, 2025 · 1 comment
Open

Critical/warning alert functions placed in wrong class #2

BZab opened this issue Jan 9, 2025 · 1 comment

Comments

@BZab
Copy link

BZab commented Jan 9, 2025

Functions warning_alert_threshold and critical_alert_threshold should be in channel's class.
Here's patch moving them:

--- old.py	2025-01-09 21:33:42.763661755 +0100
+++ new.py	2025-01-09 21:38:14.906018218 +0100
@@ -180,6 +180,56 @@
         self._parent._shunt_resistance[self._channel] = value
 
     @property
+    def critical_alert_threshold(self) -> float:
+        """Critical-Alert threshold in amperes
+
+        Returns:
+            float: The current critical alert threshold in amperes.
+        """
+        if self._channel > 2:
+            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
+
+        reg_addr = CRITICAL_ALERT_LIMIT_CH1 + 2 * self._channel
+        result = self._parent._read_register(reg_addr, 2)
+        threshold = int.from_bytes(result, "big")
+        return (threshold >> 3) * 40e-6 / self.shunt_resistance
+
+    @critical_alert_threshold.setter
+    def critical_alert_threshold(self, current: float) -> None:
+        if self._channel > 2:
+            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
+
+        threshold = int(current * self.shunt_resistance / 40e-6 * 8)
+        reg_addr = CRITICAL_ALERT_LIMIT_CH1 + 2 * self._channel
+        threshold_bytes = threshold.to_bytes(2, "big")
+        self._parent._write_register(reg_addr, threshold_bytes)
+
+    @property
+    def warning_alert_threshold(self) -> float:
+        """Warning-Alert threshold in amperes
+
+        Returns:
+            float: The current warning alert threshold in amperes.
+        """
+        if self._channel > 2:
+            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
+
+        reg_addr = WARNING_ALERT_LIMIT_CH1 + self._channel
+        result = self._parent._read_register(reg_addr, 2)
+        threshold = int.from_bytes(result, "big")
+        return threshold / (self.shunt_resistance * 8)
+
+    @warning_alert_threshold.setter
+    def warning_alert_threshold(self, current: float) -> None:
+        if self._channel > 2:
+            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
+
+        threshold = int(current * self.shunt_resistance * 8)
+        reg_addr = WARNING_ALERT_LIMIT_CH1 + self._channel
+        threshold_bytes = threshold.to_bytes(2, "big")
+        self._parent._write_register(reg_addr, threshold_bytes)
+
+    @property
     def current_amps(self) -> float:
         """Returns the current in amperes.
 
@@ -363,56 +413,6 @@
         self._write_register(CONFIGURATION, config)
 
     @property
-    def critical_alert_threshold(self) -> float:
-        """Critical-Alert threshold in amperes
-
-        Returns:
-            float: The current critical alert threshold in amperes.
-        """
-        if self._channel > 2:
-            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
-
-        reg_addr = CRITICAL_ALERT_LIMIT_CH1 + 2 * self._channel
-        result = self._parent._read_register(reg_addr, 2)
-        threshold = int.from_bytes(result, "big")
-        return (threshold >> 3) * 40e-6 / self.shunt_resistance
-
-    @critical_alert_threshold.setter
-    def critical_alert_threshold(self, current: float) -> None:
-        if self._channel > 2:
-            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
-
-        threshold = int(current * self.shunt_resistance / 40e-6 * 8)
-        reg_addr = CRITICAL_ALERT_LIMIT_CH1 + 2 * self._channel
-        threshold_bytes = threshold.to_bytes(2, "big")
-        self._parent._write_register(reg_addr, threshold_bytes)
-
-    @property
-    def warning_alert_threshold(self) -> float:
-        """Warning-Alert threshold in amperes
-
-        Returns:
-            float: The current warning alert threshold in amperes.
-        """
-        if self._channel > 2:
-            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
-
-        reg_addr = WARNING_ALERT_LIMIT_CH1 + self._channel
-        result = self._parent._read_register(reg_addr, 2)
-        threshold = int.from_bytes(result, "big")
-        return threshold / (self.shunt_resistance * 8)
-
-    @warning_alert_threshold.setter
-    def warning_alert_threshold(self, current: float) -> None:
-        if self._channel > 2:
-            raise ValueError("Invalid channel number. Must be 0, 1, or 2.")
-
-        threshold = int(current * self.shunt_resistance * 8)
-        reg_addr = WARNING_ALERT_LIMIT_CH1 + self._channel
-        threshold_bytes = threshold.to_bytes(2, "big")
-        self._parent._write_register(reg_addr, threshold_bytes)
-
-    @property
     def flags(self) -> int:
         """Flag indicators from the Mask/Enable register.
 
@BZab
Copy link
Author

BZab commented Jan 10, 2025

I have added the pull request #3 with the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant