Working example of HA integration

It can be hard to find integrations supporting serial from the code as the connection logic is typically handled by a separate library (separation is required for core integrations)

There seem to be more integrations using serial when searching for “serial” on https://www.home-assistant.io/ However most of those results are from integrations that have yaml setup where you specify the serial port. Since these are still using yaml they are probably older integrations following old best practices, not sure if I would recommend these as reference.

I am not sure how relevant it is that your integration will be using serial.

Independant of connection/protocol most API packages that I have worked with are like the api.py in the msp_integration_101_intermediate example.

  • There is an init() with sometimes a separate connect() or open() method if these are expensive operations, but often also combined in the init. This is probably where you would setup your eventloop.
  • Next to that there is a usually a way to get data like with get_mock_data() in the example which retrieves all data, or for individual attributes. All vs per attribute usually depends on the API you are working with.
  • And last there is a way to set/write things to the API like set_moc_data() in the example.

An instance of this API is created during integration setup and used by the entities later to get or set data. In the example a coordinator is used to poll the data instead of doing it in the update() method of the entity itself. Using a coordinator makes it easier to have one place to update your data and make it accessible to multiple entities. I would say using a coordinator is the standard nowadays.

As for a fully commented working example I think the examples provided in this thread is about the best you can get right now. I think the code generated by the scaffold also contains some useful comments but less than these examples.

If you have specific questions I would suggest to push your code to Github so people can look at the whole (even when it is not done yet). You could create a thread here or ask a question in #dev_core on Discord.

Thank you both. I will probably do a bit of both. @msp1974 I’ll send you my class via PM and I will publish it on github so I can post a new thread for assistance.

I suppose that it isn’t serial per se that I feel the issue is but using asyncio and specifically needing to run tasks. As I indicated above the docs seem to not want you doing any IO outside of the update function but that doesn’t work when you need a tasks that are reading and responding to the device. I guess you would have the same problem if it was say connecting to a TCP socket on a remote device and spawning tasks to read/write to the device. I guess I don’t understand where you would run your own tasks in this framework. Perhaps I’m being obtuse, hopefully MSP1974 can help me out a bit.

Thank you again.

Hi,

I been using the temples you helped me with and they have been working great.
I’m getting 2 errors in the logs that suggest I need to update "async_forward_entry_setup " as it is be deprecated.

Errors in the logs:

  • Detected that custom integration ‘example’ calls async_forward_entry_setup for integration, which is deprecated and will stop working in Home Assistant 2025.6, await async_forward_entry_setups instead at custom_components/example/init.py, line 73: hass.async_create_task(, please report it to the author of the ‘example’ custom integration

  • Detected code that calls async_forward_entry_setup for integration example with title: example Integration - device and entry_id: 01J5VW4BDSMCWYTXQMXKEYA840, during setup without awaiting async_forward_entry_setup, which can cause the setup lock to be released before the setup is done. This will stop working in Home Assistant 2025.1. Please report this issue

Any advice on how to update this, I tired to replace it with “await async_forward_entry_setups” but get the following error

  • Error setting up entry Integration
    Traceback (most recent call last):
    File “/usr/src/homeassistant/homeassistant/config_entries.py”, line 604, in async_setup
    result = await component.async_setup_entry(hass, self)

Regards

Hi James,

You need to change

for platform in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(config_entry, platform)
        )

In init.py
To

await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

I’ll update the templates when i get time.

Hi Mark - your examples are great, fantastic work!

I’ve dabbled a bit with Config Flow and have a reasonable understanding of the basics, but one thing I’m looking into now is dynamic fields or callback-based fields, for example typing an address into one text field and running a periodic address lookup API call and populating a second text field.

I’ve done some research and can’t find anything relating to callbacks or similar, the best thing I can see is to have a two-step process where the code that runs the second step invokes the API call, processes the return and prepopulates the second step fields.

Do you have any thoughts or suggestions at all?

Cheers,
Andy

I dont think there is any way to have a callback, so the way you are planning (by having a 2 step process) is probably the way to go.