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."""
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."""