Using the throttle function in HA

Hi,
I have been working on an integration that talks to an Acmeda Pulse Blind hub over serial rs232.
It’s all working well for individual blind control.
But when the Update function gets called, they all get called at the same time, which over a slower serial connection can lead to updates being called without a response.
The other difficulty is that if I use a scene in HomeKit, that say opens/closes all blinds at the same time, then only some of them work, because again they get called at the exact same time.

I created it as a cover component, with a controller object in an external library. That controller library sends the serial commands and listens for responses to update each cover.
The listener runs Async so it can keep the socket open.

So I stumbled upon the @Throttle decorator that a lot of projects are using to reduce the number of API calls, but putting this in the cover object doesn’t seem to limit it at all.
When I put some logger debugging in I can see each cover update gets called in a seperate thread, so I wonder if the throttling only works per thread. Which doesn’t help in my case.

Any ideas on how I can limit the number of calls to the controller object for all cover objects?

Even though I don’t know much about that specific case, to me it sounds more like you should actually implement some sort of queue in the controller library. So whenever the update happens, it just pushes the required command to some FIFO-queue in the library, the library communicates over serial, and then returns the result when it’s done, maybe via a callback or similar. The details are up to you. My point is: the entities shouldn’t be able to directly initiate dataflow over the serial connection. The more entities you have that need updating, the more likely it would be for them to collide while doing so if there’s nothing inbetween to regulate this.

Good idea, I was hesitant doing a queue and keeping it thread safe.
But it might be my only option and actually would be better at avoiding collisions.
Ill need to add a timeout too, as I have noticed not all messsages get a response.

Queue and a threaded manager worked perfectly, thanks for the help.
It’s just a thread that picks the first off a queue on a timer. Now there’s no collisions, I can work on making it more efficient and responsive.

1 Like