Updated 22/9/19 - Android/Nvidia Shield - Application based Automation (watching netflix turns off lights)

Hey All,

I am very novice so excuse my coding or logic.

I wanted to dim my lights if I was watching netflix

Using this method I was able to control my lights based on the app on screen. So if Netflix is on screen the lights dim if netflix is not on screen they brighten.

Components

  • ADB Tools
  • Bash Script
  • Sensor that runs Bash script
  • Automation based on the sensor result

Requirements

  1. Android-tools-adb ( apt-get install android-tools-adb )
  2. Enable Developer Options on your Nvidia Shield
  3. enable network debugging
  4. To test ADB is working, on the HASS server run (shell command)
adb connect <Android_device_IP>:5555
  1. On the Shield you must authorise HASS to connect and allow future connections.
  2. To confirm that you are able to query the current app on screen run this from HASS terminal
adb shell dumpsys window windows | grep -E 'mFocusedApp'| cut -d / -f 1 | cut -d " " -f 7

IF you are able to connect via ADB over network and can query the on screen app then you should be able to create a sensor and automation in HASS

configuration.yaml

sensor:
  - platform: command_line
    name: Nvidia Application
    command: "/home/homeassistant/.homeassistant/app.sh"
    scan_interval: 10

app.sh

  • replace the shield:5555 with your own device hostname or IP
  • Add your own application names, use this command to find them
    (adb shell dumpsys window windows | grep -E ā€˜mFocusedAppā€™| cut -d / -f 1 | cut -d " " -f 7)
adb connect  shield:5555 > /dev/null 2>&1
app=$(adb shell dumpsys window windows | grep -E 'mFocusedApp'| cut -d / -f 1 | cut -d " " -f 7)


case $app in
    com.netflix.ninja | com.amazon.amazonvideo.livingroom | com.flymovie.tvguide ) echo "True";;
    *) echo "False";;
esac

automations.yaml

- id: '1569096107410'
  alias: NetFlixOn
  trigger:
  - entity_id: sensor.nvidia_application
    from: 'False'
    platform: state
    to: 'True'
  condition: []
  action:
  - service: light.turn_off
- id: '1569096302497'
  alias: NetflixOff
  trigger:
  - entity_id: sensor.nvidia_application
    from: 'True'
    platform: state
    to: 'False'
  condition: []
  action:
  - service: light.turn_on

8 Likes

This is cool. The easier way is to use Automagic on the shield and hit the hass api.

Thanks for the insight. Iā€™ll check it out, never heard of it before.

Iā€™d love to use your solution on my Nvidia shield too! Iā€™d like to look at your bash script, but the patebin link you provided no longer works. Is this solution still working for you? If so, would you be willing to reshare your bash script again?

Hello, I am not using it for a while but only due to data loss. Iā€™ll try to find my script but it was basically an adb over network command that returned the application on screen and when matched a string (app name) it then told home assist to do a function (canā€™t recall exactly). Maybe someone else has a copy and might see this or try archive.org.

I will let you know and if I donā€™t find it I will redo it from scratch and update you.

The script ran every second so as soon as an app I was looking for was open on screen home assist we do an action.

Sorry I donā€™t have more info right now.

Here is the current working version.

Pre-reqs

  • Android developer option to enable network debugging
  • working ADB connection to your Android device (trusted/approved)
  • app.sh script with correct permissions for execute for HASS user

Notes

  • This currently will turn off and on the lights based on the application on screen
  • It is basic but you can likely improve it in many ways
  • There is a 10 second delay set on the sensor

configuration.yaml

sensor:
  - platform: command_line
    name: Nvidia Application
    command: "/home/homeassistant/.homeassistant/app.sh"
    scan_interval: 10

app.sh

  • replace the shield:5555 with your own device hostname or IP
  • Add your own application names, use this command to find them
    (adb shell dumpsys window windows | grep -E ā€˜mFocusedAppā€™| cut -d / -f 1 | cut -d " " -f 7)
adb connect  shield:5555 > /dev/null 2>&1
app=$(adb shell dumpsys window windows | grep -E 'mFocusedApp'| cut -d / -f 1 | cut -d " " -f 7)


case $app in
    com.netflix.ninja | com.amazon.amazonvideo.livingroom | com.flymovie.tvguide ) echo "True";;
    *) echo "False";;
esac

automations.yaml

- id: '1569096107410'
  alias: NetFlixOn
  trigger:
  - entity_id: sensor.nvidia_application
    from: 'False'
    platform: state
    to: 'True'
  condition: []
  action:
  - service: light.turn_off
- id: '1569096302497'
  alias: NetflixOff
  trigger:
  - entity_id: sensor.nvidia_application
    from: 'True'
    platform: state
    to: 'False'
  condition: []
  action:
  - service: light.turn_on

2 Likes

So this is cool. So you can tell if a particular app on the Shield is running. How about the other way around so to speak? Any way to launch an app in a similar fashion? Trying to see if there is a way with Alexa or the Harmony Hub to launch things like YouTube directly without a bunch of commands to go to the home page, scroll down two clicks, to the right three clicks, click enter. Or whatever. Be nice to directly call apps on the device.

Also side question as I am still debating on getting a Shield, does that command tell you if itā€™s open in the foreground vs having multiple apps open at once? Not sure if they close when you switch to semething else or what.

That command does show what app is running on the screen, that is how I am able to dim lights when Netflix is on screen.

There may be a way to execute a command remotely on the shield but I havenā€™t looked into it. Have a Google for ā€œlaunching application using shell on androidā€ this looks promising https://stackoverflow.com/questions/4567904/how-to-start-an-application-using-android-adb-tools

You can see commands for both launching apps and detecting whatā€™s running in my code in this post.

I use it to provide buttons in ha which will start everything and go straight to an app on the shield, or changes app if everythingā€™s already on.

To get the same functionality on the harmony remote, youā€™d probably need to use something like emulated roku

You could then program a harmony button to launch an ha script.

1 Like