Skip to content

Commit 385ab83

Browse files
raman325claude
andcommitted
Use get_managed_slots helper and extract _clear_slots method
- Replace inline managed_slots comprehension with get_managed_slots from data.py (matches PR #989 pattern) - Extract _clear_slots helper to deduplicate the per-lock slot clearing loop used in both lock_reset_clear and lock_reset_adopt Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Entire-Checkpoint: 76009cde6ae0
1 parent 7786073 commit 385ab83

1 file changed

Lines changed: 22 additions & 38 deletions

File tree

custom_components/lock_code_manager/config_flow.py

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
DOMAIN,
3636
EXCLUDED_CONDITION_PLATFORMS,
3737
)
38-
from .data import get_entry_data
38+
from .data import get_entry_data, get_managed_slots
3939
from .exceptions import LockCodeManagerError
4040
from .models import SlotCode
4141
from .providers import INTEGRATIONS_CLASS_MAP
@@ -196,16 +196,11 @@ async def _async_get_unmanaged_codes(
196196
exc_info=True,
197197
)
198198
else:
199-
managed_slots = {
200-
int(s)
201-
for entry in hass.config_entries.async_entries(DOMAIN)
202-
if lock_entity_id in get_entry_data(entry, CONF_LOCKS, [])
203-
for s in get_entry_data(entry, CONF_SLOTS, {})
204-
}
199+
managed = get_managed_slots(hass, lock_entity_id)
205200
unmanaged = {
206201
slot: code
207202
for slot, code in usercodes.items()
208-
if code is not SlotCode.EMPTY and slot not in managed_slots
203+
if code is not SlotCode.EMPTY and slot not in managed
209204
}
210205
if unmanaged:
211206
result[lock_entity_id] = unmanaged
@@ -294,19 +289,17 @@ async def async_step_lock_reset(
294289
description_placeholders={"slot_summary": slot_summary},
295290
)
296291

297-
async def async_step_lock_reset_clear(
298-
self, user_input: dict[str, Any] | None = None
299-
) -> dict[str, Any]:
300-
"""Clear all unmanaged codes from the selected locks."""
301-
for lock_entity_id, codes in self._unmanaged_codes.items():
292+
async def _clear_slots(self, slots_to_clear: dict[str, list[int]]) -> None:
293+
"""Clear specified slots on each lock, logging failures."""
294+
for lock_entity_id, slots in slots_to_clear.items():
302295
lock_instance = self._lock_instances.get(lock_entity_id)
303296
if not lock_instance:
304297
_LOGGER.warning(
305-
"No lock instance for %s; cannot clear unmanaged codes",
298+
"No lock instance for %s; cannot clear codes",
306299
lock_entity_id,
307300
)
308301
continue
309-
for slot in codes:
302+
for slot in slots:
310303
try:
311304
await lock_instance.async_internal_clear_usercode(
312305
slot, source="direct"
@@ -318,6 +311,14 @@ async def async_step_lock_reset_clear(
318311
lock_entity_id,
319312
exc_info=True,
320313
)
314+
315+
async def async_step_lock_reset_clear(
316+
self, user_input: dict[str, Any] | None = None
317+
) -> dict[str, Any]:
318+
"""Clear all unmanaged codes from the selected locks."""
319+
await self._clear_slots(
320+
{lid: list(codes) for lid, codes in self._unmanaged_codes.items()}
321+
)
321322
self._unmanaged_codes = {}
322323
self._lock_instances = {}
323324
return await self.async_step_choose_path()
@@ -350,29 +351,12 @@ async def async_step_lock_reset_adopt(
350351
}
351352

352353
# Clear masked codes that were not adopted
353-
for lock_entity_id, codes in self._unmanaged_codes.items():
354-
masked_slots = [s for s, c in codes.items() if c is SlotCode.UNKNOWN]
355-
if not masked_slots:
356-
continue
357-
lock_instance = self._lock_instances.get(lock_entity_id)
358-
if not lock_instance:
359-
_LOGGER.warning(
360-
"No lock instance for %s; cannot clear masked codes",
361-
lock_entity_id,
362-
)
363-
continue
364-
for slot in masked_slots:
365-
try:
366-
await lock_instance.async_internal_clear_usercode(
367-
slot, source="direct"
368-
)
369-
except Exception: # noqa: BLE001
370-
_LOGGER.warning(
371-
"Failed to clear masked slot %s on %s",
372-
slot,
373-
lock_entity_id,
374-
exc_info=True,
375-
)
354+
await self._clear_slots(
355+
{
356+
lid: [s for s, c in codes.items() if c is SlotCode.UNKNOWN]
357+
for lid, codes in self._unmanaged_codes.items()
358+
}
359+
)
376360

377361
self._unmanaged_codes = {}
378362
self._lock_instances = {}

0 commit comments

Comments
 (0)