Skip to content
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

Fix issue 108296 - nfandroidtv service notify disappears when restarting home assistant #128958

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

sVnsation
Copy link
Contributor

@sVnsation sVnsation commented Oct 22, 2024

sVnsation:sVnsation-nfandroidtv_issue_108296

Breaking change

No

Proposed change

Fixes Issue #108296

These changes should ensure that the notify action ‘notify.android_tv’ is always available, even if the Android Device (TV) is switched off. The connection is only established when a message is actually to be sent.

  1. in __init__.py:

    • We store the host information in hass.data[DOMAIN][entry.entry_id] to be able to use it later in notify.py.
  2. in notify.py:

    • The NFAndroidTVNotificationService class has been customised to store the host information and establish the connection when needed.
    • The send_message method now tries to establish a connection if it does not yet exist and catches ConnectError exceptions.

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

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 @tkdrob, mind taking a look at this pull request as it has been labeled with an integration (nfandroidtv) you are listed as a code owner for? Thanks!

Code owner commands

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

@sVnsation sVnsation closed this Oct 22, 2024
@sVnsation sVnsation reopened this Oct 22, 2024
@sVnsation sVnsation marked this pull request as draft October 22, 2024 10:24
@sVnsation sVnsation marked this pull request as ready for review October 22, 2024 10:27
@sVnsation sVnsation marked this pull request as draft October 22, 2024 10:33
@sVnsation sVnsation closed this Oct 22, 2024
@sVnsation sVnsation force-pushed the sVnsation-nfandroidtv_issue_108296 branch from 15e2396 to 4a94fb9 Compare October 22, 2024 11:00
@sVnsation sVnsation reopened this Oct 22, 2024
@sVnsation sVnsation force-pushed the sVnsation-nfandroidtv_issue_108296 branch from 803f7d7 to 19372ad Compare October 22, 2024 11:34
@sVnsation sVnsation closed this Oct 22, 2024
@sVnsation sVnsation force-pushed the sVnsation-nfandroidtv_issue_108296 branch from 19372ad to cdf8099 Compare October 22, 2024 11:37
@b2un0
Copy link

b2un0 commented Oct 23, 2024

+1

@wernerhp
Copy link

Thanks for the fix!

@i8nemo
Copy link

i8nemo commented Oct 29, 2024

Im new to this: Can I confirm that I just need to replace my local HA files with the below new files for the fix?
__init__.py:
notify.py:

Copy link
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

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

This PR is incorrect.

  1. Previously we tried connecting when setting up the integration, and if we could not connect we would raise ConfigEntryNotReady, causing the setup to be tried again a minute later.
  2. I am not sure how error handling fixes the notify service dissapearing from Home Assistant. Can you elaborate on that? Because if the integration is unable to setup, it would have a reason to

@home-assistant home-assistant bot marked this pull request as draft October 29, 2024 11:52
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@sVnsation
Copy link
Contributor Author

sVnsation commented Oct 29, 2024

  1. I understand the reason of the current implementation of the setup. However, when Home Assistant is restarted, setting up the integration is repeated every time. If the TV is switched off, it fails and the notify service is not created.
    It certainly depends on how you look at it. But, it is not absolutely necessary to establish a connection when setting up the integration. When setting up for the first time, it is of course helpful to receive direct feedback as to whether the connection is working or not. The TV is certainly switched on at this point of time.
    However, if Home Assistant is restarted later in time, the TV is probably switched off and the setup cannot establish a connection to the TV and therefore fails to setup. This leads to a ‘disappearing’ notification service, but in reality it was not created after the restart.

  2. If the establishment of the connection is removed from the async_setup_entry of the integration, then the notify service can be created even if the TV is switched off.

@sVnsation
Copy link
Contributor Author

@joostlek Thank you for reviewing this pull request.

I did some further testing.
When you setup the integration for the first time you still get a error message when the TV is switched off or something else is wrong. Because of the config_flow.py implementation. This was tested with the changes from the pull request.
I think connecting when setting up the integration in async_setup_entry was used in the past while using yaml configuration.

So in my opinion it's perfectly fine to remove ConfigEntryNotReady from __init__.py.
Because ConfigFlow shows an error if something is wrong when first time setting up the integration.

grafik

@joostlek
Copy link
Member

Right, let me ask around to see what we want with this situation

@Paul-Vdp
Copy link

Paul-Vdp commented Nov 3, 2024

+1

@sVnsation sVnsation requested a review from joostlek December 6, 2024 12:59
@erkr
Copy link

erkr commented Dec 8, 2024

+1

@wernerhp
Copy link

wernerhp commented Dec 8, 2024

🎗️

@erkr
Copy link

erkr commented Dec 8, 2024

My two cents:

  • TV needs to be on for first time discovery
  • once added, the notify service should always be created at startup (tv on or off)
  • add an extra state entity that reflects the TV being detected On or Off. Use case: don't call the notify when the TV is off to avoid errors in my log

@sVnsation
Copy link
Contributor Author

Any news on this? I would also be happy to write tests if the changes are accepted.

@sVnsation sVnsation marked this pull request as ready for review January 2, 2025 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

service notify.my_android_tv disappears when restarting home assistant
7 participants