@@ -49,19 +49,12 @@ async def async_setup_entry(
49
49
async_add_entities : AddEntitiesCallback ,
50
50
) -> bool :
51
51
"""Set up config entry."""
52
- coordinators : list [LockUsercodeUpdateCoordinator ] = list (
53
- hass .data [DOMAIN ][config_entry .entry_id ][COORDINATORS ].values ()
54
- )
55
52
56
53
@callback
57
54
def add_pin_enabled_entity (slot_num : int , ent_reg : er .EntityRegistry ) -> None :
58
55
"""Add PIN enabled binary sensor entities for slot."""
59
56
async_add_entities (
60
- [
61
- LockCodeManagerPINSyncedEntity (
62
- hass , ent_reg , config_entry , coordinators , slot_num
63
- )
64
- ],
57
+ [LockCodeManagerPINSyncedEntity (hass , ent_reg , config_entry , slot_num )],
65
58
True ,
66
59
)
67
60
@@ -84,14 +77,12 @@ def __init__(
84
77
hass : HomeAssistant ,
85
78
ent_reg : er .EntityRegistry ,
86
79
config_entry : ConfigEntry ,
87
- coordinators : list [LockUsercodeUpdateCoordinator ],
88
80
slot_num : int ,
89
81
) -> None :
90
82
"""Initialize entity."""
91
83
BaseLockCodeManagerEntity .__init__ (
92
84
self , hass , ent_reg , config_entry , slot_num , ATTR_PIN_SYNCED_TO_LOCKS
93
85
)
94
- self .coordinators = coordinators
95
86
self ._entity_id_map : dict [str , str ] = {}
96
87
self ._issue_reg : ir .IssueRegistry | None = None
97
88
self ._call_later_unsub : Callable | None = None
@@ -114,18 +105,7 @@ async def async_update_usercodes(
114
105
"""Update usercodes on locks based on state change."""
115
106
if not states :
116
107
states = {}
117
- disabling_entity_ids = (
118
- state ["entity_id" ]
119
- for key , state in states .items ()
120
- if (key != CONF_NUMBER_OF_USES and state ["state" ] != STATE_ON )
121
- or (
122
- key == CONF_NUMBER_OF_USES
123
- and (
124
- state ["state" ] in (STATE_UNAVAILABLE , STATE_UNKNOWN )
125
- or int (float (state ["state" ])) == 0
126
- )
127
- )
128
- )
108
+ coordinators : list [LockUsercodeUpdateCoordinator ] = []
129
109
for lock in self .locks :
130
110
lock_slot_sensor_state = self ._lock_slot_sensor_state (lock )
131
111
if self .is_on :
@@ -145,27 +125,52 @@ async def async_update_usercodes(
145
125
if lock_slot_sensor_state == pin_state .state :
146
126
continue
147
127
128
+ await lock .async_set_usercode (
129
+ int (self .slot_num ), pin_state .state , name_state .state
130
+ )
131
+
148
132
_LOGGER .info (
149
- "%s (%s): Setting usercode for %s slot %s" ,
133
+ "%s (%s): Set usercode for %s slot %s" ,
150
134
self .config_entry .entry_id ,
151
135
self .config_entry .title ,
152
136
lock .lock .entity_id ,
153
137
self .slot_num ,
154
138
)
155
-
156
- await lock .async_set_usercode (
157
- int (self .slot_num ), pin_state .state , name_state .state
158
- )
159
139
else :
160
140
if lock_slot_sensor_state in (
161
141
"" ,
162
142
STATE_UNKNOWN ,
163
143
):
164
144
continue
165
145
146
+ if not (
147
+ disabling_entity_ids := list (
148
+ state ["entity_id" ]
149
+ for key , state in states .items ()
150
+ if (
151
+ key not in (CONF_NUMBER_OF_USES , CONF_PIN )
152
+ and state ["state" ] != STATE_ON
153
+ )
154
+ or (
155
+ key == CONF_NUMBER_OF_USES
156
+ and (
157
+ state ["state" ] in (STATE_UNAVAILABLE , STATE_UNKNOWN )
158
+ or int (float (state ["state" ])) == 0
159
+ )
160
+ )
161
+ or (
162
+ key == CONF_PIN
163
+ and state ["state" ] != self ._lock_slot_sensor_state (lock )
164
+ )
165
+ )
166
+ ):
167
+ return
168
+
169
+ await lock .async_clear_usercode (int (self .slot_num ))
170
+
166
171
_LOGGER .info (
167
172
(
168
- "%s (%s): Clearing usercode for lock %s slot %s because the "
173
+ "%s (%s): Cleared usercode for lock %s slot %s because the "
169
174
"following entities indicate the slot is disabled: %s"
170
175
),
171
176
self .config_entry .entry_id ,
@@ -175,12 +180,14 @@ async def async_update_usercodes(
175
180
", " .join (disabling_entity_ids ),
176
181
)
177
182
178
- await lock .async_clear_usercode (int (self .slot_num ))
179
-
180
- await asyncio .gather (
181
- * [coordinator .async_refresh () for coordinator in self .coordinators ]
183
+ coordinators .append (
184
+ self .hass .data [DOMAIN ][COORDINATORS ][lock .lock .entity_id ]
182
185
)
183
186
187
+ await asyncio .gather (
188
+ * [coordinator .async_refresh () for coordinator in coordinators ]
189
+ )
190
+
184
191
async def _update_state (self , _ : datetime | None = None ) -> None :
185
192
"""Update binary sensor state by getting dependent states."""
186
193
if self ._call_later_unsub :
0 commit comments