Skip to content

Fix Shelly entity removal #147665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2025
Merged

Conversation

thecode
Copy link
Member

@thecode thecode commented Jun 27, 2025

Proposed change

Fix Shelly entity removal
Shelly uses conditions to remove entities when the device configuration change (for example the output type is changed from Switch to Light. This logic was broken and the test just checked that Switch entity is not created in Light mode, but in fact the test didn't validate that the Switch entity is actually created to so it can be removed.

Note: There is some cleanup that can be done around the changed code, I plan to do it in a following PR, I limited this PR to the minimal changes lines to fix the bug.

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

  • 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.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

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

Code owner commands

Code owners of shelly can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign shelly Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Copy link
Contributor

@Copilot 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

This PR fixes the removal of Shelly entities when a device’s configuration switches from a switch to a light mode by injecting and processing an RPC config_changed event and waiting out a reload cooldown.

  • Updated tests to simulate RPC events, advance time with freezer, and verify entity removal and creation
  • Modified entity removal logic to derive domain and unique_id via the actual entity class

Reviewed Changes

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

File Description
tests/components/shelly/test_switch.py Added RPC event injection, cooldown wait, and light entity assertion to test switch-to-light removal
homeassistant/components/shelly/entity.py Changed removal logic to instantiate the entity class for unique_id and log removal via LOGGER.debug

@@ -192,8 +192,12 @@ def async_setup_rpc_attribute_entities(
if description.removal_condition and description.removal_condition(
coordinator.device.config, coordinator.device.status, key
):
domain = sensor_class.__module__.split(".")[-1]
unique_id = f"{coordinator.mac}-{key}-{sensor_id}"
entity_class = get_entity_class(sensor_class, description)
Copy link
Preview

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

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

Deriving domain by splitting the __module__ string is brittle; consider adding an explicit platform or domain attribute to the entity description or class for more robust domain determination.

Copilot uses AI. Check for mistakes.

unique_id = f"{coordinator.mac}-{key}-{sensor_id}"
entity_class = get_entity_class(sensor_class, description)
domain = entity_class.__module__.split(".")[-1]
unique_id = entity_class(
Copy link
Preview

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

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

[nitpick] Instantiating the full entity_class just to compute unique_id may incur unnecessary overhead; consider providing a static method or utility function to generate the unique ID without creating the entity instance.

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@chemelli74 chemelli74 left a comment

Choose a reason for hiding this comment

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

Nice spot !

As per our chat, let's proceed and refactor to optimze in a subsquent PR

@thecode
Copy link
Member Author

thecode commented Jun 27, 2025

Thanks 👍

@thecode thecode merged commit 2120ff6 into home-assistant:dev Jun 27, 2025
34 checks passed
@thecode thecode deleted the fix-shelly-entity-removal branch June 27, 2025 15:50
frenck pushed a commit that referenced this pull request Jun 27, 2025
@thecode thecode mentioned this pull request Jun 27, 2025
19 tasks
@github-actions github-actions bot locked and limited conversation to collaborators Jun 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants