Skip to content

Commit b129277

Browse files
raman325claude
andcommitted
Simplify lock reset step: cache query and collapse menu branches
- Guard _async_get_unmanaged_codes behind check to avoid re-querying locks on menu re-entry - Collapse redundant menu option branches into single has_readable check - Remove unnecessary int() cast on slot keys that are already int Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Entire-Checkpoint: f60a9b6e05e7
1 parent 6287a8c commit b129277

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

custom_components/lock_code_manager/config_flow.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,13 @@ async def async_step_lock_reset(
217217
self, user_input: dict[str, Any] | None = None
218218
) -> dict[str, Any]:
219219
"""Check for unmanaged codes on selected locks and show reset options."""
220-
self._unmanaged_codes, self._lock_instances = await _async_get_unmanaged_codes(
221-
self.hass, self.dev_reg, self.ent_reg, self.data[CONF_LOCKS]
222-
)
220+
if not self._unmanaged_codes:
221+
(
222+
self._unmanaged_codes,
223+
self._lock_instances,
224+
) = await _async_get_unmanaged_codes(
225+
self.hass, self.dev_reg, self.ent_reg, self.data[CONF_LOCKS]
226+
)
223227
if not self._unmanaged_codes:
224228
return await self.async_step_choose_path()
225229

@@ -228,22 +232,9 @@ async def async_step_lock_reset(
228232
for codes in self._unmanaged_codes.values()
229233
for code in codes.values()
230234
)
231-
has_masked = any(
232-
code is SlotCode.UNKNOWN
233-
for codes in self._unmanaged_codes.values()
234-
for code in codes.values()
235-
)
236235

237236
menu_options = ["lock_reset_clear", "lock_reset_cancel"]
238-
if has_readable and not has_masked:
239-
# All readable: offer adopt and clear
240-
menu_options = [
241-
"lock_reset_clear",
242-
"lock_reset_adopt",
243-
"lock_reset_cancel",
244-
]
245-
elif has_readable and has_masked:
246-
# Mix: offer adopt readable (clearing masked) and clear all
237+
if has_readable:
247238
menu_options = [
248239
"lock_reset_clear",
249240
"lock_reset_adopt",
@@ -293,9 +284,8 @@ async def async_step_lock_reset_adopt(
293284
for slot, code in codes.items():
294285
if code is SlotCode.UNKNOWN:
295286
continue
296-
int_slot = int(slot)
297-
if int_slot in self.data[CONF_SLOTS]:
298-
existing_pin = self.data[CONF_SLOTS][int_slot].get(CONF_PIN)
287+
if slot in self.data[CONF_SLOTS]:
288+
existing_pin = self.data[CONF_SLOTS][slot].get(CONF_PIN)
299289
if existing_pin != str(code):
300290
_LOGGER.warning(
301291
"Slot %s has conflicting PINs across locks "
@@ -304,7 +294,7 @@ async def async_step_lock_reset_adopt(
304294
lock_entity_id,
305295
)
306296
continue
307-
self.data[CONF_SLOTS][int_slot] = {
297+
self.data[CONF_SLOTS][slot] = {
308298
CONF_NAME: f"Slot {slot}",
309299
CONF_PIN: str(code),
310300
CONF_ENABLED: True,

0 commit comments

Comments
 (0)