11"""Platform file for Magic Area's light entities."""
22
3+ import asyncio
34import logging
45
56from homeassistant .components .group .light import LightGroup
6- from homeassistant .components .light import DOMAIN as LIGHT_DOMAIN
7+ from homeassistant .components .light import (
8+ ATTR_COLOR_TEMP ,
9+ ATTR_HS_COLOR ,
10+ DOMAIN as LIGHT_DOMAIN ,
11+ SUPPORT_COLOR ,
12+ SUPPORT_COLOR_TEMP ,
13+ )
714from homeassistant .components .switch import DOMAIN as SWITCH_DOMAIN
815from homeassistant .const import (
916 ATTR_ENTITY_ID ,
17+ ATTR_SUPPORTED_FEATURES ,
1018 SERVICE_TURN_OFF ,
1119 SERVICE_TURN_ON ,
1220 STATE_OFF ,
1321 STATE_ON ,
1422)
1523from homeassistant .helpers .dispatcher import async_dispatcher_connect
1624from homeassistant .helpers .event import async_track_state_change_event
25+ from homeassistant .util import color as color_util
1726
1827from .add_entities_when_ready import add_entities_when_ready
1928from .base .entities import MagicEntity
@@ -153,6 +162,7 @@ async def async_turn_on(self, **kwargs) -> None:
153162
154163 # Active lights
155164 active_lights = self ._get_active_lights ()
165+ targeted_lights = self ._entity_ids
156166
157167 if active_lights :
158168 _LOGGER .debug (
@@ -161,17 +171,84 @@ async def async_turn_on(self, **kwargs) -> None:
161171 str (active_lights ),
162172 )
163173
164- data [ ATTR_ENTITY_ID ] = active_lights if active_lights else self . _entity_ids
174+ targeted_lights = active_lights
165175
166- # Forward call
167- await self .hass .services .async_call (
168- LIGHT_DOMAIN ,
169- SERVICE_TURN_ON ,
170- data ,
171- blocking = True ,
172- context = self ._context ,
176+ # Split entities by supported features
177+ entity_map = {SUPPORT_COLOR : [], SUPPORT_COLOR_TEMP : []}
178+ for entity_id in targeted_lights :
179+ state = self .hass .states .get (entity_id )
180+ if not state :
181+ continue
182+ support = state .attributes .get (ATTR_SUPPORTED_FEATURES )
183+
184+ if bool (support & SUPPORT_COLOR ):
185+ if bool (support & SUPPORT_COLOR_TEMP ):
186+ entity_map [SUPPORT_COLOR_TEMP ].append (entity_id )
187+ else :
188+ entity_map [SUPPORT_COLOR ].append (entity_id )
189+
190+ no_color_support = list (
191+ set (targeted_lights )
192+ - set (entity_map [SUPPORT_COLOR ])
193+ - set (entity_map [SUPPORT_COLOR_TEMP ])
173194 )
174195
196+ service_calls = []
197+
198+ if entity_map [SUPPORT_COLOR_TEMP ]:
199+ service_data = data .copy ()
200+ service_data [ATTR_ENTITY_ID ] = entity_map [SUPPORT_COLOR_TEMP ]
201+ service_call = self .hass .services .async_call (
202+ LIGHT_DOMAIN ,
203+ SERVICE_TURN_ON ,
204+ service_data ,
205+ blocking = True ,
206+ context = self ._context ,
207+ )
208+ service_calls .append (service_call )
209+
210+ if entity_map [SUPPORT_COLOR ]:
211+ service_data = data .copy ()
212+ service_data [ATTR_ENTITY_ID ] = entity_map [SUPPORT_COLOR ]
213+
214+ # Perform color_temp emulation if ATTR_COLOR_TEMP is passed
215+ if ATTR_COLOR_TEMP in service_data :
216+ temp_k = color_util .color_temperature_mired_to_kelvin (
217+ service_data [ATTR_COLOR_TEMP ]
218+ )
219+ hs_color = color_util .color_temperature_to_hs (temp_k )
220+ service_data [ATTR_HS_COLOR ] = hs_color
221+ del service_data [ATTR_COLOR_TEMP ]
222+
223+ service_call = self .hass .services .async_call (
224+ LIGHT_DOMAIN ,
225+ SERVICE_TURN_ON ,
226+ service_data ,
227+ blocking = True ,
228+ context = self ._context ,
229+ )
230+ service_calls .append (service_call )
231+
232+ if no_color_support :
233+ service_data = data .copy ()
234+ service_data [ATTR_ENTITY_ID ] = no_color_support
235+ if ATTR_COLOR_TEMP in service_data :
236+ del service_data [ATTR_COLOR_TEMP ]
237+ if ATTR_HS_COLOR in service_data :
238+ del service_data [ATTR_HS_COLOR ]
239+
240+ service_call = self .hass .services .async_call (
241+ LIGHT_DOMAIN ,
242+ SERVICE_TURN_ON ,
243+ service_data ,
244+ blocking = True ,
245+ context = self ._context ,
246+ )
247+ service_calls .append (service_call )
248+
249+ # Perform calls
250+ await asyncio .gather (* service_calls )
251+
175252
176253class AreaLightGroup (MagicLightGroup ):
177254 """Magic Light Group."""
0 commit comments