From 6d8f4add1a8e67d9e184a6cc31b8d519544061e7 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Wed, 16 Jun 2021 05:05:50 +0200 Subject: [PATCH] Add HackPin. --- controlpi_plugins/gpio.py | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/controlpi_plugins/gpio.py b/controlpi_plugins/gpio.py index 1c62c21..74e1b0f 100644 --- a/controlpi_plugins/gpio.py +++ b/controlpi_plugins/gpio.py @@ -58,6 +58,51 @@ class OutputPin(BasePlugin): pass +class HackPin(BasePlugin): + """… plugin. + + Do this and that. + """ + + CONF_SCHEMA = {'properties': {'pin': {'type': 'integer', + 'minimum': 0, 'maximum': 31}}, + 'required': ['pin']} + + async def _receive(self, message: Message) -> None: + if message['command'] == 'get state': + await self.bus.send(Message(self.name, {'state': self._state})) + elif message['command'] == 'set state': + pi = _get_pigpio_pi() + assert isinstance(message['new state'], bool) + pi.set_mode(self.conf['pin'], int(message['new state'])) + new_state = bool(pi.get_mode(self.conf['pin'])) + if new_state != self._state: + self._state: bool = new_state + await self.bus.send(Message(self.name, {'event': 'changed', + 'state': new_state})) + else: + await self.bus.send(Message(self.name, {'state': new_state})) + + def process_conf(self) -> None: + """Configure pin and register bus client.""" + pi = _get_pigpio_pi() + self._state = bool(pi.get_mode(self.conf['pin'])) + sends = [MessageTemplate({'event': {'const': 'changed'}, + 'state': {'type': 'boolean'}}), + MessageTemplate({'state': {'type': 'boolean'}})] + receives = [MessageTemplate({'target': {'const': self.name}, + 'command': {'const': 'get state'}}), + MessageTemplate({'target': {'const': self.name}, + 'command': {'const': 'set state'}, + 'new state': {'type': 'boolean'}})] + self.bus.register(self.name, 'OutputPin', + sends, receives, self._receive) + + async def run(self) -> None: + """Run no code proactively.""" + pass + + class InputPin(BasePlugin): """… plugin. -- 2.34.1