Use new register in state.py.
authorBenjamin Braatz <bb@bbraatz.eu>
Wed, 3 Nov 2021 02:46:24 +0000 (03:46 +0100)
committerBenjamin Braatz <bb@bbraatz.eu>
Wed, 3 Nov 2021 02:46:24 +0000 (03:46 +0100)
controlpi_plugins/state.py

index ac615c5a933ef1124fae7153d7f545f20d4f1fcc..9d8dc7f6382401e3d280895e93b2d653f6ddb0f9 100644 (file)
@@ -137,27 +137,35 @@ class State(BasePlugin):
         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, 'State', sends, receives, self.receive)
-
-    async def receive(self, message: Message) -> None:
-        """Process commands to get/set state."""
-        if message['command'] == 'get state':
-            await self.bus.send(Message(self.name, {'state': self.state}))
-        elif message['command'] == 'set state':
-            if self.state != message['new state']:
-                assert isinstance(message['new state'], bool)
-                self.state = message['new state']
-                await self.bus.send(Message(self.name,
-                                            {'event': 'changed',
-                                             'state': self.state}))
-            else:
-                await self.bus.send(Message(self.name,
-                                            {'state': self.state}))
+        self.bus.register(self.name, 'State', sends,
+                          [([MessageTemplate({'target':
+                                              {'const': self.name},
+                                              'command':
+                                              {'const': 'get state'}})],
+                            self.get_state),
+                           ([MessageTemplate({'target':
+                                              {'const': self.name},
+                                              'command':
+                                              {'const': 'set state'},
+                                              'new state':
+                                              {'type': 'boolean'}})],
+                            self.set_state)])
+
+    async def get_state(self, message: Message) -> None:
+        """Get current state."""
+        await self.bus.send(Message(self.name, {'state': self.state}))
+
+    async def set_state(self, message: Message) -> None:
+        """Set state to new state given in message."""
+        if self.state != message['new state']:
+            assert isinstance(message['new state'], bool)
+            self.state = message['new state']
+            await self.bus.send(Message(self.name,
+                                        {'event': 'changed',
+                                         'state': self.state}))
+        else:
+            await self.bus.send(Message(self.name,
+                                        {'state': self.state}))
 
     async def run(self) -> None:
         """Run no code proactively."""
@@ -228,41 +236,46 @@ class StateAlias(BasePlugin):
                  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'}}),
-                    MessageTemplate({'sender':
-                                     {'const': self.conf['alias for']},
-                                     'event': {'const': 'changed'},
-                                     'state': {'type': 'boolean'}}),
-                    MessageTemplate({'sender':
-                                     {'const': self.conf['alias for']},
-                                     'state': {'type': 'boolean'}})]
-        self.bus.register(self.name, 'StateAlias',
-                          sends, receives, self.receive)
-
-    async def receive(self, message: Message) -> None:
-        """Translate states from and commands to aliased state."""
+        self.bus.register(self.name, 'StateAlias', sends,
+                          [([MessageTemplate({'target':
+                                              {'const': self.name},
+                                              'command':
+                                              {'const': 'get state'}})],
+                            self.get_state),
+                           ([MessageTemplate({'target':
+                                              {'const': self.name},
+                                              'command':
+                                              {'const': 'set state'},
+                                              'new state':
+                                              {'type': 'boolean'}})],
+                            self.set_state),
+                           ([MessageTemplate({'sender':
+                                              {'const':
+                                               self.conf['alias for']},
+                                              'state':
+                                              {'type': 'boolean'}})],
+                            self.translate)])
+
+    async def get_state(self, message: Message) -> None:
+        """Get state from other state."""
+        await self.bus.send(Message(self.name,
+                                    {'target': self.conf['alias for'],
+                                     'command': 'get state'}))
+
+    async def set_state(self, message: Message) -> None:
+        """Set state on other state."""
+        await self.bus.send(Message(self.name,
+                                    {'target': self.conf['alias for'],
+                                     'command': 'set state',
+                                     'new state': message['new state']}))
+
+    async def translate(self, message: Message) -> None:
+        """Translate state messages from other state."""
         alias_message = Message(self.name)
-        if ('target' in message and message['target'] == self.name and
-                'command' in message):
-            alias_message['target'] = self.conf['alias for']
-            if message['command'] == 'get state':
-                alias_message['command'] = 'get state'
-                await self.bus.send(alias_message)
-            elif (message['command'] == 'set state' and
-                    'new state' in message):
-                alias_message['command'] = 'set state'
-                alias_message['new state'] = message['new state']
-                await self.bus.send(alias_message)
-        if (message['sender'] == self.conf['alias for'] and
-                'state' in message):
-            if 'event' in message and message['event'] == 'changed':
-                alias_message['event'] = 'changed'
-            alias_message['state'] = message['state']
-            await self.bus.send(alias_message)
+        if 'event' in message and message['event'] == 'changed':
+            alias_message['event'] = 'changed'
+        alias_message['state'] = message['state']
+        await self.bus.send(alias_message)
 
     async def run(self) -> None:
         """Run no code proactively."""