Skip to content

Commit f4cfd1f

Browse files
raman325claude
andcommitted
Extract _query_lock inner function for cleaner control flow
Replace guard-clause-with-continue pattern with an inner function that raises LockCodeManagerError on skip conditions. The outer loop catches expected skips (LockCodeManagerError) and unexpected failures (Exception) separately. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Entire-Checkpoint: 2c7783140f9d
1 parent b4dcb72 commit f4cfd1f

1 file changed

Lines changed: 36 additions & 19 deletions

File tree

custom_components/lock_code_manager/config_flow.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
EXCLUDED_CONDITION_PLATFORMS,
3737
)
3838
from .data import get_entry_data
39+
from .exceptions import LockCodeManagerError
3940
from .models import SlotCode
4041
from .providers import INTEGRATIONS_CLASS_MAP
4142

@@ -133,23 +134,29 @@ async def _async_get_unmanaged_codes(
133134
- Dict keyed by lock entity ID to temporary lock provider instances, for
134135
reuse in clear/adopt steps.
135136
"""
136-
result: dict[str, dict[int, str | SlotCode]] = {}
137-
lock_instances: dict[str, Any] = {}
138-
for lock_entity_id in lock_entity_ids:
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+
"""
139146
lock_entry = ent_reg.async_get(lock_entity_id)
140147
if not lock_entry:
141148
_LOGGER.warning(
142149
"Entity %s not found in registry; skipping usercode check",
143150
lock_entity_id,
144151
)
145-
continue
152+
raise LockCodeManagerError(lock_entity_id)
146153
if lock_entry.platform not in INTEGRATIONS_CLASS_MAP:
147154
_LOGGER.debug(
148155
"Lock %s uses unsupported platform %s; skipping usercode check",
149156
lock_entity_id,
150157
lock_entry.platform,
151158
)
152-
continue
159+
raise LockCodeManagerError(lock_entity_id)
153160
lock_config_entry = hass.config_entries.async_get_entry(
154161
lock_entry.config_entry_id
155162
)
@@ -158,20 +165,13 @@ async def _async_get_unmanaged_codes(
158165
"Config entry for lock %s not found; skipping usercode check",
159166
lock_entity_id,
160167
)
161-
continue
162-
try:
163-
lock_instance = INTEGRATIONS_CLASS_MAP[lock_entry.platform](
164-
hass, dev_reg, ent_reg, lock_config_entry, lock_entry
165-
)
166-
usercodes = await lock_instance.async_internal_get_usercodes()
167-
except Exception: # noqa: BLE001
168-
_LOGGER.warning(
169-
"Failed to get usercodes from %s during lock reset check; "
170-
"this lock's codes will not be shown",
171-
lock_entity_id,
172-
exc_info=True,
173-
)
174-
continue
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+
175175
managed_slots = {
176176
int(s)
177177
for entry in hass.config_entries.async_entries(DOMAIN)
@@ -183,6 +183,23 @@ async def _async_get_unmanaged_codes(
183183
for slot, code in usercodes.items()
184184
if code is not SlotCode.EMPTY and slot not in managed_slots
185185
}
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
186203
if unmanaged:
187204
result[lock_entity_id] = unmanaged
188205
lock_instances[lock_entity_id] = lock_instance

0 commit comments

Comments
 (0)