1010)
1111from homeassistant .util import slugify
1212
13- from .ha_helpers import get_all_entities
14-
1513from .const import (
16- DOMAIN ,
1714 CONFIG_AUTO_LIGHTS_MAX_ILLUMINANCE ,
18- EXCLUDED_DOMAINS ,
1915 ILLUMINANCE_SENSOR_ENTITY_PREFIX ,
16+ LIGHT_GROUP_ENTITY_PREFIX ,
2017 LOGGER ,
2118 CONFIG_IS_SLEEPING_AREA ,
2219 CONFIG_EXCLUDED_LIGHT_ENTITIES ,
@@ -66,33 +63,20 @@ def __init__(self, auto_area) -> None:
6663 f"{ ILLUMINANCE_SENSOR_ENTITY_PREFIX } {
6764 slugify (self .auto_area .area_name )} "
6865 )
69-
70- self .light_entity_ids = [
71- entity .entity_id
72- for entity in get_all_entities (
73- self .auto_area .entity_registry ,
74- self .auto_area .device_registry ,
75- self .auto_area .area_id ,
76- [LIGHT_DOMAIN ],
77- )
78- if entity .entity_id not in self .excluded_light_entities and entity .platform not in EXCLUDED_DOMAINS
79- ]
66+ self .light_group_entity_id = (
67+ f"{ LIGHT_GROUP_ENTITY_PREFIX } {
68+ slugify (self .auto_area .area_name )} "
69+ )
8070
8171 self .sleep_mode_enabled = None
8272 self .lights_turned_on = None
8373
8474 LOGGER .debug (
85- "%s: Managing light entities : %s" ,
75+ "%s: Managing light group entity : %s" ,
8676 self .auto_area .area_name ,
87- self .light_entity_ids ,
77+ self .light_group_entity_id ,
8878 )
8979
90- if len (self .light_entity_ids ) == 0 :
91- LOGGER .warning (
92- "%s: No light entities found to manage" ,
93- self .auto_area .area_name
94- )
95-
9680 async def initialize (self ):
9781 """Start subscribing to state changes."""
9882 LOGGER .debug ("AutoLights %s %s" ,
@@ -113,31 +97,21 @@ async def initialize(self):
11397
11498 # set lights initially based on presence
11599 initial_presence_state = self .hass .states .get (self .presence_entity_id )
116- if initial_presence_state and self .light_entity_ids :
100+ if initial_presence_state and self .light_group_entity_id :
117101 if initial_presence_state .state == STATE_ON :
118102 LOGGER .info (
119103 "%s: Initial presence detected. Turning lights on %s" ,
120104 self .auto_area .area_name ,
121- self .light_entity_ids ,
105+ self .light_group_entity_id ,
122106 )
123- await self .auto_area .hass .services .async_call (
124- LIGHT_DOMAIN ,
125- SERVICE_TURN_ON ,
126- {ATTR_ENTITY_ID : self .light_entity_ids },
127- )
128- self .lights_turned_on = True
107+ await self ._turn_lights_on ()
129108 else :
130109 LOGGER .info (
131110 "%s: No initial presence detected. Turning lights off %s" ,
132111 self .auto_area .area_name ,
133- self .light_entity_ids ,
134- )
135- await self .auto_area .hass .services .async_call (
136- LIGHT_DOMAIN ,
137- SERVICE_TURN_OFF ,
138- {ATTR_ENTITY_ID : self .light_entity_ids },
112+ self .light_group_entity_id ,
139113 )
140- self .lights_turned_on = False
114+ await self ._turn_lights_off ()
141115
142116 self .unsubscribe_presence = async_track_state_change_event (
143117 self .auto_area .hass ,
@@ -201,29 +175,35 @@ async def handle_presence_state_change(self, event: Event[EventStateChangedData]
201175 LOGGER .info (
202176 "%s: Turning lights on %s" ,
203177 self .auto_area .area_name ,
204- self .light_entity_ids ,
205- )
206- await self .hass .services .async_call (
207- LIGHT_DOMAIN ,
208- SERVICE_TURN_ON ,
209- {ATTR_ENTITY_ID : self .light_entity_ids },
178+ self .light_group_entity_id ,
210179 )
211- self .lights_turned_on = True
180+ await self ._turn_lights_on ()
212181 return
213182 else :
214183 # turn lights off
215184 if not self .sleep_mode_enabled :
216185 LOGGER .info (
217186 "%s: Turning lights off %s" ,
218187 self .auto_area .area_name ,
219- self .light_entity_ids ,
188+ self .light_group_entity_id ,
220189 )
221- await self .hass .services .async_call (
222- LIGHT_DOMAIN ,
223- SERVICE_TURN_OFF ,
224- {ATTR_ENTITY_ID : self .light_entity_ids },
225- )
226- self .lights_turned_on = False
190+ await self ._turn_lights_off ()
191+
192+ async def _turn_lights_on (self ):
193+ await self .hass .services .async_call (
194+ LIGHT_DOMAIN ,
195+ SERVICE_TURN_ON ,
196+ {ATTR_ENTITY_ID : self .light_group_entity_id },
197+ )
198+ self .lights_turned_on = True
199+
200+ async def _turn_lights_off (self ):
201+ await self .hass .services .async_call (
202+ LIGHT_DOMAIN ,
203+ SERVICE_TURN_OFF ,
204+ {ATTR_ENTITY_ID : self .light_group_entity_id },
205+ )
206+ self .lights_turned_on = False
227207
228208 async def handle_sleep_mode_state_change (self , event : Event [EventStateChangedData ]):
229209 """Handle changes in sleep mode."""
@@ -249,15 +229,10 @@ async def handle_sleep_mode_state_change(self, event: Event[EventStateChangedDat
249229 LOGGER .info (
250230 "%s: Sleep mode enabled - turning lights off %s" ,
251231 self .auto_area .area_name ,
252- self .light_entity_ids ,
232+ self .light_group_entity_id ,
253233 )
254234 self .sleep_mode_enabled = True
255- await self .hass .services .async_call (
256- LIGHT_DOMAIN ,
257- SERVICE_TURN_OFF ,
258- {ATTR_ENTITY_ID : self .light_entity_ids },
259- )
260- self .lights_turned_on = False
235+ await self ._turn_lights_off ()
261236 else :
262237 LOGGER .info (
263238 "%s: Sleep mode disabled" ,
@@ -273,14 +248,9 @@ async def handle_sleep_mode_state_change(self, event: Event[EventStateChangedDat
273248 LOGGER .info (
274249 "%s: Turning lights on due to presence %s" ,
275250 self .auto_area .area_name ,
276- self .light_entity_ids ,
277- )
278- await self .hass .services .async_call (
279- LIGHT_DOMAIN ,
280- SERVICE_TURN_ON ,
281- {ATTR_ENTITY_ID : self .light_entity_ids },
251+ self .light_group_entity_id ,
282252 )
283- self .lights_turned_on = True
253+ await self ._turn_lights_on ()
284254
285255 async def handle_illuminance_change (self , _event : Event [EventStateChangedData ]):
286256 """Handle changes in illuminance."""
@@ -304,13 +274,9 @@ async def handle_illuminance_change(self, _event: Event[EventStateChangedData]):
304274 LOGGER .info (
305275 "%s: Turning lights on due to illuminance %s" ,
306276 self .auto_area .area_name ,
307- self .light_entity_ids ,
308- )
309- await self .hass .services .async_call (
310- LIGHT_DOMAIN ,
311- SERVICE_TURN_ON ,
312- {ATTR_ENTITY_ID : self .light_entity_ids },
277+ self .light_group_entity_id ,
313278 )
279+ await self ._turn_lights_on ()
314280
315281 def is_below_illuminance_threshold (self ) -> bool :
316282 """Evaluate if current illuminance is below threshold."""
0 commit comments