From 1c6252959cad6da299dd612e5c4dffd1c44eb559 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Mon, 8 Mar 2021 21:58:02 +0100 Subject: [PATCH] Add templates for sent messages in Init and Alias --- controlpi-plugins/util.py | 49 ++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/controlpi-plugins/util.py b/controlpi-plugins/util.py index 66a89ae..eb509dc 100644 --- a/controlpi-plugins/util.py +++ b/controlpi-plugins/util.py @@ -9,12 +9,29 @@ import asyncio from controlpi import BasePlugin, Message, PluginConfiguration +def template_from_message(message: Message) -> Message: + template = {} + for key in message: + value = message[key] + if (isinstance(value, bool) or isinstance(value, int) or + isinstance(value, float) or isinstance(value, str)): + value = {'const': value} + elif (isinstance(value, dict)): + value = {'type': 'object', + 'properties': template_from_message(value)} + template[key] = value + return template + + class Log(BasePlugin): async def _log(self, message: Message) -> None: print(f"{self._name}: {message}") def _process_conf(self, conf: PluginConfiguration) -> None: - self._bus.register(self._name, [], conf['filter'], self._log) + self._bus.register(self._name, + [], + conf['filter'], + self._log) super()._process_conf(conf) @@ -31,8 +48,13 @@ class Init(BasePlugin): self._messages.append(complete_message) receives = [{'target': {'const': self._name}, 'command': {'const': 'execute'}}] - # TODO: Generate send templates from conf['messages'] - self._bus.register(self._name, [{}], receives, self._execute) + sends = [template_from_message(message) + for message in self._messages] + sends.extend(receives) + self._bus.register(self._name, + sends, + receives, + self._execute) super()._process_conf(conf) async def run(self) -> None: @@ -52,7 +74,10 @@ class Wait(BasePlugin): receives = [{'target': {'const': self._name}, 'command': {'const': 'wait'}}] sends = [{'event': {'const': 'finished'}}] - self._bus.register(self._name, sends, receives, self._wait) + self._bus.register(self._name, + sends, + receives, + self._wait) super()._process_conf(conf) @@ -67,7 +92,10 @@ class GenericWait(BasePlugin): 'seconds': {'type': 'number'}, 'id': {'type': 'string'}}] sends = [{'id': {'type': 'string'}}] - self._bus.register(self._name, sends, receives, self._wait) + self._bus.register(self._name, + sends, + receives, + self._wait) super()._process_conf(conf) @@ -84,8 +112,10 @@ class Alias(BasePlugin): def _process_conf(self, conf: PluginConfiguration) -> None: self._from = conf['from'] self._to = conf['to'] - # TODO: Generate send template from conf['to'] - self._bus.register(self._name, [{}], [self._from], self._alias) + self._bus.register(self._name, + [template_from_message(conf['to'])], + [self._from], + self._alias) super()._process_conf(conf) @@ -114,5 +144,8 @@ class State(BasePlugin): {'target': {'const': self._name}, 'command': {'const': 'set state'}, 'new state': {'type': 'boolean'}}] - self._bus.register(self._name, sends, receives, self._receive) + self._bus.register(self._name, + sends, + receives, + self._receive) super()._process_conf(conf) -- 2.34.1