Here's a quick setup guide for controlling your Android TV from within Home Assistant. I've used it to control a genuine Android TV (Philips 7304) and an Odroid N2 running Android TV. For this to work you need ADB access. It can usually be enabled from within Developer Settings. The great part is - you don't need root access!
The most important things are described in the androidtv component for Home Assistant: https://www.home-assistant.io/integrations/androidtv/
Make sure you go through the adb setup.
My configuration is simple (inside configuration.yaml):
media_player:
- platform: androidtv
name: TV Bedroom ATV
host: 192.168.1.61
device_class: androidtv
Once Home Assistant restarts, your TV might require you to accept the connection (adb authentication). This happens only once (or until you reset your ATV to factory settings).
Once running the integration will show you the current ATV state (on or off) and allows you to turn it on or off.
Together with the history_stats sensor, it helps you keep an eye on tv viewing time:
sensor:
- platform: history_stats
name: TV Bedroom view time
entity_id: media_player.tv_bedroom_atv
state: 'on'
type: time
start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
end: '{{ now() }}'
So, yesterday the kids watched TV for just half an hour. Nice.
You can also create a virtual remote and inject keystrokes from within Home Assistant.
Let's define some scripts inside scripts.yaml:
'atv_bedroom_home':
alias: ATV Bedroom Home
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "HOME"
'atv_bedroom_up':
alias: ATV Bedroom Up
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "UP"
'atv_bedroom_down':
alias: ATV Bedroom Down
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "DOWN"
'atv_bedroom_left':
alias: ATV Bedroom Left
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "LEFT"
'atv_bedroom_right':
alias: ATV Bedroom Right
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "RIGHT"
'atv_bedroom_center':
alias: ATV Bedroom Center
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "CENTER"
'atv_bedroom_exit':
alias: ATV Bedroom Exit
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "EXIT"
'atv_bedroom_back':
alias: ATV Bedroom Back
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "BACK"
'atv_bedroom_menu':
alias: ATV Bedroom Menu
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "MENU"
'atv_bedroom_volup':
alias: ATV Bedroom Volume Up
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "VOLUME_UP"
'atv_bedroom_voldown':
alias: ATV Bedroom Volume Down
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "VOLUME_DOWN"
'atv_bedroom_power':
alias: ATV Bedroom Power
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "POWER"
'atv_bedroom_reboot':
alias: ATV Bedroom Reboot
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "reboot"
Once you restart Home Assistant you'll be able to add the scripts as buttons inside Lovelace UI. You can paste the config below as a custom card:
cards:
- cards:
- entity: media_player.tv_bedroom_atv
icon: 'mdi:power'
name: Power
tap_action:
action: call-service
service: media_player.toggle
service_data:
entity_id: media_player.tv_bedroom_atv
type: entity-button
- entity: script.atv_bedroom_power
icon: 'mdi:power'
name: ATV Power
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_power
type: entity-button
- entity: script.atv_bedroom_reboot
icon: 'mdi:power'
name: ATV Reboot
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_reboot
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_volup
icon: 'mdi:volume-plus'
name: Vol+
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_volup
type: entity-button
- entity: script.atv_bedroom_up
icon: 'mdi:arrow-up-bold'
name: Up
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_up
type: entity-button
- entity: media_player.tv_bedroom_atv
icon: 'mdi:video-input-hdmi'
name: Kodi
tap_action:
action: call-service
service: media_player.select_source
service_data:
entity_id: media_player.tv_bedroom_atv
source: org.xbmc.kodi
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_left
icon: 'mdi:arrow-left-bold'
name: Left
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_left
type: entity-button
- entity: script.atv_bedroom_center
icon: 'mdi:bullseye-arrow'
name: Enter
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_center
type: entity-button
- entity: script.atv_bedroom_right
icon: 'mdi:arrow-right-bold'
name: Right
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_right
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_voldown
icon: 'mdi:volume-minus'
name: Vol-
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_voldown
type: entity-button
- entity: script.atv_bedroom_down
icon: 'mdi:arrow-down-bold'
name: Down
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_down
type: entity-button
- entity: script.atv_bedroom_exit
icon: 'mdi:exit-to-app'
name: Exit
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_exit
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_back
icon: 'mdi:exit-to-app'
name: Back
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_back
type: entity-button
- entity: script.atv_bedroom_home
icon: 'mdi:home'
name: ATV Home
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_home
type: entity-button
- entity: script.atv_bedroom_menu
icon: 'mdi:menu'
name: ATV Menu
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_menu
type: entity-button
type: horizontal-stack
type: vertical-stack
Note that at one point I defined a button that starts Kodi (or brings it into foreground), and also I have a reboot button (that works without confirmation).
This is what you should get in Lovelace:
That should make navigation easier.
You can also use the scripts as actions in various automations.
Next step is to use adb to get a screenshot of what's currently on the screen and display it as a webcam in Home Assistant. This will require extending the androidtv component code. Also interesting would be to log what app is being used on screen (to better understand what the kids are doing when unsupervised) :)
The most important things are described in the androidtv component for Home Assistant: https://www.home-assistant.io/integrations/androidtv/
Make sure you go through the adb setup.
My configuration is simple (inside configuration.yaml):
media_player:
- platform: androidtv
name: TV Bedroom ATV
host: 192.168.1.61
device_class: androidtv
Once Home Assistant restarts, your TV might require you to accept the connection (adb authentication). This happens only once (or until you reset your ATV to factory settings).
Once running the integration will show you the current ATV state (on or off) and allows you to turn it on or off.
sensor:
- platform: history_stats
name: TV Bedroom view time
entity_id: media_player.tv_bedroom_atv
state: 'on'
type: time
start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
end: '{{ now() }}'
You can also create a virtual remote and inject keystrokes from within Home Assistant.
Let's define some scripts inside scripts.yaml:
'atv_bedroom_home':
alias: ATV Bedroom Home
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "HOME"
'atv_bedroom_up':
alias: ATV Bedroom Up
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "UP"
'atv_bedroom_down':
alias: ATV Bedroom Down
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "DOWN"
'atv_bedroom_left':
alias: ATV Bedroom Left
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "LEFT"
'atv_bedroom_right':
alias: ATV Bedroom Right
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "RIGHT"
'atv_bedroom_center':
alias: ATV Bedroom Center
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "CENTER"
'atv_bedroom_exit':
alias: ATV Bedroom Exit
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "EXIT"
'atv_bedroom_back':
alias: ATV Bedroom Back
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "BACK"
'atv_bedroom_menu':
alias: ATV Bedroom Menu
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "MENU"
'atv_bedroom_volup':
alias: ATV Bedroom Volume Up
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "VOLUME_UP"
'atv_bedroom_voldown':
alias: ATV Bedroom Volume Down
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "VOLUME_DOWN"
'atv_bedroom_power':
alias: ATV Bedroom Power
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "POWER"
'atv_bedroom_reboot':
alias: ATV Bedroom Reboot
sequence:
- service: androidtv.adb_command
data:
entity_id: media_player.tv_bedroom_atv
command: "reboot"
Once you restart Home Assistant you'll be able to add the scripts as buttons inside Lovelace UI. You can paste the config below as a custom card:
cards:
- cards:
- entity: media_player.tv_bedroom_atv
icon: 'mdi:power'
name: Power
tap_action:
action: call-service
service: media_player.toggle
service_data:
entity_id: media_player.tv_bedroom_atv
type: entity-button
- entity: script.atv_bedroom_power
icon: 'mdi:power'
name: ATV Power
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_power
type: entity-button
- entity: script.atv_bedroom_reboot
icon: 'mdi:power'
name: ATV Reboot
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_reboot
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_volup
icon: 'mdi:volume-plus'
name: Vol+
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_volup
type: entity-button
- entity: script.atv_bedroom_up
icon: 'mdi:arrow-up-bold'
name: Up
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_up
type: entity-button
- entity: media_player.tv_bedroom_atv
icon: 'mdi:video-input-hdmi'
name: Kodi
tap_action:
action: call-service
service: media_player.select_source
service_data:
entity_id: media_player.tv_bedroom_atv
source: org.xbmc.kodi
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_left
icon: 'mdi:arrow-left-bold'
name: Left
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_left
type: entity-button
- entity: script.atv_bedroom_center
icon: 'mdi:bullseye-arrow'
name: Enter
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_center
type: entity-button
- entity: script.atv_bedroom_right
icon: 'mdi:arrow-right-bold'
name: Right
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_right
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_voldown
icon: 'mdi:volume-minus'
name: Vol-
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_voldown
type: entity-button
- entity: script.atv_bedroom_down
icon: 'mdi:arrow-down-bold'
name: Down
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_down
type: entity-button
- entity: script.atv_bedroom_exit
icon: 'mdi:exit-to-app'
name: Exit
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_exit
type: entity-button
type: horizontal-stack
- cards:
- entity: script.atv_bedroom_back
icon: 'mdi:exit-to-app'
name: Back
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_back
type: entity-button
- entity: script.atv_bedroom_home
icon: 'mdi:home'
name: ATV Home
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_home
type: entity-button
- entity: script.atv_bedroom_menu
icon: 'mdi:menu'
name: ATV Menu
tap_action:
action: call-service
service: script.turn_on
service_data:
entity_id: script.atv_bedroom_menu
type: entity-button
type: horizontal-stack
type: vertical-stack
Note that at one point I defined a button that starts Kodi (or brings it into foreground), and also I have a reboot button (that works without confirmation).
This is what you should get in Lovelace:
That should make navigation easier.
You can also use the scripts as actions in various automations.
Next step is to use adb to get a screenshot of what's currently on the screen and display it as a webcam in Home Assistant. This will require extending the androidtv component code. Also interesting would be to log what app is being used on screen (to better understand what the kids are doing when unsupervised) :)
Comments
https://community.home-assistant.io/t/androidtv-adb-command-slow-delay-when-sending-command/214214