Skip to content

Improve exceptions handling in WLED#171646

Draft
mik-laj wants to merge 3 commits into
home-assistant:devfrom
mik-laj:wled-exceptions
Draft

Improve exceptions handling in WLED#171646
mik-laj wants to merge 3 commits into
home-assistant:devfrom
mik-laj:wled-exceptions

Conversation

@mik-laj
Copy link
Copy Markdown
Contributor

@mik-laj mik-laj commented May 21, 2026

Breaking change

Proposed change

WLED devices store presets in their internal flash memory (presets.json). This file is prone to corruption — many users have reported devices where it is damaged or missing. Previously, when the python-wled library tried to fetch presets from such a device, it would propagate low-level exceptions (e.g. UnicodeError) that surfaced as cryptic, unhelpful error messages with no guidance for the user. Related issues: #162199 #163772.

The python-wled library (as of frenck/python-wled#2052) now raises typed exceptions - WLEDInvalidResponseError and WLEDEmptyResponseError- for these cases instead of letting raw errors bubble up.

This PR catches those exceptions explicitly in:

  • config_flow.py - during manual setup and Zeroconf discovery
  • coordinator.py - during periodic data updates
  • helpers.py - in the wled_exception_handler decorator used by entity actions (light, switch, select, number)

When the error message indicates the failure is related to presets, a dedicated, actionable error message is shown:

"Failed to download presets from device. Check preset configurations in WLED UI."

This guides users to the WLED web UI where they can repair or reset their preset configuration. For other invalid response errors, a generic message is shown.

Additionally, WLEDUpgradeError is now caught during firmware installation in update.py, replacing a previously unhandled exception with a proper translated error message.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

… in WLED integration

Add proper exception handling for the new exception types introduced in wled library, with differentiated translation keys for invalid response errors and connection errors.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 21, 2026 02:38
@home-assistant home-assistant Bot added cla-signed has-tests integration: wled new-feature Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage labels May 21, 2026
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @frenck, mind taking a look at this pull request as it has been labeled with an integration (wled) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of wled can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign wled Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds more granular WLED error handling (invalid/empty responses and upgrade failures) and aligns tests/translations with the new behavior.

Changes:

  • Add translation keys/messages for invalid responses (including presets-specific) and firmware install failures.
  • Update coordinator/helpers/config flow/update flows to raise translated HomeAssistantError / UpdateFailed for new WLED exception types.
  • Extend WLED component tests to cover the new error scenarios and state behavior.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/components/wled/test_update.py Updates update-install error test to assert translated HomeAssistantError for upgrade failures.
tests/components/wled/test_switch.py Adds invalid-response handling tests ensuring entity does not become unavailable; adjusts call counts.
tests/components/wled/test_select.py Adds invalid-response handling tests ensuring entity does not become unavailable; adjusts call counts.
tests/components/wled/test_number.py Adds invalid-response test for number service calls and availability behavior.
tests/components/wled/test_light.py Expands parametrized coverage to include invalid/empty response errors (presets vs full).
tests/components/wled/test_coordinator.py Consolidates setup-failure tests into a parametrized suite covering more exceptions + log messages.
tests/components/wled/test_config_flow.py Adds config-flow coverage for invalid/empty response errors and consolidates zeroconf abort tests.
tests/components/wled/test_button.py Expands restart button error coverage and adds assertion for reset call.
homeassistant/components/wled/update.py Converts WLEDUpgradeError into translated HomeAssistantError and refreshes coordinator after install attempt.
homeassistant/components/wled/strings.json Adds new config-flow errors and entity error translation messages for presets/upgrade cases.
homeassistant/components/wled/helpers.py Adds handling for invalid/empty responses and maps them to presets vs generic translation keys.
homeassistant/components/wled/coordinator.py Adds invalid/empty response handling during coordinator updates and adjusts translation mapping for failures.
homeassistant/components/wled/config_flow.py Adds invalid/empty response mapping to config-flow error/abort reasons.

Comment thread tests/components/wled/test_number.py Outdated
Comment thread tests/components/wled/test_button.py Outdated
Comment thread homeassistant/components/wled/coordinator.py
Comment thread homeassistant/components/wled/helpers.py
Comment thread tests/components/wled/test_button.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

by-code-owner cla-signed has-tests integration: wled new-feature Quality Scale: platinum Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants