Skip to content

Conversation

@TimoPtr
Copy link
Member

@TimoPtr TimoPtr commented Nov 7, 2025

Proposed change

When the component is created we register a callback for the register signal but it is never clear, which could lead to memory leaks especially since the component reloads each time a user enable/disable a sensor on a device. This PR makes sure that we clear when the component is unloaded.

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

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

home-assistant bot commented Nov 7, 2025

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (mobile_app) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mobile_app 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 mobile_app 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.

@TimoPtr TimoPtr force-pushed the mobile-app-setup-unload-register branch from 13efccd to 30bed06 Compare November 7, 2025 10:58
@TimoPtr TimoPtr marked this pull request as ready for review November 7, 2025 11:11
@TimoPtr TimoPtr requested a review from a team as a code owner November 7, 2025 11:11
Comment on lines +346 to +359
async_dispatcher_send(
hass,
"mobile_app_binary_sensor_register",
{
CONF_WEBHOOK_ID: webhook_id,
ATTR_SENSOR_NAME: "Test After Unload",
ATTR_SENSOR_STATE: False,
ATTR_SENSOR_TYPE: "binary_sensor",
ATTR_SENSOR_UNIQUE_ID: "test_after_unload",
ATTR_SENSOR_ICON: "mdi:test",
ATTR_SENSOR_ATTRIBUTES: {},
},
)
await hass.async_block_till_done()
Copy link
Member

Choose a reason for hiding this comment

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

Ideally we don't touch the dispatcher in the tests, let's use the webhook to create a (binary) sensor

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member Author

Choose a reason for hiding this comment

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

Not done, we can't use the webhook easilly since we unload the component so the webhook returns a 201.

Copy link
Member

@MartinHjelmare MartinHjelmare Nov 7, 2025

Choose a reason for hiding this comment

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

@joostlek I think it could be ok to use the dispatcher helper directly here since the alternative is to check for logs, per my suggestion below, which isn't great either. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I guess that makes sense. I kinda wish there was a different way to test that we unregistered these

@TimoPtr TimoPtr requested a review from joostlek November 7, 2025 12:24

# This binary sensor should be created successfully after reload
assert hass.states.get("binary_sensor.test_1_test_after_reload") is not None
assert hass.states.get("binary_sensor.test_1_test_after_reload").state == "on"
Copy link
Member

@MartinHjelmare MartinHjelmare Nov 7, 2025

Choose a reason for hiding this comment

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

I think we should try to check that the dispatch target is properly unregistered between reloads, by checking that there's not a unique id error when adding the entity. Set the same unique id in the webhook data to try to provoke this. There should be an error log about duplicate unique id.

Verify that the test works manually by running it without the fix.

Copy link
Member

Choose a reason for hiding this comment

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

f"Platform {self.platform_name} does not generate unique IDs. "

@MartinHjelmare MartinHjelmare marked this pull request as draft November 7, 2025 12:35
This reverts commit 108866a.
@TimoPtr TimoPtr marked this pull request as ready for review November 10, 2025 08:38
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Let's merge here. Joost and me have been discussing how to improve tests like this in general but we should do that separately.

@MartinHjelmare MartinHjelmare added this to the 2025.11.2 milestone Nov 12, 2025
@MartinHjelmare MartinHjelmare merged commit 4615145 into home-assistant:dev Nov 12, 2025
36 checks passed
@TimoPtr
Copy link
Member Author

TimoPtr commented Nov 12, 2025

Let's merge here. Joost and me have been discussing how to improve tests like this in general but we should do that separately.

Lemme know when you agree on something so I can update the test 👍🏻

@github-actions github-actions bot locked and limited conversation to collaborators Nov 13, 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.

4 participants