Skip to content

Commit

Permalink
Merge 4e6616d into 88dab23
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jan 16, 2021
2 parents 88dab23 + 4e6616d commit 1fcd50e
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 51 deletions.
13 changes: 8 additions & 5 deletions androidtv/__init__.py
Expand Up @@ -46,15 +46,15 @@ def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, st
if device_class == 'androidtv':
atv = AndroidTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
atv.adb_connect(auth_timeout_s=auth_timeout_s)
atv.device_properties = atv.get_device_properties()
atv.installed_apps = atv.get_installed_apps()
atv.get_device_properties()
atv.get_installed_apps()
return atv

if device_class == 'firetv':
ftv = FireTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
ftv.adb_connect(auth_timeout_s=auth_timeout_s)
ftv.device_properties = ftv.get_device_properties()
ftv.installed_apps = ftv.get_installed_apps()
ftv.get_device_properties()
ftv.get_installed_apps()
return ftv

if device_class != 'auto':
Expand All @@ -69,7 +69,7 @@ def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, st
aftv.device_properties = aftv.get_device_properties()

# get the installed apps
aftv.installed_apps = aftv.get_installed_apps()
aftv.get_installed_apps()

# Fire TV
if aftv.device_properties.get('manufacturer') == 'Amazon':
Expand All @@ -79,6 +79,9 @@ def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, st
else:
aftv.__class__ = AndroidTVSync

# Fill in commands that are specific to the device
aftv._fill_in_commands() # pylint: disable=protected-access

return aftv


Expand Down
11 changes: 7 additions & 4 deletions androidtv/androidtv/androidtv_async.py
Expand Up @@ -38,6 +38,9 @@ class AndroidTVAsync(BaseTVAsync, BaseAndroidTV):
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVAsync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# fill in commands that can vary based on the device
BaseAndroidTV._fill_in_commands(self)

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down Expand Up @@ -124,14 +127,14 @@ async def get_properties(self, get_running_apps=True, lazy=False):
"""
if lazy:
if get_running_apps:
output = await self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_lazy_running_apps)
else:
output = await self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY_NO_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_NO_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_lazy_no_running_apps)
else:
if get_running_apps:
output = await self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_not_lazy_running_apps)
else:
output = await self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_not_lazy_no_running_apps)
_LOGGER.debug("Android TV %s:%d `get_properties` response: %s", self.host, self.port, output)

return self._get_properties(output, get_running_apps)
Expand Down
11 changes: 7 additions & 4 deletions androidtv/androidtv/androidtv_sync.py
Expand Up @@ -38,6 +38,9 @@ class AndroidTVSync(BaseTVSync, BaseAndroidTV):
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVSync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# fill in commands that can vary based on the device
BaseAndroidTV._fill_in_commands(self)

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down Expand Up @@ -126,14 +129,14 @@ def get_properties(self, get_running_apps=True, lazy=False):
"""
if lazy:
if get_running_apps:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_lazy_running_apps)
else:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY_NO_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_NO_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_lazy_no_running_apps)
else:
if get_running_apps:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_not_lazy_running_apps)
else:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_not_lazy_no_running_apps)
_LOGGER.debug("Android TV %s:%d `get_properties` response: %s", self.host, self.port, output)

return self._get_properties(output, get_running_apps)
Expand Down
19 changes: 19 additions & 0 deletions androidtv/androidtv/base_androidtv.py
Expand Up @@ -37,6 +37,25 @@ class BaseAndroidTV(BaseTV): # pylint: disable=too-few-public-methods
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None):
BaseTV.__init__(self, None, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)

def _fill_in_commands(self):
"""Fill in commands that are specific to Android TV devices."""
# Is this a Google Chromecast Android TV?
if "Google" in self.device_properties.get("manufacturer", "") and "Chromecast" in self.device_properties.get("model", ""):
self._cmd_get_properties_lazy_running_apps = constants.CMD_ANDROIDTV_PROPERTIES_LAZY_RUNNING_APPS
self._cmd_get_properties_lazy_no_running_apps = constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_NO_RUNNING_APPS
self._cmd_get_properties_not_lazy_running_apps = constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_RUNNING_APPS
self._cmd_get_properties_not_lazy_no_running_apps = constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS
self._cmd_current_app = constants.CMD_CURRENT_APP_GOOGLE_TV
self._cmd_launch_app = constants.CMD_LAUNCH_APP_GOOGLE_TV
return

