From: Benjamin Braatz Date: Tue, 2 Mar 2021 16:48:13 +0000 (+0100) Subject: Add State plugin with example in conf.json X-Git-Tag: v0.3.0~85 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=3e7151b7ad86f312d91ae9e78d25bf1198828cb9;p=graphit%2Fcontrolpi.git Add State plugin with example in conf.json --- diff --git a/conf.json b/conf.json index 077a332..6c25b31 100644 --- a/conf.json +++ b/conf.json @@ -1,27 +1,55 @@ { - "TriggerWait2": { + "TriggerWaitCheck": { "plugin": "Alias", - "from": { "sender": "Wait1", "event": "finished" }, - "to": { "target": "Wait2", "command": "wait" } + "from": { "sender": "WaitCheck", "event": "finished" }, + "to": { "target": "WaitCheck", "command": "wait" } }, - "TriggerWait1": { + "TriggerStateCheck": { "plugin": "Alias", - "from": { "sender": "Wait2", "event": "finished" }, - "to": { "target": "Wait1", "command": "wait" } + "from": { "sender": "WaitCheck", "event": "finished" }, + "to": { "target": "State", "command": "get state" } }, - "Wait1": { + "TriggerWaitOnOff": { + "plugin": "Alias", + "from": { "sender": "WaitOn", "event": "finished" }, + "to": { "target": "WaitOff", "command": "wait" } + }, + "TriggerStateOnOff": { + "plugin": "Alias", + "from": { "sender": "WaitOn", "event": "finished" }, + "to": { "target": "State", "command": "set state", "state": false } + }, + "TriggerWaitOffOn": { + "plugin": "Alias", + "from": { "sender": "WaitOff", "event": "finished" }, + "to": { "target": "WaitOn", "command": "wait" } + }, + "TriggerStateOffOn": { + "plugin": "Alias", + "from": { "sender": "WaitOff", "event": "finished" }, + "to": { "target": "State", "command": "set state", "state": true } + }, + "WaitCheck": { "plugin": "Wait", "seconds": 1.0 }, - "Wait2": { + "WaitOn": { + "plugin": "Wait", + "seconds": 2.0 + }, + "WaitOff": { "plugin": "Wait", "seconds": 2.0 }, + "State": { + "plugin": "State" + }, "Test Procedure": { "plugin": "Init", "messages": [ { "event": "started" }, - { "target": "Wait1", "command": "wait" }, + { "target": "WaitOff", "command": "wait" }, + { "target": "WaitCheck", "command": "wait" }, { "event": "stopped" } ] }, diff --git a/controlpi/plugins/util.py b/controlpi/plugins/util.py index ad84814..a3687a8 100644 --- a/controlpi/plugins/util.py +++ b/controlpi/plugins/util.py @@ -64,7 +64,32 @@ class Alias(BasePlugin): alias_message[key] = message[key] await self._bus.send(alias_message) - def _process_conf(self, conf): + def _process_conf(self, conf: PluginConfiguration) -> None: self._from = conf['from'] self._to = conf['to'] self._bus.register(self._name, [self._to], [self._from], self._alias) + + +class State(BasePlugin): + async def _receive(self, message: Message) -> None: + if 'command' in message: + if message['command'] == 'get state': + await self._bus.send({'sender': self._name, + 'state': self._state, + 'changed': False}) + elif message['command'] == 'set state': + if 'state' in message and self._state != message['state']: + self._state: bool = message['state'] + await self._bus.send({'sender': self._name, + 'state': self._state, + 'changed': True}) + + def _process_conf(self, conf: PluginConfiguration) -> None: + self._state = False + sends: list[Message] = [{'sender': self._name, 'state': bool}] + receives: list[Message] = [{'target': self._name, + 'command': 'get state'}, + {'target': self._name, + 'command': 'set state', + 'state': bool}] + self._bus.register(self._name, sends, receives, self._receive)