From: Benjamin Braatz Date: Wed, 3 Nov 2021 02:46:24 +0000 (+0100) Subject: Use new register in state.py. X-Git-Tag: v0.3.0~6 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=d1921fdcdcb4e35a72b0938d3f0c6bf6c5203980;p=graphit%2Fcontrolpi.git Use new register in state.py. --- diff --git a/controlpi_plugins/state.py b/controlpi_plugins/state.py index ac615c5..9d8dc7f 100644 --- a/controlpi_plugins/state.py +++ b/controlpi_plugins/state.py @@ -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."""