Using serial_proxy from an integration when Home Assistant is starting up

Hi, I'm hoping that someone can help me with this. I have successfully created a serial_proxy in an ESP32 device, I can connect to it from my custom integration and pass serial data to and from the device, looking OK so far.

The problem I have is when Home Assistant is starting up and the ESPHome integration may or may not be available. What can I do? Do I:

  1. Put in a big delay in my integration .... I know I know, don't do this option :wink:
  2. Put a callback handler in so I know that Home Assistant has completed startup (and therefore assume that ESPHome with have the ESP device)
  3. Is there are way I can check/wait for the ESPHome integration to be loaded?

If I try to create the serial connection before the ESPHome integration loads, it looks like it works but it hasn't and then starts to do weird stuff.

Any guidance is appreciated, thanks

So after a few hours experimenting I think that this might be the best approach

Put this in async_setup_entry

        try:
            asu = None
            serial_url = entry.data.get(CONF_PATH, "")
            parsed: ParseResult = urlparse(serial_url)
            if len(parsed.scheme) > 0:
                asu = serialx.async_serial_for_url(
                    url=serial_url,
                    baudrate=9600,
                )
                await asu.open()
                _ = asu.is_open
                await asu.close()

        except (OSError, serialx.SerialException) as err:
            raise ConfigEntryNotReady("ESPHome serial proxy not ready, cannot continue") from err
        finally:
            if asu is not None and asu.is_open:

I tried getting the esphome integration runtime but that just seemed to get way too complicated, and then I ended up doing the above anyway. Test to see if it opens, and then close it straight away. Raise a not ready exception so Home Assistant tries my integration again "later".

Does this seem like the correct thing to do?