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

Fire TV Select source doesn't work right after "POWER" ADB command #194

Closed
square99 opened this issue Aug 7, 2020 · 12 comments · Fixed by #195
Closed

Fire TV Select source doesn't work right after "POWER" ADB command #194

square99 opened this issue Aug 7, 2020 · 12 comments · Fixed by #195

Comments

@square99
Copy link

square99 commented Aug 7, 2020

I am trying to make some scripts on HA 0.114.0.dev0,
so, I'm using androidtv[async] 0.0.48 module.

The script didn't work as I wanted, so I looked into it.
And I guess there are some bugs on this project.

media_player.select_source service never work right after POWER adb command,
and the log is,
2020-08-07 23:32:53 INFO (MainThread) [homeassistant.components.androidtv.media_player] ADB command not executed because the connection is currently in use

In this situation, push any button on remote controller, after then select_source works again.

I think there will be a bug in the adb_manager_async.py related to _adb_lock, so please look for it.

@square99
Copy link
Author

square99 commented Aug 7, 2020

It means wake up with POWER command when the fire TV is in idle state.

@JeffLIrion
Copy link
Owner

Are you using an ADB server or the Python ADB implementation?

Does the media player in HA become unavailable when this happens?

In this situation, push any button on remote controller, after then select_source works again.

When you say "remote controller" I assume you're talking about a physical remote controller, not a remote that you created in Lovelace. What if instead of hitting a button on the remote, you just wait a few seconds? Does it work then?

I bet you have the screencap option enabled. In some testing I did, retrieving the screencap can take a second or two. The timeout for ADB commands is 3 seconds, so if you send the power command, then HA tries to take a screencap and it takes 3+ seconds, your select source command will not get executed and you'll get that log message.

@square99
Copy link
Author

square99 commented Aug 7, 2020

Are you using an ADB server or the Python ADB implementation?

Actually, I tested both ADB server and python ADB implementation both, and the results are same.

Does the media player in HA become unavailable when this happens?

No, It's not unavailable, I can see status of media_player

Even if I don't use Media control Card at all, the situation is the same.
After push a physical button on remote controller, It's starting to work.
If I don't push a button, It does not work until it goes to IDLE again.

Yeah, the log is a little bit confusing.
I checked again, There isn't any log when select_source doesn't work.
It didn't work, so I pressed it several times so the log has occurerd.

@JeffLIrion
Copy link
Owner

Depending on the device and the app you're trying to open, select_source may not work. There's an existing issue for it, but I don't know that a universal fix is possible. The alternative is to find the right ADB shell command to launch the app. See this comment: #40 (comment)

And keep in mind that if you bombard the device with commands that take some time, eventually a command won't get run within 3 seconds from the time that you tried to send it and you'll get that message in the log.

Because getting a screenshot takes some time, I recommend disabling this option if you want to ensure that your ADB commands get executed quickly.

Also, if you send "POWER" via androidtv.adb_command it will send a keyevent command. If you use media_player.turn_on it will send a command that checks if the device is on and, if it is not, sends a command to turn it on. But you can customize this via the turn_on_command configuration parameter.

@square99
Copy link
Author

square99 commented Aug 7, 2020

Um.. I think It is fire TV's Problem. I tested with ADB server Directly,
If fire TV turned on by ADB and without remote controller, It does not execute shell command 'monkey'.

Right after turn on:
mantis:/ $ monkey -p com.netflix.ninja -c android.intent.category.LAUNCHER 1
** SYS_KEYS has no physical keys but with factor 2.0%.

after push some physical button:
255|mantis:/ $ monkey -p com.netflix.ninja -c android.intent.category.LAUNCHER 1
Events injected: 1
## Network stats: elapsed time=29ms (0ms mobile, 0ms wifi, 29ms not connected)

Do you have any idea?

@square99
Copy link
Author

square99 commented Aug 7, 2020

I found solution from googling :)
can you add this option? --pct-syskeys 0

255|mantis:/ $ monkey -p com.netflix.ninja -c android.intent.category.LAUNCHER --pct-syskeys 0 1
Events injected: 1
## Network stats: elapsed time=115ms (0ms mobile, 0ms wifi, 115ms not connected)

@JeffLIrion JeffLIrion linked a pull request Aug 8, 2020 that will close this issue
@JeffLIrion
Copy link
Owner

Here’s the change: #195

But I haven't tested it to see if it works.

@JeffLIrion
Copy link
Owner

So this was the only issue, the current command used to launch an app wasn't working correctly?

@square99
Copy link
Author

square99 commented Aug 8, 2020

Yeah, like I said, if you don't press any button on remote controller, it doesn't work without being paired.

@JeffLIrion
Copy link
Owner

I want to make sure that this change won't break any existing functionality before merging it. I tested that modified command on my Fire TV stick and it successfully launched Netflix. Then again, the current command works fine, also.

FYI, if you use the following command via the androidtv.adb_command service it should

  • Turn on your device if it's currently off
  • Launch Netflix if it's not already open
(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') || input keyevent 26; CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && if [ $CURRENT_APP != 'com.netflix.ninja' ]; then monkey -p com.netflix.ninja -c android.intent.category.LAUNCHER --pct-syskeys 0 1; fi

@square99
Copy link
Author

So when do you plan to merge? I look forward to using it in the next version.

@JeffLIrion
Copy link
Owner

I merged it in, bumped the version, and updated the version in Home Assistant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants