Add relay outputs.
authorBenjamin Braatz <bb@bbraatz.eu>
Tue, 21 Feb 2023 01:06:11 +0000 (02:06 +0100)
committerBenjamin Braatz <bb@bbraatz.eu>
Tue, 21 Feb 2023 01:07:37 +0000 (02:07 +0100)
controlpi_plugins/unipi.py

index 6afc1de982a26c148aa04c9533603dfb1682831e..e64900691d830579a34228c9fba0dfbe5e0418de 100644 (file)
@@ -9,13 +9,14 @@ from controlpi import BasePlugin, Message, MessageTemplate
 
 class UniPi(BasePlugin):
     CONF_SCHEMA = {}
+    SYS_DIR = '/sys/bus/platform/devices/unipi_plc'
 
     def process_conf(self) -> None:
         """Search all input and output pins and initialise."""
         self._paths: Dict[str, str] = {}
         self._states: Dict[str, bool] = {}
         self._counters: Dict[str, int] = {}
-        for path in glob.glob('/sys/bus/platform/devices/unipi_plc/io_group?/di_?_??'):
+        for path in glob.glob(self.SYS_DIR + '/io_group?/di_?_??'):
             client = path[46:53]
             self._paths[client] = path
             with open(f"{path}/{client[0:2]}_value", 'r') as sysfs_file:
@@ -53,9 +54,11 @@ class UniPi(BasePlugin):
                                ([MessageTemplate({'target':
                                                   {'const': client},
                                                   'command':
-                                                  {'const': 'reset counter'}})],
+                                                  {'const':
+                                                   'reset counter'}})],
                                 self._reset_counter)])
-        for path in glob.glob('/sys/bus/platform/devices/unipi_plc/io_group?/do_?_??'):
+        for path in (glob.glob(self.SYS_DIR + '/io_group?/do_?_??') +
+                     glob.glob(self.SYS_DIR + '/io_group?/ro_?_??')):
             client = path[46:53]
             self._paths[client] = path
             with open(f"{path}/{client[0:2]}_value", 'r') as sysfs_file:
@@ -91,7 +94,8 @@ class UniPi(BasePlugin):
         # Get state from sysfs file:
         old_state = self._states[client]
         new_state = False
-        async with aiofiles.open(f"{path}/{client[0:2]}_value", 'r') as sysfs_file:
+        async with aiofiles.open(f"{path}/{client[0:2]}_value",
+                                 'r') as sysfs_file:
             file_state = await sysfs_file.read()
             if file_state[0] == '1':
                 new_state = True
@@ -144,7 +148,8 @@ class UniPi(BasePlugin):
         client = message['target']
         path = self._paths[client]
         # Write new state to sysfs file:
-        async with aiofiles.open(f"{path}/{client[0:2]}_value", 'w') as sysfs_file:
+        async with aiofiles.open(f"{path}/{client[0:2]}_value",
+                                 'w') as sysfs_file:
             if message['new state']:
                 await sysfs_file.write('1')
             else:
@@ -152,7 +157,8 @@ class UniPi(BasePlugin):
         # Get state from sysfs file:
         old_state = self._states[client]
         new_state = False
-        async with aiofiles.open(f"{path}/{client[0:2]}_value", 'r') as sysfs_file:
+        async with aiofiles.open(f"{path}/{client[0:2]}_value",
+                                 'r') as sysfs_file:
             file_state = await sysfs_file.read()
             if file_state[0] == '1':
                 new_state = True
@@ -172,7 +178,8 @@ class UniPi(BasePlugin):
                 # Get state from sysfs file:
                 old_state = self._states[client]
                 new_state = False
-                async with aiofiles.open(f"{path}/{client[0:2]}_value", 'r') as sysfs_file:
+                async with aiofiles.open(f"{path}/{client[0:2]}_value",
+                                         'r') as sysfs_file:
                     file_state = await sysfs_file.read()
                     if file_state[0] == '1':
                         new_state = True
@@ -185,10 +192,12 @@ class UniPi(BasePlugin):
                 # Get counter from sysfs file:
                 old_counter = self._counters[client]
                 new_counter = 0
-                async with aiofiles.open(f"{path}/counter", 'r') as sysfs_file:
+                async with aiofiles.open(f"{path}/counter",
+                                         'r') as sysfs_file:
                     file_counter = await sysfs_file.read()
                     new_counter = int(file_counter)
                 self._counters[client] = new_counter
                 if old_counter != new_counter:
                     await self.bus.send(Message(client, {'event': 'changed',
-                                                         'counter': new_counter}))
+                                                         'counter':
+                                                         new_counter}))