From fec50ad5265064545dad62dddbb434d422b0da92 Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Sat, 5 Aug 2023 23:19:18 -0500 Subject: [PATCH] Adapt lights simultaneously instead of one by one (#529) * Update switch.py * Update test_switch.py * Revert "Update test_switch.py" This reverts commit 87ea5243b97bfa064c282382b29e09ed61917892. * remove unnecessary create_task * remove unneeded len() * Revert "remove unnecessary create_task" This reverts commit 2c5da6d73954e0ff3808b61092adbe55cdec1bb4. * use `hass.async_create_task` --------- Co-authored-by: Bas Nijholt Co-authored-by: Bas Nijholt --- custom_components/adaptive_lighting/switch.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 49d886fa..b0aa7f8c 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1399,7 +1399,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): adapt_color = self.adapt_color_switch.is_on assert isinstance(adapt_brightness, bool) assert isinstance(adapt_color, bool) - + tasks = [] for light in filtered_lights: manually_controlled = ( self._take_over_control @@ -1446,7 +1446,13 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): transition, context.id, ) - await self._adapt_light(light, context, transition, force=force) + coro = self._adapt_light(light, context, transition, force=force) + task = self.hass.async_create_task( + coro, + ) + tasks.append(task) + if tasks: + await asyncio.gather(*tasks) async def _respond_to_off_to_on_event(self, entity_id: str, event: Event) -> None: assert not self.manager.is_proactively_adapting(event.context.id)