From 87800a3e8a043786f5d4f6be1394537b408be8b4 Mon Sep 17 00:00:00 2001 From: "regicidal.plutophage" <36969337+regicidalplutophage@users.noreply.github.com> Date: Sun, 6 Oct 2024 19:02:59 +0300 Subject: [PATCH 1/4] digital_inout.py: add exceptions when ipol register is absent Fixes #59 --- adafruit_mcp230xx/digital_inout.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index e3b57e2..6d4e3b3 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -145,16 +145,23 @@ def pull(self, val: Pull) -> None: @property def invert_polarity(self) -> bool: - """The polarity of the pin, either True for an Inverted or - False for an normal. - """ - if _get_bit(self._mcp.ipol, self._pin): - return True - return False + try: + """The polarity of the pin, either True for an Inverted or + False for an normal. + """ + if _get_bit(self._mcp.ipol, self._pin): + return True + return False + except AttributeError: + print("IO expander doesn't support inverting polarity! Continuing...") + return False @invert_polarity.setter def invert_polarity(self, val: bool) -> None: - if val: - self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin) - else: - self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin) + try: + if val: + self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin) + else: + self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin) + except AttributeError: + print("IO expander doesn't support inverting polarity! Continuing...") From 85974d9b0ff846ab6faac9b5ca4d767dbf3661a2 Mon Sep 17 00:00:00 2001 From: "regicidal.plutophage" <36969337+regicidalplutophage@users.noreply.github.com> Date: Sun, 6 Oct 2024 19:09:30 +0300 Subject: [PATCH 2/4] Appease pylint --- adafruit_mcp230xx/digital_inout.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index 6d4e3b3..a39facd 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -145,10 +145,10 @@ def pull(self, val: Pull) -> None: @property def invert_polarity(self) -> bool: + """The polarity of the pin, either True for an Inverted or + False for an normal. + """ try: - """The polarity of the pin, either True for an Inverted or - False for an normal. - """ if _get_bit(self._mcp.ipol, self._pin): return True return False From 824f7ba749386723963c2e435687d1ed21caa51a Mon Sep 17 00:00:00 2001 From: "regicidal.plutophage" <36969337+regicidalplutophage@users.noreply.github.com> Date: Sun, 6 Oct 2024 19:47:38 +0300 Subject: [PATCH 3/4] Update digital_inout.py --- adafruit_mcp230xx/digital_inout.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index a39facd..4583508 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -153,15 +153,17 @@ def invert_polarity(self) -> bool: return True return False except AttributeError: - print("IO expander doesn't support inverting polarity! Continuing...") return False @invert_polarity.setter def invert_polarity(self, val: bool) -> None: - try: - if val: + if val: + try: self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin) - else: + except AttributeError as error: + raise ValueError("Inverted polarity is not supported.") from error + else: + try: self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin) - except AttributeError: - print("IO expander doesn't support inverting polarity! Continuing...") + except AttributeError: + return From 2dbe675036d26922e8b57b9edbf692c56ac1241a Mon Sep 17 00:00:00 2001 From: "regicidal.plutophage" <36969337+regicidalplutophage@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:59:12 +0300 Subject: [PATCH 4/4] Add requested changes --- adafruit_mcp230xx/digital_inout.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index 4583508..17e8dcd 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -148,22 +148,19 @@ def invert_polarity(self) -> bool: """The polarity of the pin, either True for an Inverted or False for an normal. """ - try: - if _get_bit(self._mcp.ipol, self._pin): - return True - return False - except AttributeError: - return False + if hasattr(self._mcp, "ipol") and _get_bit(self._mcp.ipol, self._pin): + return True + return False @invert_polarity.setter def invert_polarity(self, val: bool) -> None: if val: - try: + if hasattr(self._mcp, "ipol"): self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin) - except AttributeError as error: - raise ValueError("Inverted polarity is not supported.") from error + else: + raise ValueError("Inverted polarity is not supported.") else: - try: + if hasattr(self._mcp, "ipol"): self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin) - except AttributeError: + else: return