Register callbacks for device updates

Hey everybody,

I´m struggling with the registration of callbacks for updates a device has pending.

Look at the following case:
Device sends events over Serial
My library recieves it and processes them
I want to process the updated values in HA (if a callback is registered), meaning my device triggers an action or update in HA

How can i do this? There is virtually zero documentation for local_push and the other integrations are no help for me either. Can someone maybe give me a hint? :sleepy:

Best

1 Like

I’m following this link, hoping that someone will answer.
I’m a beginner with python, and I also find that documentation is missing.
I believe that the “demo” integration should be commented and integrated with a real case.
I’m also trying to develop a integration for I/O modules connectable by serial interface (RS485), but it’s really difficult with HA even if I have already written it for Domoticz.
Thanks for any answer…
Paolo

1 Like

Has nobody any idea? :frowning:

This is a common pattern. Here how I did it.

You have your Manager class (that’s the thing that is receiving data) and you have the entities defined by you integration (sensors). During initialization and creation of your sensor entities register then via a callback with the manager (observer pattern). Later on when the manager gets a change it call the appropriate callbacks, and then the entity’s callback invokes schedule_ha_update(), later on ha will read the state of the entity,

I agree a better example should be constructed.

Here is a working example of a sensor.

1 Like

Hi PeteRage,
Thanks for your clear example. But, what are the events when HA will call that callback?
In my case, it will be nice to have a callback when something is changed by the user through the Configuration → Customization menu where it’s possible to change sensor class and add attributes to the sensor.
Thanks. Paolo

Configuration is a separate topic, you may want to ask that in a separate thread.

In the manager class it is reading off a cloud message bus, when it receives data that will cause the sensor state to update it invokes the callback. That reception is running in the background in asyncio.

In your example on RS 485, you have a decision to make.

Option 1: Have home assistant drive the polling. In this mode HA will call the entities update method periodically and you then execute the Rs 485 call to read the data.

Option 2: You have a asyncio manager running receiving Data from the device. It in turn executes callbacks on the affected entities and then the entity notifies HA that it has new data. HA will then call into get the state of your entity. Which you already have stores in a variable.

My integration uses Option 2, because that approach worked best with the way the data arrives.