Skip to content

Commit 4d4d9b7

Browse files
raman325claude
andcommitted
Extract _async_check_existing_usercodes as top-level function
Move inner function to module level with explicit parameters. Rename to _async_check_existing_usercodes to distinguish from provider get_usercodes operations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Entire-Checkpoint: 10d0912a53d7
1 parent 6789251 commit 4d4d9b7

1 file changed

Lines changed: 58 additions & 51 deletions

File tree

custom_components/lock_code_manager/config_flow.py

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,51 @@ def _check_common_slots(
119119
}
120120

121121

122+
async def _async_check_existing_usercodes(
123+
hass: HomeAssistant,
124+
dev_reg: dr.DeviceRegistry,
125+
ent_reg: er.EntityRegistry,
126+
lock_entity_id: str,
127+
) -> tuple[Any, dict[int, str | SlotCode]]:
128+
"""Query a single lock for all existing usercodes.
129+
130+
Returns (lock_instance, usercodes) where usercodes is the full dict from
131+
the provider (managed and unmanaged, including empty slots). Callers are
132+
responsible for filtering.
133+
134+
Raises LockCodeManagerError for expected skip conditions (missing entity,
135+
unsupported platform, missing config entry). Unexpected exceptions from
136+
the provider propagate directly.
137+
"""
138+
lock_entry = ent_reg.async_get(lock_entity_id)
139+
if not lock_entry:
140+
_LOGGER.warning(
141+
"Entity %s not found in registry; skipping usercode check",
142+
lock_entity_id,
143+
)
144+
raise LockCodeManagerError(lock_entity_id)
145+
if lock_entry.platform not in INTEGRATIONS_CLASS_MAP:
146+
_LOGGER.debug(
147+
"Lock %s uses unsupported platform %s; skipping usercode check",
148+
lock_entity_id,
149+
lock_entry.platform,
150+
)
151+
raise LockCodeManagerError(lock_entity_id)
152+
lock_config_entry = hass.config_entries.async_get_entry(lock_entry.config_entry_id)
153+
if lock_config_entry is None:
154+
_LOGGER.warning(
155+
"Config entry for lock %s not found; skipping usercode check",
156+
lock_entity_id,
157+
)
158+
raise LockCodeManagerError(lock_entity_id)
159+
160+
lock_instance = INTEGRATIONS_CLASS_MAP[lock_entry.platform](
161+
hass, dev_reg, ent_reg, lock_config_entry, lock_entry
162+
)
163+
usercodes = await lock_instance.async_internal_get_usercodes()
164+
return lock_instance, usercodes
165+
166+
122167
async def _async_get_unmanaged_codes(
123168
hass: HomeAssistant,
124169
dev_reg: dr.DeviceRegistry,
@@ -134,44 +179,23 @@ async def _async_get_unmanaged_codes(
134179
- Dict keyed by lock entity ID to temporary lock provider instances, for
135180
reuse in clear/adopt steps.
136181
"""
137-
138-
async def _query_lock(
139-
lock_entity_id: str,
140-
) -> tuple[Any, dict[int, str | SlotCode]]:
141-
"""Query a single lock for unmanaged codes.
142-
143-
Returns (lock_instance, unmanaged_codes) or raises on any skip condition.
144-
Logs the reason before raising so the outer loop can use a bare except.
145-
"""
146-
lock_entry = ent_reg.async_get(lock_entity_id)
147-
if not lock_entry:
148-
_LOGGER.warning(
149-
"Entity %s not found in registry; skipping usercode check",
150-
lock_entity_id,
151-
)
152-
raise LockCodeManagerError(lock_entity_id)
153-
if lock_entry.platform not in INTEGRATIONS_CLASS_MAP:
154-
_LOGGER.debug(
155-
"Lock %s uses unsupported platform %s; skipping usercode check",
156-
lock_entity_id,
157-
lock_entry.platform,
182+
result: dict[str, dict[int, str | SlotCode]] = {}
183+
lock_instances: dict[str, Any] = {}
184+
for lock_entity_id in lock_entity_ids:
185+
try:
186+
lock_instance, usercodes = await _async_check_existing_usercodes(
187+
hass, dev_reg, ent_reg, lock_entity_id
158188
)
159-
raise LockCodeManagerError(lock_entity_id)
160-
lock_config_entry = hass.config_entries.async_get_entry(
161-
lock_entry.config_entry_id
162-
)
163-
if lock_config_entry is None:
189+
except LockCodeManagerError:
190+
continue
191+
except Exception: # noqa: BLE001
164192
_LOGGER.warning(
165-
"Config entry for lock %s not found; skipping usercode check",
193+
"Failed to get usercodes from %s during lock reset check; "
194+
"this lock's codes will not be shown",
166195
lock_entity_id,
196+
exc_info=True,
167197
)
168-
raise LockCodeManagerError(lock_entity_id)
169-
170-
lock_instance = INTEGRATIONS_CLASS_MAP[lock_entry.platform](
171-
hass, dev_reg, ent_reg, lock_config_entry, lock_entry
172-
)
173-
usercodes = await lock_instance.async_internal_get_usercodes()
174-
198+
continue
175199
managed_slots = {
176200
int(s)
177201
for entry in hass.config_entries.async_entries(DOMAIN)
@@ -183,23 +207,6 @@ async def _query_lock(
183207
for slot, code in usercodes.items()
184208
if code is not SlotCode.EMPTY and slot not in managed_slots
185209
}
186-
return lock_instance, unmanaged
187-
188-
result: dict[str, dict[int, str | SlotCode]] = {}
189-
lock_instances: dict[str, Any] = {}
190-
for lock_entity_id in lock_entity_ids:
191-
try:
192-
lock_instance, unmanaged = await _query_lock(lock_entity_id)
193-
except LockCodeManagerError:
194-
continue
195-
except Exception: # noqa: BLE001
196-
_LOGGER.warning(
197-
"Failed to get usercodes from %s during lock reset check; "
198-
"this lock's codes will not be shown",
199-
lock_entity_id,
200-
exc_info=True,
201-
)
202-
continue
203210
if unmanaged:
204211
result[lock_entity_id] = unmanaged
205212
lock_instances[lock_entity_id] = lock_instance

0 commit comments

Comments
 (0)