Access to all Android settings/properties from Home Assistant

We are many to use tablet or Android devices for our Home Assistant.
Thanks to its development console, Android allows us to access and modify the parameters (hardware and software). Here’s how to access to them and take control of your Android device:

  1. ADB over TCP
    The first step is to active the Android Debug Bridge (ADB) over TCP
    See Enabling developer mode on your device from this page GitHub - hassio-addons/addon-adb: Android Debug Bridge - Home Assistant Community Add-ons.
    If the Network debugging is not available on your device, follow tutorials on internet or connect your device to your computer and with the terminal set this command:

    adb tcpip 5555

  2. Install ADB server (Optional)
    Follow instructions here: GitHub - hassio-addons/addon-adb: Android Debug Bridge - Home Assistant Community Add-ons

  3. Configure Home Assistant
    Follow instructions here: Android Debug Bridge - Home Assistant
    Then, restart and a new media_player should appear

  4. Tests and use
    From the androidtv.adb_command service, we can try the different commands. GET commands will display properties in device’s state.
    I give some example here that can be use in automations and scripts:

    • Get screen brightness

      entity_id: media_player.xxx
      command: settings get system screen_brightness

    • Set screen brightness

      entity_id: media_player.xxx
      command: settings put system screen_brightness {value}

      The screen backlight brightness between 0 and 255

    • Get screen brightness mode

      entity_id: media_player.xxx
      command: settings get system screen_brightness_mode

    • Set screen brightness

      entity_id: media_player.xxx
      command: settings put system screen_brightness_mode

      Supported values are 0 for manual and 1 for automatic

    • Get stay on while plugged in

      entity_id: media_player.xxx
      command: global get system stay_on_while_plugged_in

    • Set stay on while plugged in

      entity_id: media_player.xxx
      command: global put system stay_on_while_plugged_in {value}

      Supported values are:

      • 0 to never stay on while plugged in
      • BatteryManager#BATTERY_PLUGGED_AC to stay on for AC charger
      • BatteryManager#BATTERY_PLUGGED_USB to stay on for USB charger
      • BatteryManager#BATTERY_PLUGGED_WIRELESS to stay on for wireless charger
    • Turn off the screen by simulating a press on the Power Button

      entity_id: media_player.xxx
      command: input keyevent 26

    • Start and kill an app (example with Home Assistant Companion for Android)

      entity_id: media_player.xxx
      command: am force-stop io.homeassistant.companion.android

      entity_id: media_player.xxx
      command: monkey -p io.homeassistant.companion.android 1

    • Turn off the screen in case of long press on the Hue remote:

        alias: Screens off after long press
        trigger:
        - entity_id: remote.remote
          platform: state
          to: 4_hold_up
        condition:
        - condition: template
          value_template: '{{ states("media_player.xxx") != "off" }}'
          # check current screen status
        action:
        - data:
            command: input keyevent 26
            entity_id: media_player.xxx
          service: androidtv.adb_command
      

All the ADB commands are accessible from developers Android website (Settings.Global  |  Android Developers).
ADB over TCP offers unlimited possibilities (and for free). You can modify and read all hardware and software properties, and also simulate pressing buttons.

10 Likes

I always noticed the ADB integration, but for some reason thought it was limited to Android TV devices (being a media_player didn’t help that).

This is great info, thank you!

1 Like