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 separateconnect()
oropen()
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.