Allow for custom turn on/off commands (#22354)

This commit is contained in:
Jeff Irion 2019-03-25 05:37:10 -07:00 committed by Charles Garwood
parent 17a96c6d9b
commit c8048e1aff

View File

@ -40,6 +40,8 @@ CONF_ADB_SERVER_IP = 'adb_server_ip'
CONF_ADB_SERVER_PORT = 'adb_server_port' CONF_ADB_SERVER_PORT = 'adb_server_port'
CONF_APPS = 'apps' CONF_APPS = 'apps'
CONF_GET_SOURCES = 'get_sources' CONF_GET_SOURCES = 'get_sources'
CONF_TURN_ON_COMMAND = 'turn_on_command'
CONF_TURN_OFF_COMMAND = 'turn_off_command'
DEFAULT_NAME = 'Android TV' DEFAULT_NAME = 'Android TV'
DEFAULT_PORT = 5555 DEFAULT_PORT = 5555
@ -79,7 +81,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
cv.port, cv.port,
vol.Optional(CONF_GET_SOURCES, default=DEFAULT_GET_SOURCES): cv.boolean, vol.Optional(CONF_GET_SOURCES, default=DEFAULT_GET_SOURCES): cv.boolean,
vol.Optional(CONF_APPS, default=dict()): vol.Optional(CONF_APPS, default=dict()):
vol.Schema({cv.string: cv.string}) vol.Schema({cv.string: cv.string}),
vol.Optional(CONF_TURN_ON_COMMAND): cv.string,
vol.Optional(CONF_TURN_OFF_COMMAND): cv.string
}) })
# Translate from `AndroidTV` / `FireTV` reported state to HA state. # Translate from `AndroidTV` / `FireTV` reported state to HA state.
@ -136,12 +140,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
else: else:
if aftv.DEVICE_CLASS == DEVICE_ANDROIDTV: if aftv.DEVICE_CLASS == DEVICE_ANDROIDTV:
device = AndroidTVDevice(aftv, config[CONF_NAME], device = AndroidTVDevice(aftv, config[CONF_NAME],
config[CONF_APPS]) config[CONF_APPS],
config.get(CONF_TURN_ON_COMMAND),
config.get(CONF_TURN_OFF_COMMAND))
device_name = config[CONF_NAME] if CONF_NAME in config \ device_name = config[CONF_NAME] if CONF_NAME in config \
else 'Android TV' else 'Android TV'
else: else:
device = FireTVDevice(aftv, config[CONF_NAME], config[CONF_APPS], device = FireTVDevice(aftv, config[CONF_NAME], config[CONF_APPS],
config[CONF_GET_SOURCES]) config[CONF_GET_SOURCES],
config.get(CONF_TURN_ON_COMMAND),
config.get(CONF_TURN_OFF_COMMAND))
device_name = config[CONF_NAME] if CONF_NAME in config \ device_name = config[CONF_NAME] if CONF_NAME in config \
else 'Fire TV' else 'Fire TV'
@ -199,7 +207,8 @@ def adb_decorator(override_available=False):
class ADBDevice(MediaPlayerDevice): class ADBDevice(MediaPlayerDevice):
"""Representation of an Android TV or Fire TV device.""" """Representation of an Android TV or Fire TV device."""
def __init__(self, aftv, name, apps): def __init__(self, aftv, name, apps, turn_on_command,
turn_off_command):
"""Initialize the Android TV / Fire TV device.""" """Initialize the Android TV / Fire TV device."""
from androidtv.constants import APPS, KEYS from androidtv.constants import APPS, KEYS
@ -209,6 +218,9 @@ class ADBDevice(MediaPlayerDevice):
self._apps.update(apps) self._apps.update(apps)
self._keys = KEYS self._keys = KEYS
self.turn_on_command = turn_on_command
self.turn_off_command = turn_off_command
# ADB exceptions to catch # ADB exceptions to catch
if not self.aftv.adb_server_ip: if not self.aftv.adb_server_ip:
# Using "python-adb" (Python ADB implementation) # Using "python-adb" (Python ADB implementation)
@ -278,12 +290,18 @@ class ADBDevice(MediaPlayerDevice):
@adb_decorator() @adb_decorator()
def turn_on(self): def turn_on(self):
"""Turn on the device.""" """Turn on the device."""
self.aftv.turn_on() if self.turn_on_command:
self.aftv.adb_shell(self.turn_on_command)
else:
self.aftv.turn_on()
@adb_decorator() @adb_decorator()
def turn_off(self): def turn_off(self):
"""Turn off the device.""" """Turn off the device."""
self.aftv.turn_off() if self.turn_off_command:
self.aftv.adb_shell(self.turn_off_command)
else:
self.aftv.turn_off()
@adb_decorator() @adb_decorator()
def media_previous_track(self): def media_previous_track(self):
@ -311,9 +329,11 @@ class ADBDevice(MediaPlayerDevice):
class AndroidTVDevice(ADBDevice): class AndroidTVDevice(ADBDevice):
"""Representation of an Android TV device.""" """Representation of an Android TV device."""
def __init__(self, aftv, name, apps): def __init__(self, aftv, name, apps, turn_on_command,
turn_off_command):
"""Initialize the Android TV device.""" """Initialize the Android TV device."""
super().__init__(aftv, name, apps) super().__init__(aftv, name, apps, turn_on_command,
turn_off_command)
self._device = None self._device = None
self._muted = None self._muted = None
@ -392,9 +412,11 @@ class AndroidTVDevice(ADBDevice):
class FireTVDevice(ADBDevice): class FireTVDevice(ADBDevice):
"""Representation of a Fire TV device.""" """Representation of a Fire TV device."""
def __init__(self, aftv, name, apps, get_sources): def __init__(self, aftv, name, apps, get_sources,
turn_on_command, turn_off_command):
"""Initialize the Fire TV device.""" """Initialize the Fire TV device."""
super().__init__(aftv, name, apps) super().__init__(aftv, name, apps, turn_on_command,
turn_off_command)
self._get_sources = get_sources self._get_sources = get_sources
self._running_apps = None self._running_apps = None