Fix unload KeyError for failed device setup#5381
Merged
Merged
Conversation
geekypilot
force-pushed
the
fix/async-delete-keyerror
branch
from
June 20, 2026 20:30
0732168 to
ba20064
Compare
geekypilot
marked this pull request as ready for review
June 20, 2026 20:59
make-all
reviewed
Jun 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This fixes an unload crash that can happen when a Tuya device is offline during Home Assistant startup. In that state Home Assistant may leave the config entry in
setup_retry, whiletuya-localnever finishes creating the usual cached device data underhass.data.Later, when Home Assistant reloads, updates, or shuts down that entry, the integration still runs its unload path. Before this change, that path assumed the cached device entry and all of its internal keys existed, so it could raise a
KeyErrorinstead of completing the unload.This PR makes that cleanup path defensive for setup-failed entries:
async_unload_entrynow tolerates a missinghass.data[DOMAIN][device_id]entry and treats it as already unloaded.async_delete_devicenow tolerates missing or partially initialized cached handles.async_stop()before removing the cacheddeviceobject, so a stop failure does not silently discard the device reference first.The intent is to keep normal unload behavior unchanged for devices that completed setup, while preventing failed setup or
setup_retryentries from crashing later reload/update/shutdown flows.Reported traceback
Device context
venta_ah510_humidifier.setup_retry.tuya-local device offline.Scope notes
This PR is intentionally narrow. It addresses unload cleanup for setup-failed or partially initialized entries, but does not try to redesign broader cache recovery behavior. In particular, it does not add new handling for unrelated cases where platform entities were already forwarded and the device cache is later externally corrupted.
Testing
uv run pytest tests/test_config_flow.py::test_async_unload_entry_ignores_missing_device_data -quv run pytest tests/test_device.py::test_delete_keeps_device_entry_when_stop_fails -quv run pytest tests/test_device.py tests/test_config_flow.py -quv run pytestuv run ruff check .uv run ruff check --select I .uv run ruff format --check .uv run yamllint custom_components/tuya_local/devices