diff --git a/androidtv/basetv/basetv.py b/androidtv/basetv/basetv.py index f95c9d8..45f47f9 100644 --- a/androidtv/basetv/basetv.py +++ b/androidtv/basetv/basetv.py @@ -543,15 +543,18 @@ def _screen_on_awake_wake_lock_size(output): Returns ------- - bool - Whether or not the device is on - bool - Whether or not the device is awake (screensaver is not running) + bool, None + Whether or not the device is on, or ``None`` if it could not be determined + bool, None + Whether or not the device is awake (screensaver is not running), or ``None`` if it could not be determined int, None The size of the current wake lock, or ``None`` if it could not be determined """ - if not output: + if output is None: + return None, None, None + + if output == "": return False, False, None screen_on = output[0] == "1" diff --git a/androidtv/constants.py b/androidtv/constants.py index 42731ee..942e4e0 100644 --- a/androidtv/constants.py +++ b/androidtv/constants.py @@ -274,80 +274,6 @@ + CMD_STREAM_MUSIC ) -#: Get the properties for a Fire TV device (``lazy=True, get_running_apps=True``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties` -CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS = ( - CMD_SCREEN_ON - + CMD_SUCCESS1 - + " && " - + CMD_AWAKE - + CMD_SUCCESS1 - + " && " - + CMD_WAKE_LOCK_SIZE - + " && " - + CMD_CURRENT_APP - + " && (" - + CMD_MEDIA_SESSION_STATE - + " || echo) && (" - + CMD_HDMI_INPUT - + " || echo) && " - + CMD_RUNNING_APPS_FIRETV -) - -#: Get the properties for a Fire TV device (``lazy=True, get_running_apps=False``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties` -CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS = ( - CMD_SCREEN_ON - + CMD_SUCCESS1 - + " && " - + CMD_AWAKE - + CMD_SUCCESS1 - + " && " - + CMD_WAKE_LOCK_SIZE - + " && " - + CMD_CURRENT_APP - + " && (" - + CMD_MEDIA_SESSION_STATE - + " || echo) && (" - + CMD_HDMI_INPUT - + " || echo)" -) - -#: Get the properties for a Fire TV device (``lazy=False, get_running_apps=True``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties` -CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS = ( - CMD_SCREEN_ON - + CMD_SUCCESS1_FAILURE0 - + " && " - + CMD_AWAKE - + CMD_SUCCESS1_FAILURE0 - + " && " - + CMD_WAKE_LOCK_SIZE - + " && " - + CMD_CURRENT_APP - + " && (" - + CMD_MEDIA_SESSION_STATE - + " || echo) && (" - + CMD_HDMI_INPUT - + " || echo) && " - + CMD_RUNNING_APPS_FIRETV -) - -#: Get the properties for a Fire TV device (``lazy=False, get_running_apps=False``); see :py:meth:`androidtv.firetv.firetv_sync.FireTVSync.get_properties` and :py:meth:`androidtv.firetv.firetv_async.FireTVAsync.get_properties` -CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS = ( - CMD_SCREEN_ON - + CMD_SUCCESS1_FAILURE0 - + " && " - + CMD_AWAKE - + CMD_SUCCESS1_FAILURE0 - + " && " - + CMD_WAKE_LOCK_SIZE - + " && " - + CMD_CURRENT_APP - + " && (" - + CMD_MEDIA_SESSION_STATE - + " || echo) && (" - + CMD_HDMI_INPUT - + " || echo)" -) - # `getprop` commands CMD_MANUFACTURER = "getprop ro.product.manufacturer" CMD_MODEL = "getprop ro.product.model" diff --git a/androidtv/firetv/base_firetv.py b/androidtv/firetv/base_firetv.py index 74c7536..6a64179 100644 --- a/androidtv/firetv/base_firetv.py +++ b/androidtv/firetv/base_firetv.py @@ -39,10 +39,6 @@ def __init__(self, host, port=5555, adbkey="", adb_server_ip="", adb_server_port 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 @@ -223,86 +219,3 @@ def _update(self, screen_on, awake, wake_lock_size, current_app, media_session_s state = constants.STATE_PAUSED return state, current_app, running_apps, hdmi_input - - # ======================================================================= # - # # - # Properties # - # # - # ======================================================================= # - def _get_properties(self, output, get_running_apps=True): - """Get the properties needed for Home Assistant updates. - - This will send one of the following ADB commands: - - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS` - - Parameters - ---------- - output : str, None - The output of the ADB command used to retrieve the properties - get_running_apps : bool - Whether or not to get the ``running_apps`` property - - Returns - ------- - screen_on : bool, None - Whether or not the device is on, or ``None`` if it was not determined - awake : bool, None - Whether or not the device is awake (screensaver is not running), or ``None`` if it was not determined - wake_lock_size : int, None - The size of the current wake lock, or ``None`` if it was not determined - current_app : str, None - The current app property, or ``None`` if it was not determined - media_session_state : int, None - The state from the output of ``dumpsys media_session``, or ``None`` if it was not determined - running_apps : list, None - A list of the running apps, or ``None`` if it was not determined - hdmi_input : str, None - The HDMI input, or ``None`` if it could not be determined - - """ - # ADB command was unsuccessful - if output is None: - return None, None, None, None, None, None, None - - # `screen_on` property - if not output: - return False, False, -1, None, None, None, None - screen_on = output[0] == "1" - - # `awake` property - if len(output) < 2: - return screen_on, False, -1, None, None, None, None - awake = output[1] == "1" - - lines = output.strip().splitlines() - - # `wake_lock_size` property - if len(lines[0]) < 3: - return screen_on, awake, -1, None, None, None, None - wake_lock_size = self._wake_lock_size(lines[0]) - - # `current_app` property - if len(lines) < 2: - return screen_on, awake, wake_lock_size, None, None, None, None - current_app = self._current_app(lines[1]) - - # `media_session_state` property - if len(lines) < 3: - return screen_on, awake, wake_lock_size, current_app, None, None, None - media_session_state = self._media_session_state(lines[2], current_app) - - # HDMI input property - if len(lines) < 4: - return screen_on, awake, wake_lock_size, current_app, media_session_state, None, None - hdmi_input = self._get_hdmi_input(lines[3]) - - # `running_apps` property - if not get_running_apps or len(lines) < 5: - return screen_on, awake, wake_lock_size, current_app, media_session_state, None, hdmi_input - running_apps = self._running_apps(lines[4:]) - - return screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps, hdmi_input diff --git a/androidtv/firetv/firetv_async.py b/androidtv/firetv/firetv_async.py index 4d05961..202eeeb 100644 --- a/androidtv/firetv/firetv_async.py +++ b/androidtv/firetv/firetv_async.py @@ -100,13 +100,6 @@ async def update(self, get_running_apps=True, lazy=True): async def get_properties(self, get_running_apps=True, lazy=False): """Get the properties needed for Home Assistant updates. - This will send one of the following ADB commands: - - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS` - Parameters ---------- get_running_apps : bool @@ -132,19 +125,20 @@ async def get_properties(self, get_running_apps=True, lazy=False): The HDMI input, or ``None`` if it could not be determined """ - if lazy: - if get_running_apps: - output = await self._adb.shell(self._cmd_get_properties_lazy_running_apps) - else: - output = await self._adb.shell(self._cmd_get_properties_lazy_no_running_apps) + screen_on, awake, wake_lock_size = await self.screen_on_awake_wake_lock_size() + if lazy and not (screen_on and awake): + return screen_on, awake, wake_lock_size, None, None, None, None + + current_app, media_session_state = await self.current_app_media_session_state() + + if get_running_apps: + running_apps = await self.running_apps() else: - if get_running_apps: - output = await self._adb.shell(self._cmd_get_properties_not_lazy_running_apps) - else: - 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) + running_apps = None + + hdmi_input = await self.get_hdmi_input() - return self._get_properties(output, get_running_apps) + return screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps, hdmi_input async def get_properties_dict(self, get_running_apps=True, lazy=True): """Get the properties needed for Home Assistant updates and return them as a dictionary. diff --git a/androidtv/firetv/firetv_sync.py b/androidtv/firetv/firetv_sync.py index 6bcb272..66cb194 100644 --- a/androidtv/firetv/firetv_sync.py +++ b/androidtv/firetv/firetv_sync.py @@ -100,13 +100,6 @@ def update(self, get_running_apps=True, lazy=True): def get_properties(self, get_running_apps=True, lazy=False): """Get the properties needed for Home Assistant updates. - This will send one of the following ADB commands: - - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS` - * :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS` - Parameters ---------- get_running_apps : bool @@ -132,19 +125,20 @@ def get_properties(self, get_running_apps=True, lazy=False): The HDMI input, or ``None`` if it could not be determined """ - if lazy: - if get_running_apps: - output = self._adb.shell(self._cmd_get_properties_lazy_running_apps) - else: - output = self._adb.shell(self._cmd_get_properties_lazy_no_running_apps) + screen_on, awake, wake_lock_size = self.screen_on_awake_wake_lock_size() + if lazy and not (screen_on and awake): + return screen_on, awake, wake_lock_size, None, None, None, None + + current_app, media_session_state = self.current_app_media_session_state() + + if get_running_apps: + running_apps = self.running_apps() else: - if get_running_apps: - output = self._adb.shell(self._cmd_get_properties_not_lazy_running_apps) - else: - 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) + running_apps = None + + hdmi_input = self.get_hdmi_input() - return self._get_properties(output, get_running_apps) + return screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps, hdmi_input def get_properties_dict(self, get_running_apps=True, lazy=True): """Get the properties needed for Home Assistant updates and return them as a dictionary. diff --git a/tests/test_basetv_async.py b/tests/test_basetv_async.py index a2a185c..36b84ed 100644 --- a/tests/test_basetv_async.py +++ b/tests/test_basetv_async.py @@ -408,7 +408,7 @@ async def test_screen_on(self): async def test_screen_on_awake_wake_lock_size(self): """Check that the ``screen_on_awake_wake_lock_size`` property works correctly.""" with async_patchers.patch_shell(None)[self.PATCH_KEY]: - self.assertTupleEqual(await self.btv.screen_on_awake_wake_lock_size(), (False, False, None)) + self.assertTupleEqual(await self.btv.screen_on_awake_wake_lock_size(), (None, None, None)) with async_patchers.patch_shell("")[self.PATCH_KEY]: self.assertTupleEqual(await self.btv.screen_on_awake_wake_lock_size(), (False, False, None)) diff --git a/tests/test_basetv_sync.py b/tests/test_basetv_sync.py index c782558..b6160ce 100644 --- a/tests/test_basetv_sync.py +++ b/tests/test_basetv_sync.py @@ -565,7 +565,7 @@ def test_screen_on(self): def test_screen_on_awake_wake_lock_size(self): """Check that the ``screen_on_awake_wake_lock_size`` property works correctly.""" with patchers.patch_shell(None)[self.PATCH_KEY]: - self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (False, False, None)) + self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (None, None, None)) with patchers.patch_shell("")[self.PATCH_KEY]: self.assertTupleEqual(self.btv.screen_on_awake_wake_lock_size(), (False, False, None)) diff --git a/tests/test_constants.py b/tests/test_constants.py index 04fba4f..afe9dcc 100644 --- a/tests/test_constants.py +++ b/tests/test_constants.py @@ -119,30 +119,6 @@ def test_constants(self): r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' || echo -e '0\c' && (dumpsys audio | grep paused | grep -qv 'Buffer Queue' && echo -e '1\c' || (dumpsys audio | grep started | grep -qv 'Buffer Queue' && echo '2\c' || echo '0\c')) && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && 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 {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo) && dumpsys audio | grep '\- STREAM_MUSIC:' -A 11", ) - # CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS - self.assertEqual( - constants.CMD_FIRETV_PROPERTIES_LAZY_RUNNING_APPS, - r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && 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 {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo) && ps | grep u0_a", - ) - - # CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS - self.assertEqual( - constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS, - r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && 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 {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo)", - ) - - # CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS - self.assertEqual( - constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS, - r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && 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 {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo) && ps | grep u0_a", - ) - - # CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS - self.assertEqual( - constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS, - r"(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' || echo -e '0\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && 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 {' || echo) && (dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]' || echo)", - ) - def test_current_app_extraction_atv_launcher(self): dumpsys_output = """ mCurrentFocus=Window{e74bb23 u0 com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity} diff --git a/tests/test_firetv_async.py b/tests/test_firetv_async.py index e1e5154..90fc000 100644 --- a/tests/test_firetv_async.py +++ b/tests/test_firetv_async.py @@ -24,9 +24,6 @@ async def setUp(self): ], async_patchers.patch_shell("")[self.PATCH_KEY]: self.ftv = FireTVAsync("HOST", 5555) await self.ftv.adb_connect() - self.assertEqual( - self.ftv._cmd_get_properties_lazy_no_running_apps, constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS - ) @awaiter async def test_turn_on_off(self): @@ -86,17 +83,64 @@ async def test_running_apps(self): async def test_get_properties(self): """Check that ``get_properties()`` works correctly.""" with async_patchers.patch_shell(None)[self.PATCH_KEY]: - for get_running_apps in [True, False]: - for lazy in [True, False]: - with patch_calls(self.ftv, self.ftv._get_properties) as patched: - await self.ftv.get_properties_dict(get_running_apps, lazy) - assert patched.called + with patch_calls( + self.ftv, self.ftv.screen_on_awake_wake_lock_size + ) as screen_on_awake_wake_lock_size, patch_calls( + self.ftv, self.ftv.current_app_media_session_state + ) as current_app_media_session_state, patch_calls( + self.ftv, self.ftv.running_apps + ) as running_apps, patch_calls( + self.ftv, self.ftv.get_hdmi_input + ) as get_hdmi_input: + await self.ftv.get_properties(lazy=True) + assert screen_on_awake_wake_lock_size.called + assert not current_app_media_session_state.called + assert not running_apps.called + assert not get_hdmi_input.called + + with patch_calls( + self.ftv, self.ftv.screen_on_awake_wake_lock_size + ) as screen_on_awake_wake_lock_size, patch_calls( + self.ftv, self.ftv.current_app_media_session_state + ) as current_app_media_session_state, patch_calls( + self.ftv, self.ftv.running_apps + ) as running_apps, patch_calls( + self.ftv, self.ftv.get_hdmi_input + ) as get_hdmi_input: + await self.ftv.get_properties(lazy=False, get_running_apps=True) + assert screen_on_awake_wake_lock_size.called + assert current_app_media_session_state.called + assert running_apps.called + assert get_hdmi_input.called + + with patch_calls( + self.ftv, self.ftv.screen_on_awake_wake_lock_size + ) as screen_on_awake_wake_lock_size, patch_calls( + self.ftv, self.ftv.current_app_media_session_state + ) as current_app_media_session_state, patch_calls( + self.ftv, self.ftv.running_apps + ) as running_apps, patch_calls( + self.ftv, self.ftv.get_hdmi_input + ) as get_hdmi_input: + await self.ftv.get_properties(lazy=False, get_running_apps=False) + assert screen_on_awake_wake_lock_size.called + assert current_app_media_session_state.called + assert not running_apps.called + assert get_hdmi_input.called + + @awaiter + async def test_get_properties_dict(self): + """Check that ``get_properties_dict()`` works correctly.""" + with async_patchers.patch_shell(None)[self.PATCH_KEY]: + with patch_calls(self.ftv, self.ftv.get_properties) as get_properties: + await self.ftv.get_properties_dict() + assert get_properties.called @awaiter async def test_update(self): """Check that the ``update`` method works correctly.""" with async_patchers.patch_shell(None)[self.PATCH_KEY]: - with patch_calls(self.ftv, self.ftv._get_properties) as patched: + with patch_calls(self.ftv, self.ftv.get_properties) as patched: await self.ftv.update() assert patched.called diff --git a/tests/test_firetv_sync.py b/tests/test_firetv_sync.py index cc86c10..7dc61c0 100644 --- a/tests/test_firetv_sync.py +++ b/tests/test_firetv_sync.py @@ -25,152 +25,6 @@ RUNNING_APPS_LIST = ["com.netflix.ninja", "com.amazon.device.controllermanager"] -GET_PROPERTIES_OUTPUT1 = "" -GET_PROPERTIES_DICT1 = { - "screen_on": False, - "awake": False, - "wake_lock_size": -1, - "current_app": None, - "media_session_state": None, - "running_apps": None, - "hdmi_input": None, -} -STATE1 = (constants.STATE_OFF, None, None, None) - -GET_PROPERTIES_OUTPUT2 = "1" -GET_PROPERTIES_DICT2 = { - "screen_on": True, - "awake": False, - "wake_lock_size": -1, - "current_app": None, - "media_session_state": None, - "running_apps": None, - "hdmi_input": None, -} -STATE2 = (constants.STATE_STANDBY, None, None, None) - -GET_PROPERTIES_OUTPUT3 = """11Wake Locks: size=2 -com.amazon.tv.launcher - - -u0_a2 17243 197 998628 24932 ffffffff 00000000 S com.amazon.device.controllermanager -u0_a2 17374 197 995368 20764 ffffffff 00000000 S com.amazon.device.controllermanager:BluetoothReceiver""" -GET_PROPERTIES_DICT3 = { - "screen_on": True, - "awake": True, - "wake_lock_size": 2, - "current_app": "com.amazon.tv.launcher", - "media_session_state": None, - "running_apps": ["com.amazon.device.controllermanager", "com.amazon.device.controllermanager:BluetoothReceiver"], - "hdmi_input": None, -} -STATE3 = ( - constants.STATE_IDLE, - "com.amazon.tv.launcher", - ["com.amazon.device.controllermanager", "com.amazon.device.controllermanager:BluetoothReceiver"], - None, -) - -GET_PROPERTIES_OUTPUT3A = GET_PROPERTIES_OUTPUT3[0] -GET_PROPERTIES_OUTPUT3B = GET_PROPERTIES_OUTPUT3[:2] -GET_PROPERTIES_OUTPUT3C = GET_PROPERTIES_OUTPUT3.splitlines()[0] -GET_PROPERTIES_OUTPUT3D = "\n".join(GET_PROPERTIES_OUTPUT3.splitlines()[:2]) -GET_PROPERTIES_OUTPUT3E = "\n".join(GET_PROPERTIES_OUTPUT3.splitlines()[:3]) -GET_PROPERTIES_OUTPUT3F = "\n".join(GET_PROPERTIES_OUTPUT3.splitlines()[:4]) + "HW2" - -GET_PROPERTIES_DICT3A = { - "screen_on": True, - "awake": False, - "wake_lock_size": -1, - "current_app": None, - "media_session_state": None, - "running_apps": None, - "hdmi_input": None, -} -GET_PROPERTIES_DICT3B = { - "screen_on": True, - "awake": True, - "wake_lock_size": -1, - "current_app": None, - "media_session_state": None, - "running_apps": None, - "hdmi_input": None, -} -GET_PROPERTIES_DICT3C = { - "screen_on": True, - "awake": True, - "wake_lock_size": 2, - "current_app": None, - "media_session_state": None, - "running_apps": None, - "hdmi_input": None, -} -GET_PROPERTIES_DICT3D = { - "screen_on": True, - "awake": True, - "wake_lock_size": 2, - "current_app": "com.amazon.tv.launcher", - "media_session_state": None, - "running_apps": None, - "hdmi_input": None, -} -GET_PROPERTIES_DICT3E = { - "screen_on": True, - "awake": True, - "wake_lock_size": 2, - "current_app": "com.amazon.tv.launcher", - "media_session_state": None, - "running_apps": None, - "hdmi_input": None, -} -GET_PROPERTIES_DICT3F = { - "screen_on": True, - "awake": True, - "wake_lock_size": 2, - "current_app": "com.amazon.tv.launcher", - "media_session_state": None, - "running_apps": None, - "hdmi_input": "HW2", -} - -GET_PROPERTIES_OUTPUT4 = """11Wake Locks: size=2 -com.amazon.tv.launcher -state=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null}""" -GET_PROPERTIES_DICT4 = { - "screen_on": True, - "awake": True, - "wake_lock_size": 2, - "current_app": "com.amazon.tv.launcher", - "media_session_state": 2, - "running_apps": None, - "hdmi_input": None, -} - -GET_PROPERTIES_OUTPUT5 = """11Wake Locks: size=2 -com.amazon.tv.launcher -state=PlaybackState {state=2, position=0, buffered position=0, speed=0.0, updated=65749, actions=240640, custom actions=[], active item id=-1, error=null} - -u0_a2 17243 197 998628 24932 ffffffff 00000000 S com.amazon.device.controllermanager -u0_a2 17374 197 995368 20764 ffffffff 00000000 S com.amazon.device.controllermanager:BluetoothReceiver""" -GET_PROPERTIES_DICT5 = { - "screen_on": True, - "awake": True, - "wake_lock_size": 2, - "current_app": "com.amazon.tv.launcher", - "media_session_state": 2, - "running_apps": ["com.amazon.device.controllermanager", "com.amazon.device.controllermanager:BluetoothReceiver"], - "hdmi_input": None, -} - -GET_PROPERTIES_DICT_NONE = { - "screen_on": None, - "awake": None, - "wake_lock_size": None, - "media_session_state": None, - "current_app": None, - "running_apps": None, - "hdmi_input": None, -} STATE_NONE = (None, None, None, None) STATE_DETECTION_RULES1 = {"com.amazon.tv.launcher": ["off"]} @@ -190,9 +44,6 @@ def setUp(self): ]: self.ftv = FireTVSync("HOST", 5555) self.ftv.adb_connect() - self.assertEqual( - self.ftv._cmd_get_properties_lazy_no_running_apps, constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS - ) def test_turn_on_off(self): """Test that the ``FireTVSync.turn_on`` and ``FireTVSync.turn_off`` methods work correctly.""" @@ -254,114 +105,64 @@ def test_running_apps(self): def test_get_properties(self): """Check that ``get_properties()`` works correctly.""" with patchers.patch_shell(None)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT_NONE) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT1) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT2)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT2) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3A)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3A) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3B)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3B) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3C)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3C) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3D)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3D) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3E) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True, get_running_apps=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3E) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3E)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=False, get_running_apps=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3E) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3F)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT3F) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT4) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT4)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True, get_running_apps=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT4) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT5)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=True) - self.assertDictEqual(properties, GET_PROPERTIES_DICT5) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT5)[self.PATCH_KEY]: - properties = self.ftv.get_properties_dict(lazy=False) - self.assertDictEqual(properties, GET_PROPERTIES_DICT5) + with patchers.patch_calls( + self.ftv, self.ftv.screen_on_awake_wake_lock_size + ) as screen_on_awake_wake_lock_size, patchers.patch_calls( + self.ftv, self.ftv.current_app_media_session_state + ) as current_app_media_session_state, patchers.patch_calls( + self.ftv, self.ftv.running_apps + ) as running_apps, patchers.patch_calls( + self.ftv, self.ftv.get_hdmi_input + ) as get_hdmi_input: + self.ftv.get_properties(lazy=True) + assert screen_on_awake_wake_lock_size.called + assert not current_app_media_session_state.called + assert not running_apps.called + assert not get_hdmi_input.called + + with patchers.patch_calls( + self.ftv, self.ftv.screen_on_awake_wake_lock_size + ) as screen_on_awake_wake_lock_size, patchers.patch_calls( + self.ftv, self.ftv.current_app_media_session_state + ) as current_app_media_session_state, patchers.patch_calls( + self.ftv, self.ftv.running_apps + ) as running_apps, patchers.patch_calls( + self.ftv, self.ftv.get_hdmi_input + ) as get_hdmi_input: + self.ftv.get_properties(lazy=False, get_running_apps=True) + assert screen_on_awake_wake_lock_size.called + assert current_app_media_session_state.called + assert running_apps.called + assert get_hdmi_input.called + + with patchers.patch_calls( + self.ftv, self.ftv.screen_on_awake_wake_lock_size + ) as screen_on_awake_wake_lock_size, patchers.patch_calls( + self.ftv, self.ftv.current_app_media_session_state + ) as current_app_media_session_state, patchers.patch_calls( + self.ftv, self.ftv.running_apps + ) as running_apps, patchers.patch_calls( + self.ftv, self.ftv.get_hdmi_input + ) as get_hdmi_input: + self.ftv.get_properties(lazy=False, get_running_apps=False) + assert screen_on_awake_wake_lock_size.called + assert current_app_media_session_state.called + assert not running_apps.called + assert get_hdmi_input.called + + def test_get_properties_dict(self): + """Check that ``get_properties_dict()`` works correctly.""" + with patchers.patch_shell(None)[self.PATCH_KEY]: + with patchers.patch_calls(self.ftv, self.ftv.get_properties) as get_properties: + self.ftv.get_properties_dict() + assert get_properties.called def test_update(self): """Check that the ``update`` method works correctly.""" with patchers.patch_connect(False)[self.PATCH_KEY]: self.ftv.adb_connect() - state = self.ftv.update() - self.assertTupleEqual(state, STATE_NONE) - - with patchers.patch_connect(True)[self.PATCH_KEY]: - self.assertTrue(self.ftv.adb_connect()) - - with patchers.patch_shell(None)[self.PATCH_KEY]: - state = self.ftv.update() - self.assertTupleEqual(state, STATE_NONE) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT1)[self.PATCH_KEY]: - state = self.ftv.update() - self.assertTupleEqual(state, STATE1) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT2)[self.PATCH_KEY]: - state = self.ftv.update() - self.assertTupleEqual(state, STATE2) - - with patchers.patch_shell(GET_PROPERTIES_OUTPUT3)[self.PATCH_KEY]: - state = self.ftv.update() - self.assertTupleEqual(state, STATE3) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES1 - state = self.ftv.update() - self.assertEqual(state[0], constants.STATE_OFF) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES2 - state = self.ftv.update() - self.assertEqual(state[0], constants.STATE_OFF) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES3 - state = self.ftv.update() - self.assertEqual(state[0], constants.STATE_STANDBY) - - self.ftv._state_detection_rules = STATE_DETECTION_RULES4 - state = self.ftv.update() - self.assertEqual(state[0], constants.STATE_PAUSED) - self.ftv._state_detection_rules = STATE_DETECTION_RULES5 - state = self.ftv.update() - self.assertEqual(state[0], constants.STATE_IDLE) + self.assertTupleEqual(self.ftv.update(), STATE_NONE) def assertUpdate(self, get_properties, update): """Check that the results of the `update` method are as expected."""