Event handler for UART data incoming?

I’m writing a driver for a new sensor type in esphome. The sensor uses uart, but it’s a little slow, so when you send a query to it you cannot immediately read the response - it takes too long and esphome kills the read with a timeout error.

I can solve this by doing the read a little later, e.g. this->set_timeout("name", 50, [this] { ... });, but that has two downsides:

  • We may be waiting too long
  • We may be waiting too short

I’m used to working with event loops and registering handlers, but I cannot find a way to register a handler to be invoked on incoming data. Am I missing something here?