async def _handle_incoming(self, incoming: Incoming, ctx: Optional[Context] = None) -> None: """Kicks of the flow for one message. This is the callback for the listener.""" # Context might be set in case of an error or an unknown message that needs to be # handled if not ctx: ctx = Context(incoming=incoming) handled = False for flow in self.flows: try: if await flow.processor.can_process(incoming.clone()): handled = True current = incoming.clone() current = await flow.processor(ctx.clone(), current) current = await self._call_formatters(flow.formatters, ctx, current) await self._call_actions(flow.actions, ctx, current) except: # pylint: disable=bare-except self.logger.exception("Error caught while processing the payload:\n%s", str(incoming)) handled = True if not isinstance(incoming, ErrorIncoming): await self._handle_error(ctx) else: self.logger.warning("While handling the error a new error was caught. Aborting... ") if not handled: if not isinstance(incoming, UnknownCommandIncoming): await self._handle_unhandled(ctx) else: self.logger.warning( "Incoming '%s' cannot be handled and no " "unknown command processor is configured.", str(ctx.incoming) )
async def test_can_process(message): dut = UnknownCommand() assert not await dut.can_process(Incoming()) assert not await dut.can_process(message) assert await dut.can_process(UnknownCommandIncoming(command="foo")) assert not await dut.can_process( ErrorIncoming(error_message="blub", trace="bla"))
async def test_can_process(dut, message): assert not await dut.can_process(Incoming()) assert not await dut.can_process(message) assert not await dut.can_process(UnknownCommandIncoming(command="foo")) assert not await dut.can_process( ErrorIncoming(error_message="blub", trace="bla")) message.text = " traffic origin to dest " assert await dut.can_process(message)
async def test_can_process(message): dut = Pricing() assert not await dut.can_process(Incoming()) assert not await dut.can_process(message) assert not await dut.can_process(UnknownCommandIncoming(command="foo")) assert not await dut.can_process( ErrorIncoming(error_message="blub", trace="bla")) message.text = " lego pricing 12345 " assert await dut.can_process(message)
async def test_can_process(message, dut): assert not await dut.can_process(Incoming()) assert not await dut.can_process(message) message.text = " help " assert not await dut.can_process(message) assert not await dut.can_process(UnknownCommandIncoming(command="foo")) assert not await dut.can_process(ErrorIncoming(error_message="blub", trace="bla")) message.text = " switch on domain.switch_entity " assert await dut.can_process(message) message.text = " switch off domain.switch_entity " assert await dut.can_process(message)