@@ -154,12 +154,12 @@ class AbstractTemplateLock(AbstractTemplateEntity, LockEntity):
154154 # This ensures that the __init__ on AbstractTemplateEntity is not called twice.
155155 def __init__ (self , config : dict [str , Any ]) -> None : # pylint: disable=super-init-not-called
156156 """Initialize the features."""
157-
158- self ._state : LockState | None = None
159157 self ._code_format_template = config .get (CONF_CODE_FORMAT )
160- self ._code_format : str | None = None
161158 self ._code_format_template_error : TemplateError | None = None
162159
160+ # Legacy behavior, create all locks as Unlocked.
161+ self ._set_state (LockState .UNLOCKED )
162+
163163 def _iterate_scripts (
164164 self , config : dict [str , Any ]
165165 ) -> Generator [tuple [str , Sequence [dict [str , Any ]], LockEntityFeature | int ]]:
@@ -171,44 +171,17 @@ def _iterate_scripts(
171171 if (action_config := config .get (action_id )) is not None :
172172 yield (action_id , action_config , supported_feature )
173173
174- @property
175- def is_locked (self ) -> bool :
176- """Return true if lock is locked."""
177- return self ._state == LockState .LOCKED
178-
179- @property
180- def is_jammed (self ) -> bool :
181- """Return true if lock is jammed."""
182- return self ._state == LockState .JAMMED
183-
184- @property
185- def is_unlocking (self ) -> bool :
186- """Return true if lock is unlocking."""
187- return self ._state == LockState .UNLOCKING
188-
189- @property
190- def is_locking (self ) -> bool :
191- """Return true if lock is locking."""
192- return self ._state == LockState .LOCKING
193-
194- @property
195- def is_open (self ) -> bool :
196- """Return true if lock is open."""
197- return self ._state == LockState .OPEN
198-
199- @property
200- def is_opening (self ) -> bool :
201- """Return true if lock is opening."""
202- return self ._state == LockState .OPENING
203-
204- @property
205- def code_format (self ) -> str | None :
206- """Regex for code format or None if no code is required."""
207- return self ._code_format
174+ def _set_state (self , state : LockState | None ) -> None :
175+ self ._attr_is_jammed = state == LockState .JAMMED
176+ self ._attr_is_opening = state == LockState .OPENING
177+ self ._attr_is_locking = state == LockState .LOCKING
178+ self ._attr_is_open = state == LockState .OPEN
179+ self ._attr_is_unlocking = state == LockState .UNLOCKING
180+ self ._attr_is_locked = state == LockState .LOCKED
208181
209182 def _handle_state (self , result : Any ) -> None :
210183 if isinstance (result , bool ):
211- self ._state = LockState .LOCKED if result else LockState .UNLOCKED
184+ self ._set_state ( LockState .LOCKED if result else LockState .UNLOCKED )
212185 return
213186
214187 if isinstance (result , str ):
@@ -217,33 +190,33 @@ def _handle_state(self, result: Any) -> None:
217190 "on" ,
218191 "locked" ,
219192 ):
220- self ._state = LockState .LOCKED
193+ self ._set_state ( LockState .LOCKED )
221194 elif result .lower () in (
222195 "false" ,
223196 "off" ,
224197 "unlocked" ,
225198 ):
226- self ._state = LockState .UNLOCKED
199+ self ._set_state ( LockState .UNLOCKED )
227200 else :
228201 try :
229- self ._state = LockState (result .lower ())
202+ self ._set_state ( LockState (result .lower () ))
230203 except ValueError :
231- self ._state = None
204+ self ._set_state ( None )
232205 return
233206
234- self ._state = None
207+ self ._set_state ( None )
235208
236209 @callback
237210 def _update_code_format (self , render : str | TemplateError | None ):
238211 """Update code format from the template."""
239212 if isinstance (render , TemplateError ):
240- self ._code_format = None
213+ self ._attr_code_format = None
241214 self ._code_format_template_error = render
242215 elif render in (None , "None" , "" ):
243- self ._code_format = None
216+ self ._attr_code_format = None
244217 self ._code_format_template_error = None
245218 else :
246- self ._code_format = render
219+ self ._attr_code_format = render
247220 self ._code_format_template_error = None
248221
249222 async def async_lock (self , ** kwargs : Any ) -> None :
@@ -253,7 +226,7 @@ async def async_lock(self, **kwargs: Any) -> None:
253226 self ._raise_template_error_if_available ()
254227
255228 if self ._attr_assumed_state :
256- self ._state = LockState .LOCKED
229+ self ._set_state ( LockState .LOCKED )
257230 self .async_write_ha_state ()
258231
259232 tpl_vars = {ATTR_CODE : kwargs .get (ATTR_CODE ) if kwargs else None }
@@ -271,7 +244,7 @@ async def async_unlock(self, **kwargs: Any) -> None:
271244 self ._raise_template_error_if_available ()
272245
273246 if self ._attr_assumed_state :
274- self ._state = LockState .UNLOCKED
247+ self ._set_state ( LockState .UNLOCKED )
275248 self .async_write_ha_state ()
276249
277250 tpl_vars = {ATTR_CODE : kwargs .get (ATTR_CODE ) if kwargs else None }
@@ -289,7 +262,7 @@ async def async_open(self, **kwargs: Any) -> None:
289262 self ._raise_template_error_if_available ()
290263
291264 if self ._attr_assumed_state :
292- self ._state = LockState .OPEN
265+ self ._set_state ( LockState .OPEN )
293266 self .async_write_ha_state ()
294267
295268 tpl_vars = {ATTR_CODE : kwargs .get (ATTR_CODE ) if kwargs else None }
@@ -343,7 +316,7 @@ def _update_state(self, result: str | TemplateError) -> None:
343316 """Update the state from the template."""
344317 super ()._update_state (result )
345318 if isinstance (result , TemplateError ):
346- self ._state = None
319+ self ._attr_is_locked = None
347320 return
348321
349322 self ._handle_state (result )
@@ -353,14 +326,14 @@ def _async_setup_templates(self) -> None:
353326 """Set up templates."""
354327 if self ._template is not None :
355328 self .add_template_attribute (
356- "_state " ,
329+ "_attr_is_locked " ,
357330 self ._template ,
358331 None ,
359332 self ._update_state ,
360333 )
361334 if self ._code_format_template :
362335 self .add_template_attribute (
363- "_code_format_template " ,
336+ "_attr_code_format " ,
364337 self ._code_format_template ,
365338 None ,
366339 self ._update_code_format ,
0 commit comments