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

Integration stops working randomly #28

Closed
albaintor opened this issue Mar 8, 2024 · 12 comments · Fixed by #36
Closed

Integration stops working randomly #28

albaintor opened this issue Mar 8, 2024 · 12 comments · Fixed by #36
Assignees
Labels
bug Something isn't working

Comments

@albaintor
Copy link
Contributor

Description

Hi,
my android (shield) TV integration stops working randomly

From the logs I have this repetitive warning, don't know why I have this as Android does not support now playing image:

2024-03-08 13:34:47.903602 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.820193 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.737474 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.645467 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.562410 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.479548 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.387415 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.304166 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.221269 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.128610 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:47.045612 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:46.962951 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:46.870786 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format

then key pressed but nothing happens on AndroidTV's side :

2024-03-08 13:34:33.758752 +00:00	ui	INFO	uc.ui.input: Key released: "DPAD_DOWN" "Activity_QMLTYPE_247(0x55a9e1ece0)"
2024-03-08 13:34:33.758242 +00:00	ui	INFO	qml: OVERRIDE RELEASED: DPAD_DOWN Activity_QMLTYPE_247(0x55a9e1ece0)
2024-03-08 13:34:33.725597 +00:00	ui	WARN	qrc:/components/entities/media_player/ImageLoader.qml:61:5: QML Image: Error decoding: data:image/png;base64,: Unsupported image format
2024-03-08 13:34:33.646885 +00:00	intg-androidtv	INFO	DEBUG:ucapi.api:[('127.0.0.1', 60446)] ->: {"kind": "resp", "req_id": 2256, "code": 200, "msg": "result", "msg_data": {}}
2024-03-08 13:34:33.646617 +00:00	intg-androidtv	INFO	INFO:driver:Got 00044BE87F74 command request: cursor_down None
2024-03-08 13:34:33.645345 +00:00	intg-androidtv	INFO	DEBUG:ucapi.api:[('127.0.0.1', 60446)] <-: {"kind":"req","id":2256,"msg":"entity_command","msg_data":{"cmd_id":"cursor_down","entity_id":"00044BE87F74","entity_type":"media_player"}}
2024-03-08 13:34:33.643512 +00:00	ui	INFO	uc.ui.input: Key pressed: "DPAD_DOWN" "Activity_QMLTYPE_247(0x55a9e1ece0)"
2024-03-08 13:34:33.643152 +00:00	ui	INFO	qml: OVERRIDE PRESSED: DPAD_DOWN Activity_QMLTYPE_247(0x55a9e1ece0)

Here are the logs :

UCR2_logs_2024-03-08.zip

How to Reproduce

1/ Launch an activity with TV + Android TV (nvidia shield) integration + HT receiver (media player entity from home assistant)
2/ Use the shield : navigate through menus, launch media
3/ After a while (happens randomly), the commands are not processed (play, pause, down arrow...)

I have set up the remote to go to sleep after 30min. In the activities I didn't enable the option to prevent sleeping, so maybe the remote goes to sleep during 2/ and 3/

Expected behavior

Android TV integration keeps on working

Integration version

No response

Additional context

No response

@albaintor albaintor added the bug Something isn't working label Mar 8, 2024
@zehnm
Copy link
Contributor

zehnm commented Mar 8, 2024

I've finished the Apple TV rewrite and will focus next on the Android TV. Not having any command errors in the logs is suspicious. I have a Shield TV as well and try to reproduce it. It's possible that some return state isn't checked or a disconnect event is missed...

@albaintor
Copy link
Contributor Author

Hi Markus, did you manage to find the problem ?
I have this issue regularly (every day or 2 days). Just after reboot of the remote, the integration works fine. Even after stop/restart of the shield activity it still works correctly, even after a few hours.

This is really weird as the driver receives the commands order and seem to process them with no error : intg-androidtv INFO INFO:driver:Got 00044BE87F74 command request: home None
It is as if the transport stream didn't work behind ? maybe a disconnection which is not caught ?

@albaintor
Copy link
Contributor Author

Update : while started (and not working) commands responded again after 20 minutes
Here are the logs : the first lines around 11:05 are the working commands, whereas at 10:53 they were not.
UCR2_logs_2024-03-27 (2).zip

