Skip to content

Commit

Permalink
Merge 0864439 into 41983c6
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jan 16, 2021
2 parents 41983c6 + 0864439 commit f8c720c
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 26 deletions.
3 changes: 3 additions & 0 deletions androidtv/__init__.py
Expand Up @@ -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
22 changes: 17 additions & 5 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,13 @@ 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.
"""

# ======================================================================= #
# #
# ADB methods #
Expand Down Expand Up @@ -152,10 +166,6 @@ def _parse_device_properties(self, properties):

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 @@ -179,6 +189,8 @@ def _parse_device_properties(self, properties):
'wifimac': wifimac,
'ethmac': ethmac}

self._fill_in_commands()

# ======================================================================= #
# #
# Custom state detection #
Expand Down
4 changes: 2 additions & 2 deletions androidtv/basetv/basetv_async.py
Expand Up @@ -251,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 Down Expand Up @@ -428,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
4 changes: 2 additions & 2 deletions androidtv/basetv/basetv_sync.py
Expand Up @@ -251,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 Down Expand Up @@ -428,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
3 changes: 3 additions & 0 deletions androidtv/setup_async.py
Expand Up @@ -75,4 +75,7 @@ async def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=50
else:
aftv.__class__ = AndroidTVAsync

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

return aftv
4 changes: 3 additions & 1 deletion tests/test_basetv_sync.py
Expand Up @@ -10,6 +10,7 @@
sys.path.insert(0, '..')

from androidtv import constants, ha_state_detection_rules_validator
from androidtv.androidtv.androidtv_sync import AndroidTVSync
from androidtv.basetv.basetv_sync import BaseTVSync
from . import patchers

Expand Down Expand Up @@ -343,8 +344,9 @@ def test_get_device_properties(self):
self.assertDictEqual({}, device_properties)

with patchers.patch_shell(DEVICE_PROPERTIES_GOOGLE_TV)[self.PATCH_KEY]:
self.btv.__class__ = AndroidTVSync
device_properties = self.btv.get_device_properties()
self.assertTrue(self.btv._is_google_tv)
self.assertEqual(self.btv.device_properties["manufacturer"], "Google")

def test_get_installed_apps(self):
""""Check that `get_installed_apps` works correctly.
Expand Down

0 comments on commit f8c720c

Please sign in to comment.