From b6e188f9e90b99c4f13f8d0c7e985991d16a7d64 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Tue, 29 Mar 2022 06:25:15 +0200 Subject: [PATCH] Catch pigpio.error. --- controlpi_plugins/pcf8574.py | 38 +++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/controlpi_plugins/pcf8574.py b/controlpi_plugins/pcf8574.py index d981440..257d92a 100644 --- a/controlpi_plugins/pcf8574.py +++ b/controlpi_plugins/pcf8574.py @@ -22,8 +22,16 @@ class OutputCard(BasePlugin): def process_conf(self) -> None: """Open I2C device, read initial state and register bus clients.""" pi = _get_pigpio_pi() - self._handle = pi.i2c_open(1, self.conf['address']) - byte = pi.i2c_read_byte(self._handle) + try: + self._handle = pi.i2c_open(1, self.conf['address']) + except pigpio.error as e: + print(e) + return + try: + byte = pi.i2c_read_byte(self._handle) + except pigpio.error as e: + print(e) + byte = 0 self._pins2states: Dict[int, bool] = {} self._pins2clients: Dict[int, List[str]] = {} for pin in range(0, 8): @@ -75,8 +83,12 @@ class OutputCard(BasePlugin): byte |= int(not self._pins2states[pin]) << pin # Write and immediately read back status byte: pi = _get_pigpio_pi() - pi.i2c_write_byte(self._handle, byte) - byte = pi.i2c_read_byte(self._handle) + try: + pi.i2c_write_byte(self._handle, byte) + byte = pi.i2c_read_byte(self._handle) + except pigpio.error as e: + print(e) + return # Send changed events for all changed clients: for pin in range(0, 8): new_state = not bool(byte & (1 << pin)) @@ -112,8 +124,16 @@ class InputCard(BasePlugin): def process_conf(self) -> None: """Open I2C device, read initial state and register bus clients.""" pi = _get_pigpio_pi() - self._handle = pi.i2c_open(1, self.conf['address']) - byte = pi.i2c_read_byte(self._handle) + try: + self._handle = pi.i2c_open(1, self.conf['address']) + except pigpio.error as e: + print(e) + return + try: + byte = pi.i2c_read_byte(self._handle) + except pigpio.error as e: + print(e) + byte = 0 self._pins2states: Dict[int, bool] = {} self._pins2clients: Dict[int, List[str]] = {} for pin in range(0, 8): @@ -154,7 +174,11 @@ class InputCard(BasePlugin): def _read(self) -> None: # Read status byte: pi = _get_pigpio_pi() - byte = pi.i2c_read_byte(self._handle) + try: + byte = pi.i2c_read_byte(self._handle) + except pigpio.error as e: + print(e) + return # Send changed events for all changed clients: for pin in range(0, 8): new_state = not bool(byte & (1 << pin)) -- 2.34.1