Repository navigation
Add per config entry batch service handlers #1308
Replies: 11 comments 20 replies
|
I think that this is a great idea. Now, putting this on an entity model seems weird though, as an entity represents a single entity, and batching should happen by config entry. Config entries are the logical grouping for batching, as there can be different authentication or connections involved per config entry. So the idea is good, the location feels weird. Although maybe a class method that also takes config entry? It is nice that it is connected to entity. |
|
I like the idea of improving performance via batching. The original post doesn’t list all integrations that could take advantage of this, but I would like to add another concrete use case: WLED. In Home Assistant, WLED exposes each segment as a separate light entity. Behind the scenes all of these entities map to a single device and a single API endpoint ( A batching mechanism similar to the one proposed here would allow the WLED integration to build a single combined payload, push it to the device, and update multiple segment entities atomically. This matches the same architectural need demonstrated by the Z-Wave example, just at a “multiple entities under one config entry” level rather than “multiple physical devices under one config entry”. This aligns with the general direction discussed here: batching improves performance, reduces network overhead, and ensures consistent state application across related entities when the underlying protocol supports it. WLED is a good example of another integration that could directly benefit from this model. Related discussion: https://github.com/orgs/home-assistant/discussions/1719 |
|
After some more research, it seems like a lot of integrations would benefit from this. Namely:
and others. I had only thought about light/switches. But a similar mechanism might be useful for media players that support synchronization. |
|
This should not be implemented as a class method, as entities can belong to different classes, but as a function on an integration/platform level. |
|
And my big problem with all of this is for things like lights where the service calls do a whole bunch of parameter validation. Am I allowed to refactor the validation logic out into a module level function? |
|
Happy New Year, all! Ok, I think I finally have it with this draft PR and this working Zwave switch multicast PR I'm ready to release these as regular PRs unless someone has issues/suggestions. Basically, the integration uses def async_register_platform_batched_service(
service_name: str,
handler: BatchedServiceCallback,
) -> Noneor EntityPlatform.async_register_batched_service(
self,
service_name: str,
handler: BatchedServiceCallback,
) -> Nonein the platform async_setup. BatchedServiceCallback is defined as: type BatchedServiceCallback = Callable[
[ConfigEntry, list[Entity], ServiceCall],
Coroutine[None, None, EntityServiceResponse | None],
]and entity_service_call has been modified to call the callbacks. |
|
While implementing batched handling of light.turn_on / light.turn_off for integrations that can operate on multiple entities at once, a few architectural questions came up: Validation source: Should batched handlers rely on entity-declared capabilities (supported_features, supported_color_modes), or are they expected to reuse the same service-level validation logic used by the default light service handler? And, if they're supposed to use the same service level validation logic by the default light service handler, can LightEntity be refactored to expose that logic? Mixed capabilities: When a service call targets entities with differing capabilities, is partial batching acceptable (batch what’s supported, handle the rest individually)? Fallback mechanics: If partial batching is acceptable, should a batched handler remove the entities it handled from the entity list and allow entity_service_call to perform a second pass using the default per-entity handler for the remainder? Unsupported parameters: Should batched handlers strictly reject unsupported parameters, or is it acceptable to pass them through based on entity capabilities? Execution details: Is it preferred to avoid asyncio.gather when only a single batch/coroutine is involved? |
|
Hi @rlippmann, |
|
@arturpragacz after reviewing the batched service handling work, it looks like this implements an improved version of the core idea from this discussion. Thank you for pushing this forward. Given that, I think this architecture discussion can be marked as answered/closed. If useful, I can open follow-up PRs for batched handling in Z-Wave lights and switches. While reviewing recent changes, I also noticed the validation helper changes were added. I think a shared light grouping helper for batched |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Updated (summary)
Note: the initial classmethod-based idea below is superseded; current direction is integration/platform-registered per-config-entry batch handlers.
light.turn_on/offetc.). When multiple targets are provided, HA automatically batches eligible entities per config entry, reducing latency, network traffic, and API pressure; unsupported cases automatically fall back to per-entity handling.EntityServiceResponsecovers response shape; remaining question is consistent per-entity reporting semantics across responses/logs/events.Hi all.
I was looking at performance improvements for light/switch entities (although this could be extended later), and was thinking that adding batching calls could be beneficial. Currently, HA sequentially calls entities to turn on/off, but it would be more efficient if an integration supports batch calls to use them.
I was thinking of adding class methods to ToggleEntity like this:
(similar for turn off)
Then modifying the service handlers that do the turn on in switch/light from just calling async_turn_on to calling batch_turn_on based upon the subclass of the entity.
This way, an integration can add batching calls if the underlying protocol/api supports it.
For example:
Thoughts?
All reactions