77 ATTR_NAME ,
88 ATTR_USERCODE ,
99 LOCK_USERCODE_PROPERTY ,
10+ LOCK_USERCODE_STATUS_PROPERTY ,
11+ CodeSlotStatus ,
1012 CommandClass ,
1113)
1214from ..exceptions import NotFoundError
1315from ..model .node import Node
1416from ..model .value import get_value_id , Value
1517
1618
17- def get_code_slot_value (node : Node , code_slot : int ) -> Value :
18- """Get a value."""
19+ def get_code_slot_value (node : Node , code_slot : int , property_name : str ) -> Value :
20+ """Get a code slot value."""
1921 value = node .values .get (
2022 get_value_id (
2123 node ,
2224 {
2325 "commandClass" : CommandClass .USER_CODE ,
24- "property" : LOCK_USERCODE_PROPERTY ,
26+ "property" : property_name ,
2527 "propertyKeyName" : str (code_slot ),
2628 },
2729 )
2830 )
2931
3032 if not value :
31- raise NotFoundError (f"Code slot { code_slot } not found" )
33+ raise NotFoundError (f"{ property_name } for code slot { code_slot } not found" )
3234
3335 return value
3436
@@ -43,7 +45,10 @@ def _get_code_slots(
4345 # Loop until we can't find a code slot
4446 while True :
4547 try :
46- value = get_code_slot_value (node , code_slot )
48+ value = get_code_slot_value (node , code_slot , LOCK_USERCODE_PROPERTY )
49+ status_value = get_code_slot_value (
50+ node , code_slot , LOCK_USERCODE_STATUS_PROPERTY
51+ )
4752 except NotFoundError :
4853 return slots
4954
@@ -52,7 +57,7 @@ def _get_code_slots(
5257 slot = {
5358 ATTR_CODE_SLOT : int (value .property_key ), # type: ignore
5459 ATTR_NAME : value .metadata .label ,
55- ATTR_IN_USE : bool ( value .value ) ,
60+ ATTR_IN_USE : status_value .value == CodeSlotStatus . ENABLED ,
5661 }
5762 if include_usercode :
5863 slot [ATTR_USERCODE ] = value .value if value .value else None
@@ -73,16 +78,22 @@ def get_usercodes(node: Node) -> List[Dict[str, Optional[Union[int, bool, str]]]
7378
7479def get_usercode (node : Node , code_slot : int ) -> Optional [str ]:
7580 """Get usercode from slot X on the lock."""
76- value = get_code_slot_value (node , code_slot )
81+ value = get_code_slot_value (node , code_slot , LOCK_USERCODE_PROPERTY )
7782
7883 return str (value .value ) if value .value else None
7984
8085
8186async def set_usercode (node : Node , code_slot : int , usercode : str ) -> None :
8287 """Set the usercode to index X on the lock."""
83- value = get_code_slot_value (node , code_slot )
88+ value = get_code_slot_value (node , code_slot , LOCK_USERCODE_PROPERTY )
8489
8590 if len (str (usercode )) < 4 :
8691 raise ValueError ("User code must be at least 4 digits" )
8792
8893 await node .async_set_value (value , usercode )
94+
95+
96+ async def clear_usercode (node : Node , code_slot : int ) -> None :
97+ """Clear a code slot on the lock."""
98+ value = get_code_slot_value (node , code_slot , LOCK_USERCODE_STATUS_PROPERTY )
99+ await node .async_set_value (value , CodeSlotStatus .AVAILABLE .value )
0 commit comments