@@ -51,17 +51,18 @@ def __init__(self, auto_area) -> None:
5151 or []
5252 )
5353
54+ # Entities
5455 self .sleep_mode_entity_id = (
5556 f"{ SLEEP_MODE_SWITCH_ENTITY_PREFIX } {
56- slugify (self .auto_area .area . name )} "
57+ slugify (self .auto_area .area_name )} "
5758 )
5859 self .presence_entity_id = (
5960 f"{ PRESENCE_BINARY_SENSOR_ENTITY_PREFIX } {
60- slugify (self .auto_area .area . name )} "
61+ slugify (self .auto_area .area_name )} "
6162 )
6263 self .illuminance_entity_id = (
6364 f"{ ILLUMINANCE_SENSOR_ENTITY_PREFIX } {
64- slugify (self .auto_area .area . name )} "
65+ slugify (self .auto_area .area_name )} "
6566 )
6667
6768 self .light_entity_ids = [
@@ -80,17 +81,22 @@ def __init__(self, auto_area) -> None:
8081
8182 LOGGER .debug (
8283 "%s: Managing light entities: %s" ,
83- self .auto_area .area . name ,
84+ self .auto_area .area_name ,
8485 self .light_entity_ids ,
8586 )
87+
8688 if len (self .light_entity_ids ) == 0 :
8789 LOGGER .warning (
88- "%s: No light entities found to manage" , self .auto_area .area .name
90+ "%s: No light entities found to manage" ,
91+ self .auto_area .area_name
8992 )
90- return
9193
9294 async def initialize (self ):
9395 """Start subscribing to state changes."""
96+ LOGGER .debug ("AutoLights %s %s" ,
97+ self .presence_entity_id ,
98+ self .illuminance_entity_id
99+ )
94100
95101 if self .is_sleeping_area :
96102 # set initial state
@@ -107,13 +113,23 @@ async def initialize(self):
107113 initial_presence_state = self .hass .states .get (self .presence_entity_id )
108114 if initial_presence_state and self .light_entity_ids :
109115 if initial_presence_state .state == STATE_ON :
116+ LOGGER .info (
117+ "%s: Initial presence detected. Turning lights on %s" ,
118+ self .auto_area .area_name ,
119+ self .light_entity_ids ,
120+ )
110121 await self .auto_area .hass .services .async_call (
111122 LIGHT_DOMAIN ,
112123 SERVICE_TURN_ON ,
113124 {ATTR_ENTITY_ID : self .light_entity_ids },
114125 )
115126 self .lights_turned_on = True
116127 else :
128+ LOGGER .info (
129+ "%s: No initial presence detected. Turning lights off %s" ,
130+ self .auto_area .area_name ,
131+ self .light_entity_ids ,
132+ )
117133 await self .auto_area .hass .services .async_call (
118134 LIGHT_DOMAIN ,
119135 SERVICE_TURN_OFF ,
@@ -139,6 +155,8 @@ async def handle_presence_state_change(self, event: Event[EventStateChangedData]
139155 from_state = event .data .get ('old_state' )
140156 to_state = event .data .get ('new_state' )
141157
158+ LOGGER .debug ("handle_presence_state_change" )
159+
142160 previous_state = from_state .state if from_state else ""
143161 if to_state is None :
144162 return
@@ -159,7 +177,7 @@ async def handle_presence_state_change(self, event: Event[EventStateChangedData]
159177 if self .sleep_mode_enabled :
160178 LOGGER .info (
161179 "%s: Sleep mode is on. Not turning on lights" ,
162- self .auto_area .area . name ,
180+ self .auto_area .area_name ,
163181 )
164182 return
165183
@@ -171,7 +189,7 @@ async def handle_presence_state_change(self, event: Event[EventStateChangedData]
171189 ):
172190 LOGGER .info (
173191 "%s: illuminance (%s lx) > threshold (%s lx). Not turning on lights" ,
174- self .auto_area .area . name ,
192+ self .auto_area .area_name ,
175193 current_illuminance ,
176194 self .illuminance_threshold ,
177195 )
@@ -180,7 +198,7 @@ async def handle_presence_state_change(self, event: Event[EventStateChangedData]
180198 # turn lights on
181199 LOGGER .info (
182200 "%s: Turning lights on %s" ,
183- self .auto_area .area . name ,
201+ self .auto_area .area_name ,
184202 self .light_entity_ids ,
185203 )
186204 await self .hass .services .async_call (
@@ -195,7 +213,7 @@ async def handle_presence_state_change(self, event: Event[EventStateChangedData]
195213 if not self .sleep_mode_enabled :
196214 LOGGER .info (
197215 "%s: Turning lights off %s" ,
198- self .auto_area .area . name ,
216+ self .auto_area .area_name ,
199217 self .light_entity_ids ,
200218 )
201219 await self .hass .services .async_call (
@@ -207,6 +225,7 @@ async def handle_presence_state_change(self, event: Event[EventStateChangedData]
207225
208226 async def handle_sleep_mode_state_change (self , event : Event [EventStateChangedData ]):
209227 """Handle changes in sleep mode."""
228+
210229 entity_id = event .data .get ('entity_id' )
211230 from_state = event .data .get ('old_state' )
212231 to_state = event .data .get ('new_state' )
@@ -227,7 +246,7 @@ async def handle_sleep_mode_state_change(self, event: Event[EventStateChangedDat
227246 if current_state == STATE_ON :
228247 LOGGER .info (
229248 "%s: Sleep mode enabled - turning lights off %s" ,
230- self .auto_area .area . name ,
249+ self .auto_area .area_name ,
231250 self .light_entity_ids ,
232251 )
233252 self .sleep_mode_enabled = True
@@ -238,7 +257,10 @@ async def handle_sleep_mode_state_change(self, event: Event[EventStateChangedDat
238257 )
239258 self .lights_turned_on = False
240259 else :
241- LOGGER .info ("%s: Sleep mode disabled" , self .auto_area .area .name )
260+ LOGGER .info (
261+ "%s: Sleep mode disabled" ,
262+ self .auto_area .area_name ,
263+ )
242264 self .sleep_mode_enabled = False
243265 has_presence = (
244266 self .hass .states .get (self .presence_entity_id ).state == STATE_ON
@@ -248,7 +270,7 @@ async def handle_sleep_mode_state_change(self, event: Event[EventStateChangedDat
248270 return
249271 LOGGER .info (
250272 "%s: Turning lights on due to presence %s" ,
251- self .auto_area .area . name ,
273+ self .auto_area .area_name ,
252274 self .light_entity_ids ,
253275 )
254276 await self .hass .services .async_call (
@@ -273,7 +295,7 @@ async def handle_illuminance_change(self, _event: Event[EventStateChangedData]):
273295 if self .lights_turned_on :
274296 LOGGER .debug (
275297 "%s: Lights were already turned on. Not turning on lights" ,
276- self .auto_area .area . name ,
298+ self .auto_area .area_name ,
277299 )
278300 return
279301
@@ -283,7 +305,7 @@ async def handle_illuminance_change(self, _event: Event[EventStateChangedData]):
283305
284306 LOGGER .info (
285307 "%s: Turning lights on due to illuminance %s" ,
286- self .auto_area .area . name ,
308+ self .auto_area .area_name ,
287309 self .light_entity_ids ,
288310 )
289311 await self .hass .services .async_call (
@@ -299,7 +321,7 @@ def is_below_illuminance_threshold(self) -> bool:
299321 if current_illuminance > self .illuminance_threshold :
300322 LOGGER .debug (
301323 "%s: illuminance (%s lx) > threshold (%s lx). Not turning on lights" ,
302- self .auto_area .area . name ,
324+ self .auto_area .area_name ,
303325 current_illuminance ,
304326 self .illuminance_threshold ,
305327 )
@@ -313,14 +335,17 @@ def get_current_illuminance(self) -> float | None:
313335 current_illuminance = float (
314336 self .hass .states .get (self .illuminance_entity_id ).state
315337 )
316- except ValueError :
338+ except ( ValueError , AttributeError ) :
317339 current_illuminance = None
318340
319341 return current_illuminance
320342
321343 def cleanup (self ):
322344 """Deinitialize this area."""
323- LOGGER .debug ("%s: Disabling light control" , self .auto_area .area .name )
345+ LOGGER .debug (
346+ "%s: Disabling light control" ,
347+ self .auto_area .area_name ,
348+ )
324349 if self .unsubscribe_sleep_mode is not None :
325350 self .unsubscribe_sleep_mode ()
326351
0 commit comments