Could it be related to keys press / release mechanism ?

@zehnm
Copy link
Contributor

zehnm commented Mar 27, 2024

I couldn't reproduce it so far, but I've also been working on PR #34 for a while which changed the connection & reconnection logic. Maybe it's fixed, otherwise I'll investigate it further after the new features have been merged.

@albaintor
Copy link
Contributor Author

I have just set up your modifications in an external driver and configured it as my main entity for the Shield. I'll let you know

@zehnm
Copy link
Contributor

zehnm commented Mar 29, 2024

I could reproduce two issues where communication breaks and no error is returned for several minutes from the communication library:

  1. Network interface changes of the Android TV device, e.g. from LAN to WiFi
  2. Client certificate gets invalid (shouldn't happen in normal use, only while developing and restoring an old certificate)

I'm a bit surprised about the first case, since I thought there's a keep-alive ping in the background. Maybe that changed, or I confuse it with the Android TV integration in Home Assistant. I'll investigate it after the multi-device PR is merged.

zehnm added a commit that referenced this issue Mar 29, 2024
AndroidTVRemote.send_key_command doesn't always raise ConnectionClosed

Relates to #28
@zehnm
Copy link
Contributor

zehnm commented Mar 29, 2024

One issue is in the androidtvremote2 library: sending a command is just fire and forget without a return value, even if the library determines that there's no connection :-(

        if not self.transport or self.transport.is_closing():
            LOGGER.debug("Connection is closed!")
            return

I've added a transport check workaround and will most likely submit a patch for the send_key_command method.

zehnm added a commit that referenced this issue Mar 29, 2024
This module logs everything in DEBUG level, even connection errors!
Enable logging to better analyze dead connections.

Relates to #28
@albaintor
Copy link
Contributor Author

The case when network interface changes (LAN to WIFI) should not occur in my setup (plugged in ethernet) but I guess that my ISP router may sometimes have dropouts. There is indeed no reconnection logic in the library which should be done on lower level inside the library.
Thank you for raising this and making a workaround

@zehnm zehnm self-assigned this Mar 30, 2024
@zehnm
Copy link
Contributor

zehnm commented Mar 30, 2024

I've started a new branch fix/28_lost-connection for refactoring and improving the connection logic. This should fix the dead connection issue and give immediate feedback, if a command couldn't be sent to the device because there's not transport available.
This requires more testing before I'll merge it into the main branch.
The main branch already contains debug logging of the androidtvremote2 library to better see what's going on (or not).

Changed IP addresses are still only handled after wakeup of the remote. Devices are disconnected before sleep, then reconnected after wakeup. IP change detection is included in the initial connection logic. Unfortunately the reconnection task of androidtvremote2 only has a callback handler for authentication errors and doesn't provide more events. It can only be stopped by calling disconnect().

@albaintor
Copy link
Contributor Author

Hi Markus,
ok I have just applied your branch on my external integration. Something that I would suggest that has disappeared from the previous PR I submitted : the profiles files are read from the $HOME folder, whereas they should read from the integration folder. On my setup the docker loads the integration files from an external mount, so that it is easier to update. This is not a common use I agree but as the profiles folder sits next to the intg-XXX driver files, one would expect to put them all in the same folder.
So I had applied a condition load on profile files from driver.py. What do you think ? (in that case if it fails to load from config_dir path it will check after the current path)

 # Internal driver mode
    profile_path = os.path.join(api.config_dir_path, "config/profiles")
    # External driver mode
    if not os.path.exists(profile_path):
        profile_path = "config/profiles"
    device_profile.load(profile_path)

@zehnm
Copy link
Contributor

zehnm commented Mar 31, 2024

This is already handled with ENV variables: https://github.com/unfoldedcircle/integration-python-library?tab=readme-ov-file#environment-variables and implemented in the Python integration library.
When running in a container you can simply set the UC_CONFIG_HOME or HOME environment variable to your mountpoint.
This is required for the correct runtime behaviour on the Remote Two.

The profile loading path is currently not correct and will fix it tomorrow:

profile_path = os.path.join(api.config_dir_path, "config/profiles")

it may not contain the config/ prefix to run correctly on the Remote Two.

@albaintor
Copy link
Contributor Author

Ok thanks it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants