From daddefdb39907d3b7b48a7b64a2b89b9897675ec Mon Sep 17 00:00:00 2001 From: Nortonko Date: Mon, 9 Oct 2023 19:55:04 +0200 Subject: [PATCH 1/9] add support for androidtv 12 & 13 --- androidtv/basetv/basetv.py | 63 ++++++++++++++++++++++++++++++++ androidtv/basetv/basetv_async.py | 2 +- androidtv/constants.py | 35 ++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/androidtv/basetv/basetv.py b/androidtv/basetv/basetv.py index 0f895890..a7a9ed73 100644 --- a/androidtv/basetv/basetv.py +++ b/androidtv/basetv/basetv.py @@ -131,6 +131,14 @@ def _cmd_audio_state(self): # Is this an Android 11 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_AUDIO_STATE11 + + # Is this an Android 12 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + return constants.CMD_AUDIO_STATE11 + + # Is this an Android 13 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + return constants.CMD_AUDIO_STATE11 return constants.CMD_AUDIO_STATE def _cmd_current_app(self): @@ -157,6 +165,14 @@ def _cmd_current_app(self): if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_CURRENT_APP11 + # Is this an Android 12 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + return constants.CMD_CURRENT_APP12 + + # Is this an Android 13 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + return constants.CMD_CURRENT_APP13 + return constants.CMD_CURRENT_APP def _cmd_current_app_media_session_state(self): @@ -183,6 +199,14 @@ def _cmd_current_app_media_session_state(self): if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11 + # Is this an Android 11 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE12 + + # Is this an Android 13 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE13 + return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE def _cmd_hdmi_input(self): @@ -200,9 +224,40 @@ def _cmd_hdmi_input(self): # Is this an Android 11 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_HDMI_INPUT11 + + # Is this an Android 12 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + return constants.CMD_HDMI_INPUT11 + + # Is this an Android 13 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + return constants.CMD_HDMI_INPUT11 return constants.CMD_HDMI_INPUT + def _cmd_volume_set(self): + """Get the command used to set volume for this device. + + Returns + ------- + str + The device-specific ADB shell command used to set volume + + """ + # Is this an Android 11 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": + return constants.CMD_VOLUME_SET_COMMAND12 + + # Is this an Android 12 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + return constants.CMD_VOLUME_SET_COMMAND12 + + # Is this an Android 13 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + return constants.CMD_VOLUME_SET_COMMAND13 + + return constants.CMD_VOLUME_SET_COMMAND + def _cmd_launch_app(self, app): """Get the command to launch the specified app for this device. @@ -234,6 +289,14 @@ def _cmd_launch_app(self, app): # Is this an Android 11 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_LAUNCH_APP11.format(app) + + # Is this an Android 12 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + return constants.CMD_LAUNCH_APP11.format(app) + + # Is this an Android 13 device? + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + return constants.CMD_LAUNCH_APP11.format(app) return constants.CMD_LAUNCH_APP.format(app) diff --git a/androidtv/basetv/basetv_async.py b/androidtv/basetv/basetv_async.py index e57198b7..b733fd87 100644 --- a/androidtv/basetv/basetv_async.py +++ b/androidtv/basetv/basetv_async.py @@ -830,7 +830,7 @@ async def set_volume_level(self, volume_level): new_volume = int(min(max(round(self.max_volume * volume_level), 0.0), self.max_volume)) - await self._adb.shell("media volume --show --stream 3 --set {}".format(new_volume)) + await self._adb.shell(self._cmd_volume_set().format(new_volume)) # return the new volume level return new_volume / self.max_volume diff --git a/androidtv/constants.py b/androidtv/constants.py index 320a0df1..81ba5028 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -99,6 +99,15 @@ class DeviceEnum(IntEnum): "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && " + CMD_PARSE_CURRENT_APP11 ) +#: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 12 device +CMD_DEFINE_CURRENT_APP_VARIABLE12 = ( + "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && " + CMD_PARSE_CURRENT_APP11 +) + +#: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 13 device +CMD_DEFINE_CURRENT_APP_VARIABLE13 = ( + "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && " + CMD_PARSE_CURRENT_APP11 +) #: Output identifier for current/focused application CMD_CURRENT_APP = CMD_DEFINE_CURRENT_APP_VARIABLE + " && echo $CURRENT_APP" @@ -106,6 +115,12 @@ class DeviceEnum(IntEnum): #: Output identifier for current/focused application for an Android 11 device CMD_CURRENT_APP11 = CMD_DEFINE_CURRENT_APP_VARIABLE11 + " && echo $CURRENT_APP" +#: Output identifier for current/focused application for an Android 12 device +CMD_CURRENT_APP12 = CMD_DEFINE_CURRENT_APP_VARIABLE12 + " && echo $CURRENT_APP" + +#: Output identifier for current/focused application for an Android 13 device +CMD_CURRENT_APP13 = CMD_DEFINE_CURRENT_APP_VARIABLE13 + " && echo $CURRENT_APP" + #: Assign focused application identifier to ``CURRENT_APP`` variable (for a Google TV device) CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV = ( "CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && " + CMD_PARSE_CURRENT_APP @@ -114,6 +129,21 @@ class DeviceEnum(IntEnum): #: Output identifier for current/focused application (for a Google TV device) CMD_CURRENT_APP_GOOGLE_TV = CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV + " && echo $CURRENT_APP" +#: set volume +CMD_VOLUME_SET_COMMAND = ( + "media volume --show --stream 3 --set {}" +) + +#: set volume for an Android 12 device +CMD_VOLUME_SET_COMMAND12 = ( + "cmd media_session volume --show --stream 3 --set {}" +) + +#: set volume for an Android 13 device +CMD_VOLUME_SET_COMMAND13 = ( + "cmd media_session volume --show --stream 3 --set {}" +) + #: Get the HDMI input CMD_HDMI_INPUT = ( "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'" @@ -164,6 +194,9 @@ class DeviceEnum(IntEnum): #: Determine the current app and get the state from ``dumpsys media_session`` for an Android 11 device CMD_CURRENT_APP_MEDIA_SESSION_STATE11 = CMD_CURRENT_APP11 + " && " + CMD_MEDIA_SESSION_STATE +#: Determine the current app and get the state from ``dumpsys media_session`` for an Android 13 device +CMD_CURRENT_APP_MEDIA_SESSION_STATE13 = CMD_CURRENT_APP13 + " && " + CMD_MEDIA_SESSION_STATE + #: Determine the current app and get the state from ``dumpsys media_session`` for a Google TV device CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV = CMD_CURRENT_APP_GOOGLE_TV + " && " + CMD_MEDIA_SESSION_STATE @@ -484,6 +517,7 @@ class DeviceEnum(IntEnum): APP_VEVO = "com.vevo.tv" APP_VH1 = "com.mtvn.vh1android" APP_VIMEO = "com.vimeo.android.videoapp" +APP_VIKI = "com.viki.android" APP_VLC = "org.videolan.vlc" APP_VOYO = "com.phonegap.voyo" APP_VRV = "com.ellation.vrv" @@ -591,6 +625,7 @@ class DeviceEnum(IntEnum): APP_VEVO: "Vevo", APP_VH1: "VH1", APP_VIMEO: "Vimeo", + APP_VIKI: "Rakuten Viki", APP_VLC: "VLC", APP_VOYO: "VOYO", APP_VRV: "VRV", From 7e569ed336be00809dbdabb757c64ee27ce1cbb0 Mon Sep 17 00:00:00 2001 From: Nortonko Date: Mon, 9 Oct 2023 20:16:08 +0200 Subject: [PATCH 2/9] reformated code by black formater --- androidtv/basetv/basetv.py | 231 +++++++++++++++++++++++++++++-------- androidtv/constants.py | 93 ++++++++++----- 2 files changed, 242 insertions(+), 82 deletions(-) diff --git a/androidtv/basetv/basetv.py b/androidtv/basetv/basetv.py index a7a9ed73..ee875036 100644 --- a/androidtv/basetv/basetv.py +++ b/androidtv/basetv/basetv.py @@ -69,7 +69,14 @@ class BaseTV(object): # pylint: disable=too-few-public-methods DEVICE_ENUM = constants.DeviceEnum.BASETV def __init__( - self, adb, host, port=5555, adbkey="", adb_server_ip="", adb_server_port=5037, state_detection_rules=None + self, + adb, + host, + port=5555, + adbkey="", + adb_server_ip="", + adb_server_port=5037, + state_detection_rules=None, ): self._adb = adb self.host = host @@ -85,7 +92,11 @@ def __init__( if self._state_detection_rules: for app_id, rules in self._state_detection_rules.items(): if not isinstance(app_id, str): - raise TypeError("{0} is of type {1}, not str".format(app_id, type(app_id).__name__)) + raise TypeError( + "{0} is of type {1}, not str".format( + app_id, type(app_id).__name__ + ) + ) state_detection_rules_validator(rules) # the max volume level (determined when first getting the volume level) @@ -129,15 +140,24 @@ def _cmd_audio_state(self): return self._custom_commands[constants.CUSTOM_AUDIO_STATE] # Is this an Android 11 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "11" + ): return constants.CMD_AUDIO_STATE11 - + # Is this an Android 12 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "12" + ): return constants.CMD_AUDIO_STATE11 - + # Is this an Android 13 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "13" + ): return constants.CMD_AUDIO_STATE11 return constants.CMD_AUDIO_STATE @@ -162,15 +182,24 @@ def _cmd_current_app(self): return constants.CMD_CURRENT_APP_GOOGLE_TV # Is this an Android 11 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "11" + ): return constants.CMD_CURRENT_APP11 # Is this an Android 12 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "12" + ): return constants.CMD_CURRENT_APP12 - + # Is this an Android 13 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "13" + ): return constants.CMD_CURRENT_APP13 return constants.CMD_CURRENT_APP @@ -185,7 +214,9 @@ def _cmd_current_app_media_session_state(self): """ if constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE in self._custom_commands: - return self._custom_commands[constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE] + return self._custom_commands[ + constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE + ] # Is this a Google Chromecast Android TV? if ( @@ -196,15 +227,24 @@ def _cmd_current_app_media_session_state(self): return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV # Is this an Android 11 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "11" + ): return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11 # Is this an Android 11 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "12" + ): return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE12 - + # Is this an Android 13 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "13" + ): return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE13 return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE @@ -222,15 +262,24 @@ def _cmd_hdmi_input(self): return self._custom_commands[constants.CUSTOM_HDMI_INPUT] # Is this an Android 11 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "11" + ): return constants.CMD_HDMI_INPUT11 - + # Is this an Android 12 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "12" + ): return constants.CMD_HDMI_INPUT11 - + # Is this an Android 13 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "13" + ): return constants.CMD_HDMI_INPUT11 return constants.CMD_HDMI_INPUT @@ -245,19 +294,28 @@ def _cmd_volume_set(self): """ # Is this an Android 11 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "11" + ): return constants.CMD_VOLUME_SET_COMMAND12 # Is this an Android 12 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "12" + ): return constants.CMD_VOLUME_SET_COMMAND12 - + # Is this an Android 13 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "13" + ): return constants.CMD_VOLUME_SET_COMMAND13 return constants.CMD_VOLUME_SET_COMMAND - + def _cmd_launch_app(self, app): """Get the command to launch the specified app for this device. @@ -287,15 +345,24 @@ def _cmd_launch_app(self, app): return constants.CMD_LAUNCH_APP_FIRETV.format(app) # Is this an Android 11 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "11" + ): return constants.CMD_LAUNCH_APP11.format(app) - + # Is this an Android 12 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "12" + ): return constants.CMD_LAUNCH_APP11.format(app) - + # Is this an Android 13 device? - if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": + if ( + self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + and self.device_properties.get("sw_version", "") == "13" + ): return constants.CMD_LAUNCH_APP11.format(app) return constants.CMD_LAUNCH_APP.format(app) @@ -402,7 +469,12 @@ def _parse_device_properties(self, properties): ``'serialno'``, ``'manufacturer'``, ``'model'``, and ``'sw_version'`` """ - _LOGGER.debug("%s:%d `get_device_properties` response: %s", self.host, self.port, properties) + _LOGGER.debug( + "%s:%d `get_device_properties` response: %s", + self.host, + self.port, + properties, + ) if not properties: self.device_properties = {} @@ -416,7 +488,12 @@ def _parse_device_properties(self, properties): manufacturer, model, serialno, version = lines if not serialno.strip(): - _LOGGER.warning("Could not obtain serialno for %s:%d, got: '%s'", self.host, self.port, serialno) + _LOGGER.warning( + "Could not obtain serialno for %s:%d, got: '%s'", + self.host, + self.port, + serialno, + ) serialno = None self.device_properties = { @@ -456,7 +533,11 @@ def _parse_mac_address(mac_response): # # # ======================================================================= # def _custom_state_detection( - self, current_app=None, media_session_state=None, wake_lock_size=None, audio_state=None + self, + current_app=None, + media_session_state=None, + wake_lock_size=None, + audio_state=None, ): """Use the rules in ``self._state_detection_rules`` to determine the state. @@ -477,7 +558,11 @@ def _custom_state_detection( The state, if it could be determined using the rules in ``self._state_detection_rules``; otherwise, ``None`` """ - if not self._state_detection_rules or current_app is None or current_app not in self._state_detection_rules: + if ( + not self._state_detection_rules + or current_app is None + or current_app not in self._state_detection_rules + ): return None rules = self._state_detection_rules[current_app] @@ -511,7 +596,9 @@ def _custom_state_detection( return None @staticmethod - def _conditions_are_true(conditions, media_session_state=None, wake_lock_size=None, audio_state=None): + def _conditions_are_true( + conditions, media_session_state=None, wake_lock_size=None, audio_state=None + ): """Check whether the conditions in ``conditions`` are true. Parameters @@ -573,7 +660,9 @@ def _audio_output_device(stream_music): if not stream_music: return None - matches = re.findall(constants.DEVICE_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE) + matches = re.findall( + constants.DEVICE_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE + ) if matches: return matches[0] @@ -617,12 +706,18 @@ def _current_app(current_app_response): The current app, or ``None`` if it could not be determined """ - if not current_app_response or "=" in current_app_response or "{" in current_app_response: + if ( + not current_app_response + or "=" in current_app_response + or "{" in current_app_response + ): return None return current_app_response - def _current_app_media_session_state(self, current_app_media_session_state_response): + def _current_app_media_session_state( + self, current_app_media_session_state_response + ): """Get the current app and the media session state properties from the output of `androidtv.basetv.basetv.BaseTV._cmd_current_app_media_session_state`. Parameters @@ -646,7 +741,9 @@ def _current_app_media_session_state(self, current_app_media_session_state_respo current_app = self._current_app(lines[0].strip()) if len(lines) > 1: - matches = constants.REGEX_MEDIA_SESSION_STATE.search(current_app_media_session_state_response) + matches = constants.REGEX_MEDIA_SESSION_STATE.search( + current_app_media_session_state_response + ) if matches: return current_app, int(matches.group("state")) @@ -667,7 +764,9 @@ def _get_hdmi_input(hdmi_response): The HDMI input, or ``None`` if it could not be determined """ - return hdmi_response.strip() if hdmi_response and hdmi_response.strip() else None + return ( + hdmi_response.strip() if hdmi_response and hdmi_response.strip() else None + ) @staticmethod def _get_installed_apps(installed_apps_response): @@ -686,7 +785,9 @@ def _get_installed_apps(installed_apps_response): """ if installed_apps_response is not None: return [ - line.strip().rsplit("package:", 1)[-1] for line in installed_apps_response.splitlines() if line.strip() + line.strip().rsplit("package:", 1)[-1] + for line in installed_apps_response.splitlines() + if line.strip() ] return None @@ -709,7 +810,9 @@ def _is_volume_muted(stream_music): if not stream_music: return None - matches = re.findall(constants.MUTED_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE) + matches = re.findall( + constants.MUTED_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE + ) if matches: return matches[0] == "true" @@ -733,7 +836,11 @@ def _parse_stream_music(stream_music_raw): if not stream_music_raw: return None - matches = re.findall(constants.STREAM_MUSIC_REGEX_PATTERN, stream_music_raw, re.DOTALL | re.MULTILINE) + matches = re.findall( + constants.STREAM_MUSIC_REGEX_PATTERN, + stream_music_raw, + re.DOTALL | re.MULTILINE, + ) if matches: return matches[0] @@ -755,7 +862,11 @@ def _running_apps(running_apps_response): """ if running_apps_response: - return [line.strip().rsplit(" ", 1)[-1] for line in running_apps_response.splitlines() if line.strip()] + return [ + line.strip().rsplit(" ", 1)[-1] + for line in running_apps_response.splitlines() + if line.strip() + ] return None @@ -810,7 +921,11 @@ def _volume(self, stream_music, audio_output_device): return None if not self.max_volume: - max_volume_matches = re.findall(constants.MAX_VOLUME_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE) + max_volume_matches = re.findall( + constants.MAX_VOLUME_REGEX_PATTERN, + stream_music, + re.DOTALL | re.MULTILINE, + ) if max_volume_matches: self.max_volume = float(max_volume_matches[0]) @@ -818,7 +933,9 @@ def _volume(self, stream_music, audio_output_device): return None volume_matches = re.findall( - audio_output_device + constants.VOLUME_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE + audio_output_device + constants.VOLUME_REGEX_PATTERN, + stream_music, + re.DOTALL | re.MULTILINE, ) if volume_matches: return int(volume_matches[0]) @@ -860,7 +977,9 @@ def _wake_lock_size(wake_lock_size_response): """ if wake_lock_size_response: - wake_lock_size_matches = constants.REGEX_WAKE_LOCK_SIZE.search(wake_lock_size_response) + wake_lock_size_matches = constants.REGEX_WAKE_LOCK_SIZE.search( + wake_lock_size_response + ) if wake_lock_size_matches: return int(wake_lock_size_matches.group("size")) @@ -943,7 +1062,11 @@ def state_detection_rules_validator(rules, exc=KeyError): for state, conditions in rule.items(): # The keys of the dictionary must be valid states if state not in constants.VALID_STATES: - raise exc("'{0}' is not a valid state for the 'state_detection_rules' parameter".format(state)) + raise exc( + "'{0}' is not a valid state for the 'state_detection_rules' parameter".format( + state + ) + ) # The values of the dictionary must be dictionaries if not isinstance(conditions, dict): @@ -956,13 +1079,19 @@ def state_detection_rules_validator(rules, exc=KeyError): for prop, value in conditions.items(): # The keys of the dictionary must be valid properties that can be checked if prop not in constants.VALID_PROPERTIES: - raise exc("Invalid property '{0}' is not in {1}".format(prop, constants.VALID_PROPERTIES)) + raise exc( + "Invalid property '{0}' is not in {1}".format( + prop, constants.VALID_PROPERTIES + ) + ) # Make sure the value is of the right type if not isinstance(value, constants.VALID_PROPERTIES_TYPES[prop]): raise exc( "Conditional value for property '{0}' must be of type {1}, not {2}".format( - prop, constants.VALID_PROPERTIES_TYPES[prop].__name__, type(value).__name__ + prop, + constants.VALID_PROPERTIES_TYPES[prop].__name__, + type(value).__name__, ) ) diff --git a/androidtv/constants.py b/androidtv/constants.py index 81ba5028..107341c9 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -88,25 +88,31 @@ class DeviceEnum(IntEnum): CMD_PARSE_CURRENT_APP = "CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*}" #: Parse current application for an Android 11 device -CMD_PARSE_CURRENT_APP11 = "CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* }" +CMD_PARSE_CURRENT_APP11 = ( + "CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* }" +) #: Assign focused application identifier to ``CURRENT_APP`` variable CMD_DEFINE_CURRENT_APP_VARIABLE = ( - "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + CMD_PARSE_CURRENT_APP + "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + + CMD_PARSE_CURRENT_APP ) #: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 11 device CMD_DEFINE_CURRENT_APP_VARIABLE11 = ( - "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && " + CMD_PARSE_CURRENT_APP11 + "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && " + + CMD_PARSE_CURRENT_APP11 ) #: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 12 device CMD_DEFINE_CURRENT_APP_VARIABLE12 = ( - "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && " + CMD_PARSE_CURRENT_APP11 + "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && " + + CMD_PARSE_CURRENT_APP11 ) #: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 13 device CMD_DEFINE_CURRENT_APP_VARIABLE13 = ( - "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && " + CMD_PARSE_CURRENT_APP11 + "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && " + + CMD_PARSE_CURRENT_APP11 ) #: Output identifier for current/focused application @@ -123,31 +129,26 @@ class DeviceEnum(IntEnum): #: Assign focused application identifier to ``CURRENT_APP`` variable (for a Google TV device) CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV = ( - "CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && " + CMD_PARSE_CURRENT_APP + "CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && " + + CMD_PARSE_CURRENT_APP ) #: Output identifier for current/focused application (for a Google TV device) -CMD_CURRENT_APP_GOOGLE_TV = CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV + " && echo $CURRENT_APP" +CMD_CURRENT_APP_GOOGLE_TV = ( + CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV + " && echo $CURRENT_APP" +) #: set volume -CMD_VOLUME_SET_COMMAND = ( - "media volume --show --stream 3 --set {}" -) +CMD_VOLUME_SET_COMMAND = "media volume --show --stream 3 --set {}" #: set volume for an Android 12 device -CMD_VOLUME_SET_COMMAND12 = ( - "cmd media_session volume --show --stream 3 --set {}" -) +CMD_VOLUME_SET_COMMAND12 = "cmd media_session volume --show --stream 3 --set {}" #: set volume for an Android 13 device -CMD_VOLUME_SET_COMMAND13 = ( - "cmd media_session volume --show --stream 3 --set {}" -) +CMD_VOLUME_SET_COMMAND13 = "cmd media_session volume --show --stream 3 --set {}" #: Get the HDMI input -CMD_HDMI_INPUT = ( - "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'" -) +CMD_HDMI_INPUT = "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'" #: Get the HDMI input for an Android 11 device CMD_HDMI_INPUT11 = ( @@ -157,32 +158,44 @@ class DeviceEnum(IntEnum): #: Launch an app if it is not already the current app (assumes the variable ``CURRENT_APP`` has already been set) CMD_LAUNCH_APP_CONDITION = ( - "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi" + "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + + INTENT_LAUNCH + + " --pct-syskeys 0 1; fi" ) #: Launch an app if it is not already the current app (assumes the variable ``CURRENT_APP`` has already been set) on a Fire TV CMD_LAUNCH_APP_CONDITION_FIRETV = ( - "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH_FIRETV + " --pct-syskeys 0 1; fi" + "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + + INTENT_LAUNCH_FIRETV + + " --pct-syskeys 0 1; fi" ) #: Launch an app if it is not already the current app CMD_LAUNCH_APP = ( - CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION + CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + + " && " + + CMD_LAUNCH_APP_CONDITION ) #: Launch an app if it is not already the current app on an Android 11 device CMD_LAUNCH_APP11 = ( - CMD_DEFINE_CURRENT_APP_VARIABLE11.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION + CMD_DEFINE_CURRENT_APP_VARIABLE11.replace("{", "{{").replace("}", "}}") + + " && " + + CMD_LAUNCH_APP_CONDITION ) #: Launch an app on a Fire TV device CMD_LAUNCH_APP_FIRETV = ( - CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION_FIRETV + CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + + " && " + + CMD_LAUNCH_APP_CONDITION_FIRETV ) #: Launch an app on a Google TV device CMD_LAUNCH_APP_GOOGLE_TV = ( - CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION + CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV.replace("{", "{{").replace("}", "}}") + + " && " + + CMD_LAUNCH_APP_CONDITION ) #: Get the state from ``dumpsys media_session``; this assumes that the variable ``CURRENT_APP`` has been defined @@ -192,13 +205,19 @@ class DeviceEnum(IntEnum): CMD_CURRENT_APP_MEDIA_SESSION_STATE = CMD_CURRENT_APP + " && " + CMD_MEDIA_SESSION_STATE #: Determine the current app and get the state from ``dumpsys media_session`` for an Android 11 device -CMD_CURRENT_APP_MEDIA_SESSION_STATE11 = CMD_CURRENT_APP11 + " && " + CMD_MEDIA_SESSION_STATE +CMD_CURRENT_APP_MEDIA_SESSION_STATE11 = ( + CMD_CURRENT_APP11 + " && " + CMD_MEDIA_SESSION_STATE +) #: Determine the current app and get the state from ``dumpsys media_session`` for an Android 13 device -CMD_CURRENT_APP_MEDIA_SESSION_STATE13 = CMD_CURRENT_APP13 + " && " + CMD_MEDIA_SESSION_STATE +CMD_CURRENT_APP_MEDIA_SESSION_STATE13 = ( + CMD_CURRENT_APP13 + " && " + CMD_MEDIA_SESSION_STATE +) #: Determine the current app and get the state from ``dumpsys media_session`` for a Google TV device -CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV = CMD_CURRENT_APP_GOOGLE_TV + " && " + CMD_MEDIA_SESSION_STATE +CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV = ( + CMD_CURRENT_APP_GOOGLE_TV + " && " + CMD_MEDIA_SESSION_STATE +) #: Get the running apps for an Android TV device CMD_RUNNING_APPS_ANDROIDTV = "ps -A | grep u0_a" @@ -232,7 +251,13 @@ class DeviceEnum(IntEnum): #: Determine if the device is on, the screen is on, and get the wake lock size CMD_SCREEN_ON_AWAKE_WAKE_LOCK_SIZE = ( - CMD_SCREEN_ON + CMD_SUCCESS1_FAILURE0 + " && " + CMD_AWAKE + CMD_SUCCESS1_FAILURE0 + " && " + CMD_WAKE_LOCK_SIZE + CMD_SCREEN_ON + + CMD_SUCCESS1_FAILURE0 + + " && " + + CMD_AWAKE + + CMD_SUCCESS1_FAILURE0 + + " && " + + CMD_WAKE_LOCK_SIZE ) # `getprop` commands @@ -246,7 +271,9 @@ class DeviceEnum(IntEnum): CMD_MAC_ETH0 = "ip addr show eth0 | grep -m 1 ether" #: The command used for getting the device properties -CMD_DEVICE_PROPERTIES = CMD_MANUFACTURER + " && " + CMD_MODEL + " && " + CMD_SERIALNO + " && " + CMD_VERSION +CMD_DEVICE_PROPERTIES = ( + CMD_MANUFACTURER + " && " + CMD_MODEL + " && " + CMD_SERIALNO + " && " + CMD_VERSION +) # ADB key event codes @@ -414,7 +441,11 @@ class DeviceEnum(IntEnum): VALID_PROPERTIES = VALID_STATE_PROPERTIES + ("wake_lock_size",) #: The required type for each entry in :py:const:`VALID_PROPERTIES` (used by :func:`~androidtv.basetv.state_detection_rules_validator`) -VALID_PROPERTIES_TYPES = {"audio_state": str, "media_session_state": int, "wake_lock_size": int} +VALID_PROPERTIES_TYPES = { + "audio_state": str, + "media_session_state": int, + "wake_lock_size": int, +} # https://developer.android.com/reference/android/media/session/PlaybackState.html #: States for the :attr:`~androidtv.basetv.basetv.BaseTV.media_session_state` property From 7f68b696d28fd852a18f9d742e0dfd721b934f38 Mon Sep 17 00:00:00 2001 From: rasto Date: Tue, 10 Oct 2023 08:46:39 +0200 Subject: [PATCH 3/9] reformatted code by black again --- androidtv/basetv/basetv.py | 162 ++++++++----------------------------- androidtv/constants.py | 70 +++++----------- 2 files changed, 52 insertions(+), 180 deletions(-) diff --git a/androidtv/basetv/basetv.py b/androidtv/basetv/basetv.py index ee875036..548f8c4c 100644 --- a/androidtv/basetv/basetv.py +++ b/androidtv/basetv/basetv.py @@ -92,11 +92,7 @@ def __init__( if self._state_detection_rules: for app_id, rules in self._state_detection_rules.items(): if not isinstance(app_id, str): - raise TypeError( - "{0} is of type {1}, not str".format( - app_id, type(app_id).__name__ - ) - ) + raise TypeError("{0} is of type {1}, not str".format(app_id, type(app_id).__name__)) state_detection_rules_validator(rules) # the max volume level (determined when first getting the volume level) @@ -140,24 +136,15 @@ def _cmd_audio_state(self): return self._custom_commands[constants.CUSTOM_AUDIO_STATE] # Is this an Android 11 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "11" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_AUDIO_STATE11 # Is this an Android 12 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "12" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": return constants.CMD_AUDIO_STATE11 # Is this an Android 13 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "13" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": return constants.CMD_AUDIO_STATE11 return constants.CMD_AUDIO_STATE @@ -182,24 +169,15 @@ def _cmd_current_app(self): return constants.CMD_CURRENT_APP_GOOGLE_TV # Is this an Android 11 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "11" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_CURRENT_APP11 # Is this an Android 12 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "12" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": return constants.CMD_CURRENT_APP12 # Is this an Android 13 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "13" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": return constants.CMD_CURRENT_APP13 return constants.CMD_CURRENT_APP @@ -214,9 +192,7 @@ def _cmd_current_app_media_session_state(self): """ if constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE in self._custom_commands: - return self._custom_commands[ - constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE - ] + return self._custom_commands[constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE] # Is this a Google Chromecast Android TV? if ( @@ -227,24 +203,15 @@ def _cmd_current_app_media_session_state(self): return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV # Is this an Android 11 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "11" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11 # Is this an Android 11 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "12" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE12 # Is this an Android 13 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "13" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE13 return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE @@ -262,24 +229,15 @@ def _cmd_hdmi_input(self): return self._custom_commands[constants.CUSTOM_HDMI_INPUT] # Is this an Android 11 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "11" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_HDMI_INPUT11 # Is this an Android 12 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "12" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": return constants.CMD_HDMI_INPUT11 # Is this an Android 13 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "13" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": return constants.CMD_HDMI_INPUT11 return constants.CMD_HDMI_INPUT @@ -294,24 +252,15 @@ def _cmd_volume_set(self): """ # Is this an Android 11 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "11" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_VOLUME_SET_COMMAND12 # Is this an Android 12 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "12" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": return constants.CMD_VOLUME_SET_COMMAND12 # Is this an Android 13 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "13" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": return constants.CMD_VOLUME_SET_COMMAND13 return constants.CMD_VOLUME_SET_COMMAND @@ -345,24 +294,15 @@ def _cmd_launch_app(self, app): return constants.CMD_LAUNCH_APP_FIRETV.format(app) # Is this an Android 11 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "11" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_LAUNCH_APP11.format(app) # Is this an Android 12 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "12" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": return constants.CMD_LAUNCH_APP11.format(app) # Is this an Android 13 device? - if ( - self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV - and self.device_properties.get("sw_version", "") == "13" - ): + if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": return constants.CMD_LAUNCH_APP11.format(app) return constants.CMD_LAUNCH_APP.format(app) @@ -558,11 +498,7 @@ def _custom_state_detection( The state, if it could be determined using the rules in ``self._state_detection_rules``; otherwise, ``None`` """ - if ( - not self._state_detection_rules - or current_app is None - or current_app not in self._state_detection_rules - ): + if not self._state_detection_rules or current_app is None or current_app not in self._state_detection_rules: return None rules = self._state_detection_rules[current_app] @@ -596,9 +532,7 @@ def _custom_state_detection( return None @staticmethod - def _conditions_are_true( - conditions, media_session_state=None, wake_lock_size=None, audio_state=None - ): + def _conditions_are_true(conditions, media_session_state=None, wake_lock_size=None, audio_state=None): """Check whether the conditions in ``conditions`` are true. Parameters @@ -660,9 +594,7 @@ def _audio_output_device(stream_music): if not stream_music: return None - matches = re.findall( - constants.DEVICE_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE - ) + matches = re.findall(constants.DEVICE_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE) if matches: return matches[0] @@ -706,18 +638,12 @@ def _current_app(current_app_response): The current app, or ``None`` if it could not be determined """ - if ( - not current_app_response - or "=" in current_app_response - or "{" in current_app_response - ): + if not current_app_response or "=" in current_app_response or "{" in current_app_response: return None return current_app_response - def _current_app_media_session_state( - self, current_app_media_session_state_response - ): + def _current_app_media_session_state(self, current_app_media_session_state_response): """Get the current app and the media session state properties from the output of `androidtv.basetv.basetv.BaseTV._cmd_current_app_media_session_state`. Parameters @@ -741,9 +667,7 @@ def _current_app_media_session_state( current_app = self._current_app(lines[0].strip()) if len(lines) > 1: - matches = constants.REGEX_MEDIA_SESSION_STATE.search( - current_app_media_session_state_response - ) + matches = constants.REGEX_MEDIA_SESSION_STATE.search(current_app_media_session_state_response) if matches: return current_app, int(matches.group("state")) @@ -764,9 +688,7 @@ def _get_hdmi_input(hdmi_response): The HDMI input, or ``None`` if it could not be determined """ - return ( - hdmi_response.strip() if hdmi_response and hdmi_response.strip() else None - ) + return hdmi_response.strip() if hdmi_response and hdmi_response.strip() else None @staticmethod def _get_installed_apps(installed_apps_response): @@ -785,9 +707,7 @@ def _get_installed_apps(installed_apps_response): """ if installed_apps_response is not None: return [ - line.strip().rsplit("package:", 1)[-1] - for line in installed_apps_response.splitlines() - if line.strip() + line.strip().rsplit("package:", 1)[-1] for line in installed_apps_response.splitlines() if line.strip() ] return None @@ -810,9 +730,7 @@ def _is_volume_muted(stream_music): if not stream_music: return None - matches = re.findall( - constants.MUTED_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE - ) + matches = re.findall(constants.MUTED_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE) if matches: return matches[0] == "true" @@ -862,11 +780,7 @@ def _running_apps(running_apps_response): """ if running_apps_response: - return [ - line.strip().rsplit(" ", 1)[-1] - for line in running_apps_response.splitlines() - if line.strip() - ] + return [line.strip().rsplit(" ", 1)[-1] for line in running_apps_response.splitlines() if line.strip()] return None @@ -977,9 +891,7 @@ def _wake_lock_size(wake_lock_size_response): """ if wake_lock_size_response: - wake_lock_size_matches = constants.REGEX_WAKE_LOCK_SIZE.search( - wake_lock_size_response - ) + wake_lock_size_matches = constants.REGEX_WAKE_LOCK_SIZE.search(wake_lock_size_response) if wake_lock_size_matches: return int(wake_lock_size_matches.group("size")) @@ -1062,11 +974,7 @@ def state_detection_rules_validator(rules, exc=KeyError): for state, conditions in rule.items(): # The keys of the dictionary must be valid states if state not in constants.VALID_STATES: - raise exc( - "'{0}' is not a valid state for the 'state_detection_rules' parameter".format( - state - ) - ) + raise exc("'{0}' is not a valid state for the 'state_detection_rules' parameter".format(state)) # The values of the dictionary must be dictionaries if not isinstance(conditions, dict): @@ -1079,11 +987,7 @@ def state_detection_rules_validator(rules, exc=KeyError): for prop, value in conditions.items(): # The keys of the dictionary must be valid properties that can be checked if prop not in constants.VALID_PROPERTIES: - raise exc( - "Invalid property '{0}' is not in {1}".format( - prop, constants.VALID_PROPERTIES - ) - ) + raise exc("Invalid property '{0}' is not in {1}".format(prop, constants.VALID_PROPERTIES)) # Make sure the value is of the right type if not isinstance(value, constants.VALID_PROPERTIES_TYPES[prop]): diff --git a/androidtv/constants.py b/androidtv/constants.py index 107341c9..03ffd67f 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -88,19 +88,14 @@ class DeviceEnum(IntEnum): CMD_PARSE_CURRENT_APP = "CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*}" #: Parse current application for an Android 11 device -CMD_PARSE_CURRENT_APP11 = ( - "CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* }" -) - +CMD_PARSE_CURRENT_APP11 = "CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* }" #: Assign focused application identifier to ``CURRENT_APP`` variable CMD_DEFINE_CURRENT_APP_VARIABLE = ( - "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " - + CMD_PARSE_CURRENT_APP + "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + CMD_PARSE_CURRENT_APP ) #: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 11 device CMD_DEFINE_CURRENT_APP_VARIABLE11 = ( - "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && " - + CMD_PARSE_CURRENT_APP11 + "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && " + CMD_PARSE_CURRENT_APP11 ) #: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 12 device @@ -129,14 +124,11 @@ class DeviceEnum(IntEnum): #: Assign focused application identifier to ``CURRENT_APP`` variable (for a Google TV device) CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV = ( - "CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && " - + CMD_PARSE_CURRENT_APP + "CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && " + CMD_PARSE_CURRENT_APP ) #: Output identifier for current/focused application (for a Google TV device) -CMD_CURRENT_APP_GOOGLE_TV = ( - CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV + " && echo $CURRENT_APP" -) +CMD_CURRENT_APP_GOOGLE_TV = CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV + " && echo $CURRENT_APP" #: set volume CMD_VOLUME_SET_COMMAND = "media volume --show --stream 3 --set {}" @@ -148,7 +140,9 @@ class DeviceEnum(IntEnum): CMD_VOLUME_SET_COMMAND13 = "cmd media_session volume --show --stream 3 --set {}" #: Get the HDMI input -CMD_HDMI_INPUT = "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'" +CMD_HDMI_INPUT = ( + "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'" +) #: Get the HDMI input for an Android 11 device CMD_HDMI_INPUT11 = ( @@ -158,44 +152,32 @@ class DeviceEnum(IntEnum): #: Launch an app if it is not already the current app (assumes the variable ``CURRENT_APP`` has already been set) CMD_LAUNCH_APP_CONDITION = ( - "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " - + INTENT_LAUNCH - + " --pct-syskeys 0 1; fi" + "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi" ) #: Launch an app if it is not already the current app (assumes the variable ``CURRENT_APP`` has already been set) on a Fire TV CMD_LAUNCH_APP_CONDITION_FIRETV = ( - "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " - + INTENT_LAUNCH_FIRETV - + " --pct-syskeys 0 1; fi" + "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH_FIRETV + " --pct-syskeys 0 1; fi" ) #: Launch an app if it is not already the current app CMD_LAUNCH_APP = ( - CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") - + " && " - + CMD_LAUNCH_APP_CONDITION + CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION ) #: Launch an app if it is not already the current app on an Android 11 device CMD_LAUNCH_APP11 = ( - CMD_DEFINE_CURRENT_APP_VARIABLE11.replace("{", "{{").replace("}", "}}") - + " && " - + CMD_LAUNCH_APP_CONDITION + CMD_DEFINE_CURRENT_APP_VARIABLE11.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION ) #: Launch an app on a Fire TV device CMD_LAUNCH_APP_FIRETV = ( - CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") - + " && " - + CMD_LAUNCH_APP_CONDITION_FIRETV + CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION_FIRETV ) #: Launch an app on a Google TV device CMD_LAUNCH_APP_GOOGLE_TV = ( - CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV.replace("{", "{{").replace("}", "}}") - + " && " - + CMD_LAUNCH_APP_CONDITION + CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION ) #: Get the state from ``dumpsys media_session``; this assumes that the variable ``CURRENT_APP`` has been defined @@ -205,19 +187,13 @@ class DeviceEnum(IntEnum): CMD_CURRENT_APP_MEDIA_SESSION_STATE = CMD_CURRENT_APP + " && " + CMD_MEDIA_SESSION_STATE #: Determine the current app and get the state from ``dumpsys media_session`` for an Android 11 device -CMD_CURRENT_APP_MEDIA_SESSION_STATE11 = ( - CMD_CURRENT_APP11 + " && " + CMD_MEDIA_SESSION_STATE -) +CMD_CURRENT_APP_MEDIA_SESSION_STATE11 = CMD_CURRENT_APP11 + " && " + CMD_MEDIA_SESSION_STATE #: Determine the current app and get the state from ``dumpsys media_session`` for an Android 13 device -CMD_CURRENT_APP_MEDIA_SESSION_STATE13 = ( - CMD_CURRENT_APP13 + " && " + CMD_MEDIA_SESSION_STATE -) +CMD_CURRENT_APP_MEDIA_SESSION_STATE13 = CMD_CURRENT_APP13 + " && " + CMD_MEDIA_SESSION_STATE #: Determine the current app and get the state from ``dumpsys media_session`` for a Google TV device -CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV = ( - CMD_CURRENT_APP_GOOGLE_TV + " && " + CMD_MEDIA_SESSION_STATE -) +CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV = CMD_CURRENT_APP_GOOGLE_TV + " && " + CMD_MEDIA_SESSION_STATE #: Get the running apps for an Android TV device CMD_RUNNING_APPS_ANDROIDTV = "ps -A | grep u0_a" @@ -251,13 +227,7 @@ class DeviceEnum(IntEnum): #: Determine if the device is on, the screen is on, and get the wake lock size CMD_SCREEN_ON_AWAKE_WAKE_LOCK_SIZE = ( - CMD_SCREEN_ON - + CMD_SUCCESS1_FAILURE0 - + " && " - + CMD_AWAKE - + CMD_SUCCESS1_FAILURE0 - + " && " - + CMD_WAKE_LOCK_SIZE + CMD_SCREEN_ON + CMD_SUCCESS1_FAILURE0 + " && " + CMD_AWAKE + CMD_SUCCESS1_FAILURE0 + " && " + CMD_WAKE_LOCK_SIZE ) # `getprop` commands @@ -271,9 +241,7 @@ class DeviceEnum(IntEnum): CMD_MAC_ETH0 = "ip addr show eth0 | grep -m 1 ether" #: The command used for getting the device properties -CMD_DEVICE_PROPERTIES = ( - CMD_MANUFACTURER + " && " + CMD_MODEL + " && " + CMD_SERIALNO + " && " + CMD_VERSION -) +CMD_DEVICE_PROPERTIES = CMD_MANUFACTURER + " && " + CMD_MODEL + " && " + CMD_SERIALNO + " && " + CMD_VERSION # ADB key event codes From 556af13ea0a84c1783e4f874f6d4a4d1f1595c76 Mon Sep 17 00:00:00 2001 From: rasto Date: Tue, 10 Oct 2023 08:54:17 +0200 Subject: [PATCH 4/9] fixed missing CMD_CURRENT_APP_MEDIA_SESSION_STATE12 --- androidtv/constants.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/androidtv/constants.py b/androidtv/constants.py index 03ffd67f..3e925c8f 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -189,6 +189,9 @@ class DeviceEnum(IntEnum): #: Determine the current app and get the state from ``dumpsys media_session`` for an Android 11 device CMD_CURRENT_APP_MEDIA_SESSION_STATE11 = CMD_CURRENT_APP11 + " && " + CMD_MEDIA_SESSION_STATE +#: Determine the current app and get the state from ``dumpsys media_session`` for an Android 12 device +CMD_CURRENT_APP_MEDIA_SESSION_STATE12 = CMD_CURRENT_APP12 + " && " + CMD_MEDIA_SESSION_STATE + #: Determine the current app and get the state from ``dumpsys media_session`` for an Android 13 device CMD_CURRENT_APP_MEDIA_SESSION_STATE13 = CMD_CURRENT_APP13 + " && " + CMD_MEDIA_SESSION_STATE From 4a7bd0480f4af5282778537dffbd659dac624772 Mon Sep 17 00:00:00 2001 From: rasto Date: Tue, 10 Oct 2023 09:37:50 +0200 Subject: [PATCH 5/9] tried to fix the tests errors --- androidtv/constants.py | 4 ++-- tests/generate_test_constants.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/androidtv/constants.py b/androidtv/constants.py index 3e925c8f..448aaae2 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -518,8 +518,8 @@ class DeviceEnum(IntEnum): APP_TWITCH_FIRETV = "tv.twitch.android.viewer" APP_VEVO = "com.vevo.tv" APP_VH1 = "com.mtvn.vh1android" -APP_VIMEO = "com.vimeo.android.videoapp" APP_VIKI = "com.viki.android" +APP_VIMEO = "com.vimeo.android.videoapp" APP_VLC = "org.videolan.vlc" APP_VOYO = "com.phonegap.voyo" APP_VRV = "com.ellation.vrv" @@ -626,8 +626,8 @@ class DeviceEnum(IntEnum): APP_TWITCH_FIRETV: "Twitch (FireTV)", APP_VEVO: "Vevo", APP_VH1: "VH1", - APP_VIMEO: "Vimeo", APP_VIKI: "Rakuten Viki", + APP_VIMEO: "Vimeo", APP_VLC: "VLC", APP_VOYO: "VOYO", APP_VRV: "VRV", diff --git a/tests/generate_test_constants.py b/tests/generate_test_constants.py index f1decd70..74e5769d 100644 --- a/tests/generate_test_constants.py +++ b/tests/generate_test_constants.py @@ -12,8 +12,12 @@ "CMD_SUCCESS1_FAILURE0", "CMD_PARSE_CURRENT_APP", "CMD_PARSE_CURRENT_APP11", + "CMD_PARSE_CURRENT_APP12", + "CMD_PARSE_CURRENT_APP13", "CMD_DEFINE_CURRENT_APP_VARIABLE", "CMD_DEFINE_CURRENT_APP_VARIABLE11", + "CMD_DEFINE_CURRENT_APP_VARIABLE12", + "CMD_DEFINE_CURRENT_APP_VARIABLE13", "CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV", "CMD_LAUNCH_APP_CONDITION", "CMD_LAUNCH_APP_CONDITION_FIRETV", From ae0f179f80f1f45a44dff86ce5511e445b4e3382 Mon Sep 17 00:00:00 2001 From: rasto Date: Tue, 10 Oct 2023 09:54:48 +0200 Subject: [PATCH 6/9] fix missing CMD_VOLUME_SET_COMMAND11 --- androidtv/basetv/basetv.py | 2 +- androidtv/constants.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/androidtv/basetv/basetv.py b/androidtv/basetv/basetv.py index 548f8c4c..373f4205 100644 --- a/androidtv/basetv/basetv.py +++ b/androidtv/basetv/basetv.py @@ -253,7 +253,7 @@ def _cmd_volume_set(self): """ # Is this an Android 11 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": - return constants.CMD_VOLUME_SET_COMMAND12 + return constants.CMD_VOLUME_SET_COMMAND11 # Is this an Android 12 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": diff --git a/androidtv/constants.py b/androidtv/constants.py index 448aaae2..a1ec6562 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -133,6 +133,9 @@ class DeviceEnum(IntEnum): #: set volume CMD_VOLUME_SET_COMMAND = "media volume --show --stream 3 --set {}" +#: set volume for an Android 11 device +CMD_VOLUME_SET_COMMAND11 = "cmd media_session volume --show --stream 3 --set {}" + #: set volume for an Android 12 device CMD_VOLUME_SET_COMMAND12 = "cmd media_session volume --show --stream 3 --set {}" From c31e66373f9316c59bcc765d99607e3cf94d02ed Mon Sep 17 00:00:00 2001 From: rasto Date: Tue, 10 Oct 2023 13:54:32 +0200 Subject: [PATCH 7/9] fixed errors --- androidtv/basetv/basetv.py | 10 ++++---- androidtv/constants.py | 18 ++++++++------ tests/test_constants.py | 50 +++++++++++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/androidtv/basetv/basetv.py b/androidtv/basetv/basetv.py index 373f4205..8174d8ef 100644 --- a/androidtv/basetv/basetv.py +++ b/androidtv/basetv/basetv.py @@ -206,7 +206,7 @@ def _cmd_current_app_media_session_state(self): if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11": return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11 - # Is this an Android 11 device? + # Is this an Android 12 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE12 @@ -257,11 +257,11 @@ def _cmd_volume_set(self): # Is this an Android 12 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": - return constants.CMD_VOLUME_SET_COMMAND12 + return constants.CMD_VOLUME_SET_COMMAND11 # Is this an Android 13 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": - return constants.CMD_VOLUME_SET_COMMAND13 + return constants.CMD_VOLUME_SET_COMMAND11 return constants.CMD_VOLUME_SET_COMMAND @@ -299,11 +299,11 @@ def _cmd_launch_app(self, app): # Is this an Android 12 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12": - return constants.CMD_LAUNCH_APP11.format(app) + return constants.CMD_LAUNCH_APP12.format(app) # Is this an Android 13 device? if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13": - return constants.CMD_LAUNCH_APP11.format(app) + return constants.CMD_LAUNCH_APP13.format(app) return constants.CMD_LAUNCH_APP.format(app) diff --git a/androidtv/constants.py b/androidtv/constants.py index a1ec6562..c1c20a67 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -133,15 +133,9 @@ class DeviceEnum(IntEnum): #: set volume CMD_VOLUME_SET_COMMAND = "media volume --show --stream 3 --set {}" -#: set volume for an Android 11 device +#: set volume for an Android 11 & 12 & 13 device CMD_VOLUME_SET_COMMAND11 = "cmd media_session volume --show --stream 3 --set {}" -#: set volume for an Android 12 device -CMD_VOLUME_SET_COMMAND12 = "cmd media_session volume --show --stream 3 --set {}" - -#: set volume for an Android 13 device -CMD_VOLUME_SET_COMMAND13 = "cmd media_session volume --show --stream 3 --set {}" - #: Get the HDMI input CMD_HDMI_INPUT = ( "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'" @@ -173,6 +167,16 @@ class DeviceEnum(IntEnum): CMD_DEFINE_CURRENT_APP_VARIABLE11.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION ) +#: Launch an app if it is not already the current app on an Android 12 device +CMD_LAUNCH_APP12 = ( + CMD_DEFINE_CURRENT_APP_VARIABLE12.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION +) + +#: Launch an app if it is not already the current app on an Android 11 device +CMD_LAUNCH_APP13 = ( + CMD_DEFINE_CURRENT_APP_VARIABLE13.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION +) + #: Launch an app on a Fire TV device CMD_LAUNCH_APP_FIRETV = ( CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION_FIRETV diff --git a/tests/test_constants.py b/tests/test_constants.py index 8093b012..ec9a5d16 100644 --- a/tests/test_constants.py +++ b/tests/test_constants.py @@ -89,6 +89,18 @@ def test_constants(self): r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP", ) + # CMD_CURRENT_APP12 + self.assertCommand( + constants.CMD_CURRENT_APP12, + r"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP", + ) + + # CMD_CURRENT_APP13 + self.assertCommand( + constants.CMD_CURRENT_APP13, + r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP", + ) + # CMD_CURRENT_APP_GOOGLE_TV self.assertCommand( constants.CMD_CURRENT_APP_GOOGLE_TV, @@ -107,6 +119,18 @@ def test_constants(self): r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP && dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'", ) + # CMD_CURRENT_APP_MEDIA_SESSION_STATE12 + self.assertCommand( + constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE12, + r"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP && dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'", + ) + + # CMD_CURRENT_APP_MEDIA_SESSION_STATE13 + self.assertCommand( + constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE13, + r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP && dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'", + ) + # CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV self.assertCommand( constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV, @@ -146,6 +170,18 @@ def test_constants(self): r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP##* }} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c android.intent.category.LEANBACK_LAUNCHER --pct-syskeys 0 1; fi", ) + # CMD_LAUNCH_APP12 + self.assertCommand( + constants.CMD_LAUNCH_APP12, + r"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP##* }} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c android.intent.category.LEANBACK_LAUNCHER --pct-syskeys 0 1; fi", + ) + + # CMD_LAUNCH_APP13 + self.assertCommand( + constants.CMD_LAUNCH_APP13, + r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP##* }} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c android.intent.category.LEANBACK_LAUNCHER --pct-syskeys 0 1; fi", + ) + # CMD_LAUNCH_APP_FIRETV self.assertCommand( constants.CMD_LAUNCH_APP_FIRETV, @@ -227,6 +263,15 @@ def test_constants(self): # CMD_VERSION self.assertCommand(constants.CMD_VERSION, r"getprop ro.build.version.release") + # CMD_VOLUME_SET_COMMAND + self.assertCommand(constants.CMD_VOLUME_SET_COMMAND, r"media volume --show --stream 3 --set {}") + + # CMD_VOLUME_SET_COMMAND11 + self.assertCommand( + constants.CMD_VOLUME_SET_COMMAND11, + r"cmd media_session volume --show --stream 3 --set {}", + ) + # CMD_WAKE_LOCK_SIZE self.assertCommand(constants.CMD_WAKE_LOCK_SIZE, r"dumpsys power | grep Locks | grep 'size='") @@ -238,7 +283,10 @@ def test_no_underscores(self): """Test that 'ANDROID_TV', 'BASE_TV', and 'FIRE_TV' do not appear in the code base.""" cwd = os.path.join(os.path.dirname(__file__), "..") for underscore_name in ["ANDROID_TV", "BASE_TV", "FIRE_TV"]: - with subprocess.Popen(shlex.split("git grep -l {} -- androidtv/".format(underscore_name)), cwd=cwd) as p: + with subprocess.Popen( + shlex.split("git grep -l {} -- androidtv/".format(underscore_name)), + cwd=cwd, + ) as p: self.assertEqual(p.wait(), 1) def test_current_app_extraction_atv_launcher(self): From 93684574d2216e328854b4c6e706fb9e18233925 Mon Sep 17 00:00:00 2001 From: rasto Date: Tue, 10 Oct 2023 15:09:31 +0200 Subject: [PATCH 8/9] Stmts Miss --- tests/test_basetv_sync.py | 398 ++++++++++++++++++++++++++++++-------- 1 file changed, 320 insertions(+), 78 deletions(-) diff --git a/tests/test_basetv_sync.py b/tests/test_basetv_sync.py index d8babddd..2931b743 100644 --- a/tests/test_basetv_sync.py +++ b/tests/test_basetv_sync.py @@ -113,6 +113,24 @@ "ethmac": "ab:cd:ef:gh:ij:kl", } +DEVICE_PROPERTIES_DICT_SHIELD_TV_12 = { + "manufacturer": "NVIDIA", + "model": "SHIELD Android TV", + "serialno": "0123456789012", + "sw_version": "12", + "wifimac": "11:22:33:44:55:66", + "ethmac": "ab:cd:ef:gh:ij:kl", +} + +DEVICE_PROPERTIES_DICT_SHIELD_TV_13 = { + "manufacturer": "NVIDIA", + "model": "SHIELD Android TV", + "serialno": "0123456789012", + "sw_version": "13", + "wifimac": "11:22:33:44:55:66", + "ethmac": "ab:cd:ef:gh:ij:kl", +} + INSTALLED_APPS_OUTPUT_1 = """package:org.example.app package:org.example.launcher """ @@ -190,290 +208,350 @@ def test_keys(self): self.btv.space() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_SPACE) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_SPACE), ) self.btv.key_0() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_0) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_0), ) self.btv.key_1() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_1) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_1), ) self.btv.key_2() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_2) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_2), ) self.btv.key_3() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_3) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_3), ) self.btv.key_4() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_4) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_4), ) self.btv.key_5() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_5) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_5), ) self.btv.key_6() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_6) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_6), ) self.btv.key_7() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_7) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_7), ) self.btv.key_8() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_8) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_8), ) self.btv.key_9() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_9) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_9), ) self.btv.key_a() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_A) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_A), ) self.btv.key_b() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_B) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_B), ) self.btv.key_c() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_C) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_C), ) self.btv.key_d() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_D) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_D), ) self.btv.key_e() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_E) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_E), ) self.btv.key_f() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_F) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_F), ) self.btv.key_g() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_G) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_G), ) self.btv.key_h() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_H) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_H), ) self.btv.key_i() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_I) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_I), ) self.btv.key_j() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_J) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_J), ) self.btv.key_k() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_K) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_K), ) self.btv.key_l() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_L) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_L), ) self.btv.key_m() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_M) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_M), ) self.btv.key_n() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_N) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_N), ) self.btv.key_o() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_O) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_O), ) self.btv.key_p() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_P) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_P), ) self.btv.key_q() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_Q) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_Q), ) self.btv.key_r() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_R) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_R), ) self.btv.key_s() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_S) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_S), ) self.btv.key_t() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_T) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_T), ) self.btv.key_u() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_U) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_U), ) self.btv.key_v() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_V) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_V), ) self.btv.key_w() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_W) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_W), ) self.btv.key_x() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_X) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_X), ) self.btv.key_y() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_Y) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_Y), ) self.btv.key_z() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_Z) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_Z), ) self.btv.power() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_POWER) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_POWER), ) self.btv.sleep() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_SLEEP) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_SLEEP), ) self.btv.home() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_HOME) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_HOME), ) self.btv.up() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_UP) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_UP), ) self.btv.down() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_DOWN) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_DOWN), ) self.btv.left() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_LEFT) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_LEFT), ) self.btv.right() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_RIGHT) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_RIGHT), ) self.btv.enter() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_ENTER) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_ENTER), ) self.btv.back() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_BACK) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_BACK), ) self.btv.menu() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_MENU) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_MENU), ) self.btv.mute_volume() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_MUTE) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_MUTE), ) self.btv.media_play() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_PLAY) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_PLAY), ) self.btv.media_pause() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_PAUSE) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_PAUSE), ) self.btv.media_play_pause() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_PLAY_PAUSE) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_PLAY_PAUSE), ) self.btv.media_stop() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_STOP) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_STOP), ) self.btv.media_next_track() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_NEXT) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_NEXT), ) self.btv.media_previous_track() self.assertEqual( - getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, "input keyevent {}".format(constants.KEY_PREVIOUS) + getattr(self.btv._adb, self.ADB_ATTR).shell_cmd, + "input keyevent {}".format(constants.KEY_PREVIOUS), ) def test_get_device_properties(self): """Check that ``get_device_properties`` works correctly.""" with patch.object( - self.btv._adb, "shell", side_effect=(DEVICE_PROPERTIES_OUTPUT1, ETHMAC_OUTPUT1, WIFIMAC_OUTPUT1) + self.btv._adb, + "shell", + side_effect=(DEVICE_PROPERTIES_OUTPUT1, ETHMAC_OUTPUT1, WIFIMAC_OUTPUT1), ): device_properties = self.btv.get_device_properties() self.assertDictEqual(DEVICE_PROPERTIES_DICT1, device_properties) with patch.object( - self.btv._adb, "shell", side_effect=(DEVICE_PROPERTIES_OUTPUT2, ETHMAC_OUTPUT1, WIFIMAC_OUTPUT1) + self.btv._adb, + "shell", + side_effect=(DEVICE_PROPERTIES_OUTPUT2, ETHMAC_OUTPUT1, WIFIMAC_OUTPUT1), ): device_properties = self.btv.get_device_properties() self.assertDictEqual(DEVICE_PROPERTIES_DICT2, device_properties) with patch.object( - self.btv._adb, "shell", side_effect=(DEVICE_PROPERTIES_OUTPUT3, ETHMAC_OUTPUT3, WIFIMAC_OUTPUT3) + self.btv._adb, + "shell", + side_effect=(DEVICE_PROPERTIES_OUTPUT3, ETHMAC_OUTPUT3, WIFIMAC_OUTPUT3), ): device_properties = self.btv.get_device_properties() self.assertDictEqual(DEVICE_PROPERTIES_DICT3, device_properties) @@ -487,7 +565,9 @@ def test_get_device_properties(self): self.assertDictEqual({"ethmac": None, "wifimac": None}, device_properties) with patch.object( - self.btv._adb, "shell", side_effect=(DEVICE_PROPERTIES_GOOGLE_TV, ETHMAC_GOOGLE, WIFIMAC_GOOGLE) + self.btv._adb, + "shell", + side_effect=(DEVICE_PROPERTIES_GOOGLE_TV, ETHMAC_GOOGLE, WIFIMAC_GOOGLE), ): self.btv = AndroidTVSync.from_base(self.btv) device_properties = self.btv.get_device_properties() @@ -496,12 +576,18 @@ def test_get_device_properties(self): self.assertEqual(self.btv.device_properties["manufacturer"], "Google") self.assertEqual(self.btv._cmd_current_app(), constants.CMD_CURRENT_APP_GOOGLE_TV) self.assertEqual( - self.btv._cmd_current_app_media_session_state(), constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV + self.btv._cmd_current_app_media_session_state(), + constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV, + ) + self.assertEqual( + self.btv._cmd_launch_app("TEST"), + constants.CMD_LAUNCH_APP_GOOGLE_TV.format("TEST"), ) - self.assertEqual(self.btv._cmd_launch_app("TEST"), constants.CMD_LAUNCH_APP_GOOGLE_TV.format("TEST")) with patch.object( - self.btv._adb, "shell", side_effect=(DEVICE_PROPERTIES_OUTPUT_SONY_TV, ETHMAC_SONY, WIFIMAC_SONY) + self.btv._adb, + "shell", + side_effect=(DEVICE_PROPERTIES_OUTPUT_SONY_TV, ETHMAC_SONY, WIFIMAC_SONY), ): device_properties = self.btv.get_device_properties() self.assertDictEqual(DEVICE_PROPERTIES_DICT_SONY_TV, device_properties) @@ -509,7 +595,11 @@ def test_get_device_properties(self): with patch.object( self.btv._adb, "shell", - side_effect=(DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11, ETHMAC_SHIELD_TV_11, WIFIMAC_SHIELD_TV_11), + side_effect=( + DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11, + ETHMAC_SHIELD_TV_11, + WIFIMAC_SHIELD_TV_11, + ), ): self.btv = AndroidTVSync.from_base(self.btv) device_properties = self.btv.get_device_properties() @@ -518,16 +608,88 @@ def test_get_device_properties(self): self.assertDictEqual(DEVICE_PROPERTIES_DICT_SHIELD_TV_11, device_properties) # _cmd_audio_state self.assertEqual(self.btv._cmd_audio_state(), constants.CMD_AUDIO_STATE11) + # _cmd_volume_set + self.assertEqual(self.btv._cmd_volume_set(), constants.CMD_VOLUME_SET_COMMAND11) # _cmd_current_app self.assertEqual(self.btv._cmd_current_app(), constants.CMD_CURRENT_APP11) # _cmd_current_app_media_session_state self.assertEqual( - self.btv._cmd_current_app_media_session_state(), constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11 + self.btv._cmd_current_app_media_session_state(), + constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11, + ) + # _cmd_hdmi_input + self.assertEqual(self.btv._cmd_hdmi_input(), constants.CMD_HDMI_INPUT11) + # _cmd_launch_app + self.assertEqual( + self.btv._cmd_launch_app("TEST"), + constants.CMD_LAUNCH_APP11.format("TEST"), + ) + + with patch.object( + self.btv._adb, + "shell", + side_effect=( + DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11, + ETHMAC_SHIELD_TV_11, + WIFIMAC_SHIELD_TV_11, + ), + ): + self.btv = AndroidTVSync.from_base(self.btv) + device_properties = self.btv.get_device_properties() + assert self.btv.device_properties.get("sw_version", "") == "12" + assert self.btv.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + self.assertDictEqual(DEVICE_PROPERTIES_DICT_SHIELD_TV_12, device_properties) + # _cmd_audio_state + self.assertEqual(self.btv._cmd_audio_state(), constants.CMD_AUDIO_STATE11) + # _cmd_volume_set + self.assertEqual(self.btv._cmd_volume_set(), constants.CMD_VOLUME_SET_COMMAND11) + # _cmd_current_app + self.assertEqual(self.btv._cmd_current_app(), constants.CMD_CURRENT_APP12) + # _cmd_current_app_media_session_state + self.assertEqual( + self.btv._cmd_current_app_media_session_state(), + constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE12, ) # _cmd_hdmi_input self.assertEqual(self.btv._cmd_hdmi_input(), constants.CMD_HDMI_INPUT11) # _cmd_launch_app - self.assertEqual(self.btv._cmd_launch_app("TEST"), constants.CMD_LAUNCH_APP11.format("TEST")) + self.assertEqual( + self.btv._cmd_launch_app("TEST"), + constants.CMD_LAUNCH_APP12.format("TEST"), + ) + + with patch.object( + self.btv._adb, + "shell", + side_effect=( + DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11, + ETHMAC_SHIELD_TV_11, + WIFIMAC_SHIELD_TV_11, + ), + ): + self.btv = AndroidTVSync.from_base(self.btv) + device_properties = self.btv.get_device_properties() + assert self.btv.device_properties.get("sw_version", "") == "13" + assert self.btv.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV + self.assertDictEqual(DEVICE_PROPERTIES_DICT_SHIELD_TV_13, device_properties) + # _cmd_audio_state + self.assertEqual(self.btv._cmd_audio_state(), constants.CMD_AUDIO_STATE11) + # _cmd_volume_set + self.assertEqual(self.btv._cmd_volume_set(), constants.CMD_VOLUME_SET_COMMAND11) + # _cmd_current_app + self.assertEqual(self.btv._cmd_current_app(), constants.CMD_CURRENT_APP13) + # _cmd_current_app_media_session_state + self.assertEqual( + self.btv._cmd_current_app_media_session_state(), + constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE13, + ) + # _cmd_hdmi_input + self.assertEqual(self.btv._cmd_hdmi_input(), constants.CMD_HDMI_INPUT11) + # _cmd_launch_app + self.assertEqual( + self.btv._cmd_launch_app("TEST"), + constants.CMD_LAUNCH_APP13.format("TEST"), + ) def test_get_installed_apps(self): """ "Check that `get_installed_apps` works correctly.""" @@ -632,9 +794,12 @@ def test_screen_on_awake_wake_lock_size(self): with patchers.patch_shell("11Wake Locks: size=2")[self.PATCH_KEY]: self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (True, True, 2)) - with patchers.patch_shell(["Failed to write while dumping serviceWake Locks: size=2", "11Wake Locks: size=2"])[ - self.PATCH_KEY - ]: + with patchers.patch_shell( + [ + "Failed to write while dumping serviceWake Locks: size=2", + "11Wake Locks: size=2", + ] + )[self.PATCH_KEY]: self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (True, True, 2)) def test_state_detection_rules_validator(self): @@ -649,37 +814,114 @@ def test_state_detection_rules_validator(self): # Make sure that an error is raised when the state detection rules are invalid self.assertRaises( - TypeError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID1 + TypeError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID1, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID2 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID2, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID3 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID3, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID4 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID4, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID5 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID5, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID6 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID6, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID7 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID7, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID8 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID8, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID9 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID9, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID10 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID10, ) self.assertRaises( - KeyError, BaseTVSync, "HOST", 5555, "", "", 5037, state_detection_rules=STATE_DETECTION_RULES_INVALID11 + KeyError, + BaseTVSync, + "HOST", + 5555, + "", + "", + 5037, + state_detection_rules=STATE_DETECTION_RULES_INVALID11, ) def test_wake_lock_size(self): From 48c39484943bbf2460861a4751a7da7663b3b90f Mon Sep 17 00:00:00 2001 From: rasto Date: Tue, 10 Oct 2023 15:15:17 +0200 Subject: [PATCH 9/9] fix --- tests/test_basetv_sync.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/test_basetv_sync.py b/tests/test_basetv_sync.py index 2931b743..073d676b 100644 --- a/tests/test_basetv_sync.py +++ b/tests/test_basetv_sync.py @@ -104,6 +104,24 @@ WIFIMAC_SHIELD_TV_11 = " link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff" ETHMAC_SHIELD_TV_11 = " link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff" +DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_12 = """NVIDIA +SHIELD Android TV +0123456789012 +12 +""" + +WIFIMAC_SHIELD_TV_12 = " link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff" +ETHMAC_SHIELD_TV_12 = " link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff" + +DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_13 = """NVIDIA +SHIELD Android TV +0123456789012 +13 +""" + +WIFIMAC_SHIELD_TV_13 = " link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff" +ETHMAC_SHIELD_TV_13 = " link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff" + DEVICE_PROPERTIES_DICT_SHIELD_TV_11 = { "manufacturer": "NVIDIA", "model": "SHIELD Android TV", @@ -629,9 +647,9 @@ def test_get_device_properties(self): self.btv._adb, "shell", side_effect=( - DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11, - ETHMAC_SHIELD_TV_11, - WIFIMAC_SHIELD_TV_11, + DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_12, + ETHMAC_SHIELD_TV_12, + WIFIMAC_SHIELD_TV_12, ), ): self.btv = AndroidTVSync.from_base(self.btv) @@ -662,9 +680,9 @@ def test_get_device_properties(self): self.btv._adb, "shell", side_effect=( - DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11, - ETHMAC_SHIELD_TV_11, - WIFIMAC_SHIELD_TV_11, + DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_13, + ETHMAC_SHIELD_TV_13, + WIFIMAC_SHIELD_TV_13, ), ): self.btv = AndroidTVSync.from_base(self.btv)