self._cmd_get_properties_lazy_running_apps = constants.CMD_ANDROIDTV_PROPERTIES_LAZY_RUNNING_APPS
self._cmd_get_properties_lazy_no_running_apps = constants.CMD_ANDROIDTV_PROPERTIES_LAZY_NO_RUNNING_APPS
self._cmd_get_properties_not_lazy_running_apps = constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_RUNNING_APPS
self._cmd_get_properties_not_lazy_no_running_apps = constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS
self._cmd_current_app = constants.CMD_CURRENT_APP
self._cmd_launch_app = constants.CMD_LAUNCH_APP

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down
43 changes: 29 additions & 14 deletions androidtv/basetv/basetv.py
Expand Up @@ -76,7 +76,14 @@ def __init__(self, adb, host, port=5555, adbkey='', adb_server_ip='', adb_server
self._state_detection_rules = state_detection_rules
self.device_properties = {}
self.installed_apps = []
self._is_google_tv = False

# commands that can vary based on the device
self._cmd_get_properties_lazy_running_apps = ""
self._cmd_get_properties_lazy_no_running_apps = ""
self._cmd_get_properties_not_lazy_running_apps = ""
self._cmd_get_properties_not_lazy_no_running_apps = ""
self._cmd_current_app = ""
self._cmd_launch_app = ""

# make sure the rules are valid
if self._state_detection_rules:
Expand All @@ -88,6 +95,14 @@ def __init__(self, adb, host, port=5555, adbkey='', adb_server_ip='', adb_server
# the max volume level (determined when first getting the volume level)
self.max_volume = None

def _fill_in_commands(self):
"""Fill in commands that are specific to the device.
This is implemented in the `BaseAndroidTV` and `BaseFireTV` classes.
"""
# pass

# ======================================================================= #
# #
# ADB methods #
Expand Down Expand Up @@ -144,18 +159,16 @@ def _parse_device_properties(self, properties):
_LOGGER.debug("%s:%d `get_device_properties` response: %s", self.host, self.port, properties)

if not properties:
return {}
self.device_properties = {}
return

lines = properties.strip().splitlines()
if len(lines) != 6:
return {}
self.device_properties = {}
return

manufacturer, model, serialno, version, mac_wlan0_output, mac_eth0_output = lines

# Is this a Google Chromecast Android TV?
if "Google" in manufacturer and "Chromecast" in model:
self._is_google_tv = True

if not serialno.strip():
_LOGGER.warning("Could not obtain serialno for %s:%d, got: '%s'", self.host, self.port, serialno)
serialno = None
Expand All @@ -172,12 +185,14 @@ def _parse_device_properties(self, properties):
else:
ethmac = None

return {'manufacturer': manufacturer,
'model': model,
'serialno': serialno,
'sw_version': version,
'wifimac': wifimac,
'ethmac': ethmac}
self.device_properties = {'manufacturer': manufacturer,
'model': model,
'serialno': serialno,
'sw_version': version,
'wifimac': wifimac,
'ethmac': ethmac}

self._fill_in_commands()

# ======================================================================= #
# #
Expand Down Expand Up @@ -395,7 +410,7 @@ def _get_hdmi_input(hdmi_response):
return hdmi_response.strip() if hdmi_response and hdmi_response.strip() else None

@staticmethod
def _installed_apps(installed_apps_response):
def _get_installed_apps(installed_apps_response):
"""Get the installed apps from the output of :py:const:`androidtv.constants.CMD_INSTALLED_APPS`.
Parameters
Expand Down
10 changes: 6 additions & 4 deletions androidtv/basetv/basetv_async.py
Expand Up @@ -198,7 +198,8 @@ async def get_device_properties(self):
constants.CMD_MAC_WLAN0 + " && " +
constants.CMD_MAC_ETH0)

return self._parse_device_properties(properties)
self._parse_device_properties(properties)
return self.device_properties

# ======================================================================= #
# #
Expand Down Expand Up @@ -250,7 +251,7 @@ async def current_app(self):
The ID of the current app, or ``None`` if it could not be determined
"""
current_app_response = await self._adb.shell(constants.CMD_CURRENT_APP)
current_app_response = await self._adb.shell(self._cmd_current_app)

return self._current_app(current_app_response)

Expand All @@ -275,7 +276,8 @@ async def get_installed_apps(self):
"""
installed_apps_response = await self._adb.shell(constants.CMD_INSTALLED_APPS)
return self._installed_apps(installed_apps_response)
self.installed_apps = self._get_installed_apps(installed_apps_response)
return self.installed_apps

async def is_volume_muted(self):
"""Whether or not the volume is muted.
Expand Down Expand Up @@ -426,7 +428,7 @@ async def launch_app(self, app):
The ID of the app that will be launched
"""
await self._adb.shell(constants.CMD_LAUNCH_APP.format(app) if not self._is_google_tv else constants.CMD_LAUNCH_APP_GOOGLE_TV.format(app))
await self._adb.shell(self._cmd_launch_app.format(app))

async def stop_app(self, app):
"""Stop an app.
Expand Down
10 changes: 6 additions & 4 deletions androidtv/basetv/basetv_sync.py
Expand Up @@ -198,7 +198,8 @@ def get_device_properties(self):
constants.CMD_MAC_WLAN0 + " && " +
constants.CMD_MAC_ETH0)

return self._parse_device_properties(properties)
self._parse_device_properties(properties)
return self.device_properties

# ======================================================================= #
# #
Expand Down Expand Up @@ -250,7 +251,7 @@ def current_app(self):
The ID of the current app, or ``None`` if it could not be determined
"""
current_app_response = self._adb.shell(constants.CMD_CURRENT_APP)
current_app_response = self._adb.shell(self._cmd_current_app)

return self._current_app(current_app_response)

Expand All @@ -275,7 +276,8 @@ def get_installed_apps(self):
"""
installed_apps_response = self._adb.shell(constants.CMD_INSTALLED_APPS)
return self._installed_apps(installed_apps_response)
self.installed_apps = self._get_installed_apps(installed_apps_response)
return self.installed_apps

def is_volume_muted(self):
"""Whether or not the volume is muted.
Expand Down Expand Up @@ -426,7 +428,7 @@ def launch_app(self, app):
The ID of the app that will be launched
"""
self._adb.shell(constants.CMD_LAUNCH_APP.format(app) if not self._is_google_tv else constants.CMD_LAUNCH_APP_GOOGLE_TV.format(app))
self._adb.shell(self._cmd_launch_app.format(app))

def stop_app(self, app):
"""Stop an app.
Expand Down
9 changes: 9 additions & 0 deletions androidtv/firetv/base_firetv.py
Expand Up @@ -37,6 +37,15 @@ class BaseFireTV(BaseTV): # pylint: disable=too-few-public-methods
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None):
BaseTV.__init__(self, None, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)

def _fill_in_commands(self):
"""Fill in commands that are specific to Fire TV devices."""
self._cmd_get_properties_lazy_running_apps = constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS
self._cmd_get_properties_lazy_no_running_apps = constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS
self._cmd_get_properties_not_lazy_running_apps = constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS
self._cmd_get_properties_not_lazy_no_running_apps = constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS
self._cmd_current_app = constants.CMD_CURRENT_APP
self._cmd_launch_app = constants.CMD_LAUNCH_APP

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down
11 changes: 7 additions & 4 deletions androidtv/firetv/firetv_async.py
Expand Up @@ -38,6 +38,9 @@ class FireTVAsync(BaseTVAsync, BaseFireTV):
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVAsync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# fill in commands that can vary based on the device
BaseFireTV._fill_in_commands(self)

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down Expand Up @@ -112,14 +115,14 @@ async def get_properties(self, get_running_apps=True, lazy=False):
"""
if lazy:
if get_running_apps:
output = await self._adb.shell(constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_lazy_running_apps)
else:
output = await self._adb.shell(constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_lazy_no_running_apps)
else:
if get_running_apps:
output = await self._adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_not_lazy_running_apps)
else:
output = await self._adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
output = await self._adb.shell(self._cmd_get_properties_not_lazy_no_running_apps)
_LOGGER.debug("Fire TV %s:%d `get_properties` response: %s", self.host, self.port, output)

return self._get_properties(output, get_running_apps)
Expand Down
11 changes: 7 additions & 4 deletions androidtv/firetv/firetv_sync.py
Expand Up @@ -38,6 +38,9 @@ class FireTVSync(BaseTVSync, BaseFireTV):
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVSync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# fill in commands that can vary based on the device
BaseFireTV._fill_in_commands(self)

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down Expand Up @@ -112,14 +115,14 @@ def get_properties(self, get_running_apps=True, lazy=False):
"""
if lazy:
if get_running_apps:
output = self._adb.shell(constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_lazy_running_apps)
else:
output = self._adb.shell(constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_lazy_no_running_apps)
else:
if get_running_apps:
output = self._adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_not_lazy_running_apps)
else:
output = self._adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
output = self._adb.shell(self._cmd_get_properties_not_lazy_no_running_apps)
_LOGGER.debug("Fire TV %s:%d `get_properties` response: %s", self.host, self.port, output)

return self._get_properties(output, get_running_apps)
Expand Down

0 comments on commit 1fcd50e

Please sign in to comment.