From 0285a9897d200df930abbda7a884db45dd159695 Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Mon, 4 May 2020 08:01:54 -0700 Subject: [PATCH 1/2] Convert properties that do I/O into methods --- androidtv/androidtv.py | 1 - androidtv/basetv.py | 42 ++++++++++++------------------- androidtv/firetv.py | 1 - tests/test_androidtv.py | 56 ++++++++++++++++++++--------------------- tests/test_basetv.py | 44 ++++++++++++++++---------------- tests/test_firetv.py | 6 ++--- 6 files changed, 69 insertions(+), 81 deletions(-) diff --git a/androidtv/androidtv.py b/androidtv/androidtv.py index b0601a72..085bd066 100644 --- a/androidtv/androidtv.py +++ b/androidtv/androidtv.py @@ -331,7 +331,6 @@ def get_properties_dict(self, get_running_apps=True, lazy=True): # Properties # # # # ======================================================================= # - @property def running_apps(self): """Return a list of running user applications. diff --git a/androidtv/basetv.py b/androidtv/basetv.py index 77f1fa09..8063c4de 100644 --- a/androidtv/basetv.py +++ b/androidtv/basetv.py @@ -104,6 +104,18 @@ def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port # ADB methods # # # # ======================================================================= # + @property + def available(self): + """Whether the ADB connection is intact. + + Returns + ------- + bool + Whether or not the ADB connection is intact + + """ + return self._adb.available + def adb_shell(self, cmd): """Send an ADB command. @@ -356,7 +368,6 @@ def _conditions_are_true(conditions, media_session_state=None, wake_lock_size=No # Properties # # # # ======================================================================= # - @property def audio_output_device(self): """Get the current audio playback device. @@ -370,7 +381,6 @@ def audio_output_device(self): return self._audio_output_device(stream_music) - @property def audio_state(self): """Check if audio is playing, paused, or idle. @@ -383,19 +393,6 @@ def audio_state(self): audio_state_response = self._adb.shell(constants.CMD_AUDIO_STATE) return self._audio_state(audio_state_response) - @property - def available(self): - """Check whether the ADB connection is intact. - - Returns - ------- - bool - Whether or not the ADB connection is intact - - """ - return self._adb.available - - @property def awake(self): """Check if the device is awake (screensaver is not running). @@ -407,7 +404,6 @@ def awake(self): """ return self._adb.shell(constants.CMD_AWAKE + constants.CMD_SUCCESS1_FAILURE0) == '1' - @property def current_app(self): """Return the current app. @@ -421,7 +417,6 @@ def current_app(self): return self._current_app(current_app_response) - @property def is_volume_muted(self): """Whether or not the volume is muted. @@ -435,7 +430,6 @@ def is_volume_muted(self): return self._is_volume_muted(stream_music) - @property def media_session_state(self): """Get the state from the output of ``dumpsys media_session``. @@ -451,7 +445,6 @@ def media_session_state(self): return media_session_state - @property def screen_on(self): """Check if the screen is on. @@ -463,7 +456,6 @@ def screen_on(self): """ return self._adb.shell(constants.CMD_SCREEN_ON + constants.CMD_SUCCESS1_FAILURE0) == '1' - @property def volume(self): """Get the absolute volume level. @@ -478,7 +470,6 @@ def volume(self): return self._volume(stream_music, audio_output_device) - @property def volume_level(self): """Get the relative volume level. @@ -488,11 +479,10 @@ def volume_level(self): The volume level (between 0 and 1), or ``None`` if it could not be determined """ - volume = self.volume + volume = self.volume() return self._volume_level(volume) - @property def wake_lock_size(self): """Get the size of the current wake lock. @@ -1120,7 +1110,7 @@ def set_volume_level(self, volume_level): """ # if necessary, determine the max volume if not self.max_volume: - _ = self.volume + _ = self.volume() if not self.max_volume: return None @@ -1146,7 +1136,7 @@ def volume_up(self, current_volume_level=None): """ if current_volume_level is None or not self.max_volume: - current_volume = self.volume + current_volume = self.volume() else: current_volume = round(self.max_volume * current_volume_level) @@ -1175,7 +1165,7 @@ def volume_down(self, current_volume_level=None): """ if current_volume_level is None or not self.max_volume: - current_volume = self.volume + current_volume = self.volume() else: current_volume = round(self.max_volume * current_volume_level) diff --git a/androidtv/firetv.py b/androidtv/firetv.py index e1f5e02a..a53a87f6 100644 --- a/androidtv/firetv.py +++ b/androidtv/firetv.py @@ -323,7 +323,6 @@ def get_properties_dict(self, get_running_apps=True, lazy=True): # Properties # # # # ======================================================================= # - @property def running_apps(self): """Return a list of running user applications. diff --git a/tests/test_androidtv.py b/tests/test_androidtv.py index f669a429..99c5e8cd 100644 --- a/tests/test_androidtv.py +++ b/tests/test_androidtv.py @@ -316,15 +316,15 @@ def test_running_apps(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - running_apps = self.atv.running_apps + running_apps = self.atv.running_apps() self.assertIsNone(running_apps, None) with patchers.patch_shell('')[self.PATCH_KEY]: - running_apps = self.atv.running_apps + running_apps = self.atv.running_apps() self.assertIsNone(running_apps, None) with patchers.patch_shell(RUNNING_APPS_OUTPUT)[self.PATCH_KEY]: - running_apps = self.atv.running_apps + running_apps = self.atv.running_apps() self.assertListEqual(running_apps, RUNNING_APPS_LIST) def test_audio_output_device(self): @@ -332,27 +332,27 @@ def test_audio_output_device(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - audio_output_device = self.atv.audio_output_device + audio_output_device = self.atv.audio_output_device() self.assertIsNone(audio_output_device) with patchers.patch_shell('')[self.PATCH_KEY]: - audio_output_device = self.atv.audio_output_device + audio_output_device = self.atv.audio_output_device() self.assertIsNone(audio_output_device) with patchers.patch_shell(' ')[self.PATCH_KEY]: - audio_output_device = self.atv.audio_output_device + audio_output_device = self.atv.audio_output_device() self.assertIsNone(audio_output_device) with patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - audio_output_device = self.atv.audio_output_device + audio_output_device = self.atv.audio_output_device() self.assertIsNone(audio_output_device) with patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - audio_output_device = self.atv.audio_output_device + audio_output_device = self.atv.audio_output_device() self.assertEqual('speaker', audio_output_device) with patchers.patch_shell(STREAM_MUSIC_ON)[self.PATCH_KEY]: - audio_output_device = self.atv.audio_output_device + audio_output_device = self.atv.audio_output_device() self.assertEqual('hmdi_arc', audio_output_device) def test_volume(self): @@ -360,34 +360,34 @@ def test_volume(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - volume = self.atv.volume + volume = self.atv.volume() self.assertIsNone(volume) with patchers.patch_shell('')[self.PATCH_KEY]: - volume = self.atv.volume + volume = self.atv.volume() self.assertIsNone(volume) with patchers.patch_shell(' ')[self.PATCH_KEY]: - volume = self.atv.volume + volume = self.atv.volume() self.assertIsNone(volume) with patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - volume = self.atv.volume + volume = self.atv.volume() self.assertIsNone(volume) with patchers.patch_shell(STREAM_MUSIC_NO_VOLUME)[self.PATCH_KEY]: - volume = self.atv.volume + volume = self.atv.volume() self.assertIsNone(volume) self.atv.max_volume = None with patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - volume = self.atv.volume + volume = self.atv.volume() self.assertEqual(volume, 20) self.assertEqual(self.atv.max_volume, 60.) self.atv.max_volume = None with patchers.patch_shell(STREAM_MUSIC_ON)[self.PATCH_KEY]: - volume = self.atv.volume + volume = self.atv.volume() self.assertEqual(volume, 22) self.assertEqual(self.atv.max_volume, 60.) @@ -396,34 +396,34 @@ def test_volume_level(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - volume_level = self.atv.volume_level + volume_level = self.atv.volume_level() self.assertIsNone(volume_level) with patchers.patch_shell('')[self.PATCH_KEY]: - volume_level = self.atv.volume_level + volume_level = self.atv.volume_level() self.assertIsNone(volume_level) with patchers.patch_shell(' ')[self.PATCH_KEY]: - volume_level = self.atv.volume_level + volume_level = self.atv.volume_level() self.assertIsNone(volume_level) with patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - volume_level = self.atv.volume_level + volume_level = self.atv.volume_level() self.assertIsNone(volume_level) with patchers.patch_shell(STREAM_MUSIC_NO_VOLUME)[self.PATCH_KEY]: - volume_level = self.atv.volume_level + volume_level = self.atv.volume_level() self.assertIsNone(volume_level) self.atv.max_volume = None with patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - volume_level = self.atv.volume_level + volume_level = self.atv.volume_level() self.assertEqual(volume_level, 20./60) self.assertEqual(self.atv.max_volume, 60.) self.atv.max_volume = None with patchers.patch_shell(STREAM_MUSIC_ON)[self.PATCH_KEY]: - volume_level = self.atv.volume_level + volume_level = self.atv.volume_level() self.assertEqual(volume_level, 22./60) self.assertEqual(self.atv.max_volume, 60.) @@ -432,23 +432,23 @@ def test_is_volume_muted(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - is_volume_muted = self.atv.is_volume_muted + is_volume_muted = self.atv.is_volume_muted() self.assertIsNone(is_volume_muted) with patchers.patch_shell('')[self.PATCH_KEY]: - is_volume_muted = self.atv.is_volume_muted + is_volume_muted = self.atv.is_volume_muted() self.assertIsNone(is_volume_muted) with patchers.patch_shell(' ')[self.PATCH_KEY]: - is_volume_muted = self.atv.is_volume_muted + is_volume_muted = self.atv.is_volume_muted() self.assertIsNone(is_volume_muted) with patchers.patch_shell(STREAM_MUSIC_EMPTY)[self.PATCH_KEY]: - is_volume_muted = self.atv.is_volume_muted + is_volume_muted = self.atv.is_volume_muted() self.assertIsNone(is_volume_muted) with patchers.patch_shell(STREAM_MUSIC_OFF)[self.PATCH_KEY]: - is_volume_muted = self.atv.is_volume_muted + is_volume_muted = self.atv.is_volume_muted() self.assertFalse(is_volume_muted) def test_set_volume_level(self): diff --git a/tests/test_basetv.py b/tests/test_basetv.py index e21a23cf..3546d037 100644 --- a/tests/test_basetv.py +++ b/tests/test_basetv.py @@ -324,32 +324,32 @@ def test_awake(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - self.assertFalse(self.btv.awake) + self.assertFalse(self.btv.awake()) with patchers.patch_shell('0')[self.PATCH_KEY]: - self.assertFalse(self.btv.awake) + self.assertFalse(self.btv.awake()) with patchers.patch_shell('1')[self.PATCH_KEY]: - self.assertTrue(self.btv.awake) + self.assertTrue(self.btv.awake()) def test_audio_state(self): """Check that the ``audio_state`` property works correctly. """ with patchers.patch_shell(None)[self.PATCH_KEY]: - audio_state = self.btv.audio_state + audio_state = self.btv.audio_state() self.assertIsNone(audio_state, None) with patchers.patch_shell('0')[self.PATCH_KEY]: - audio_state = self.btv.audio_state + audio_state = self.btv.audio_state() self.assertEqual(audio_state, constants.STATE_IDLE) with patchers.patch_shell('1')[self.PATCH_KEY]: - audio_state = self.btv.audio_state + audio_state = self.btv.audio_state() self.assertEqual(audio_state, constants.STATE_PAUSED) with patchers.patch_shell('2')[self.PATCH_KEY]: - audio_state = self.btv.audio_state + audio_state = self.btv.audio_state() self.assertEqual(audio_state, constants.STATE_PLAYING) def test_current_app(self): @@ -357,15 +357,15 @@ def test_current_app(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - current_app = self.btv.current_app + current_app = self.btv.current_app() self.assertIsNone(current_app, None) with patchers.patch_shell('')[self.PATCH_KEY]: - current_app = self.btv.current_app + current_app = self.btv.current_app() self.assertIsNone(current_app, None) with patchers.patch_shell('com.amazon.tv.launcher')[self.PATCH_KEY]: - current_app = self.btv.current_app + current_app = self.btv.current_app() self.assertEqual(current_app, "com.amazon.tv.launcher") def test_media_session_state(self): @@ -373,23 +373,23 @@ def test_media_session_state(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - media_session_state = self.btv.media_session_state + media_session_state = self.btv.media_session_state() self.assertIsNone(media_session_state, None) with patchers.patch_shell('')[self.PATCH_KEY]: - media_session_state = self.btv.media_session_state + media_session_state = self.btv.media_session_state() self.assertIsNone(media_session_state, None) with patchers.patch_shell('unknown.app\n ')[self.PATCH_KEY]: - media_session_state = self.btv.media_session_state + media_session_state = self.btv.media_session_state() self.assertIsNone(media_session_state, None) with patchers.patch_shell('2')[self.PATCH_KEY]: - media_session_state = self.btv.media_session_state + media_session_state = self.btv.media_session_state() self.assertIsNone(media_session_state, None) with patchers.patch_shell(MEDIA_SESSION_STATE_OUTPUT)[self.PATCH_KEY]: - media_session_state = self.btv.media_session_state + media_session_state = self.btv.media_session_state() self.assertEqual(media_session_state, 2) def test_screen_on(self): @@ -397,13 +397,13 @@ def test_screen_on(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - self.assertFalse(self.btv.screen_on) + self.assertFalse(self.btv.screen_on()) with patchers.patch_shell('0')[self.PATCH_KEY]: - self.assertFalse(self.btv.screen_on) + self.assertFalse(self.btv.screen_on()) with patchers.patch_shell('1')[self.PATCH_KEY]: - self.assertTrue(self.btv.screen_on) + self.assertTrue(self.btv.screen_on()) def test_state_detection_rules_validator(self): """Check that the ``state_detection_rules_validator`` function works correctly. @@ -435,16 +435,16 @@ def test_wake_lock_size(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - self.assertIsNone(self.btv.wake_lock_size) + self.assertIsNone(self.btv.wake_lock_size()) with patchers.patch_shell('')[self.PATCH_KEY]: - self.assertIsNone(self.btv.wake_lock_size) + self.assertIsNone(self.btv.wake_lock_size()) with patchers.patch_shell('Wake Locks: size=2')[self.PATCH_KEY]: - self.assertEqual(self.btv.wake_lock_size, 2) + self.assertEqual(self.btv.wake_lock_size(), 2) with patchers.patch_shell('INVALID')[self.PATCH_KEY]: - self.assertIsNone(self.btv.wake_lock_size) + self.assertIsNone(self.btv.wake_lock_size()) class TestHAStateDetectionRulesValidator(unittest.TestCase): diff --git a/tests/test_firetv.py b/tests/test_firetv.py index bc5fda4e..b65fef7b 100644 --- a/tests/test_firetv.py +++ b/tests/test_firetv.py @@ -177,15 +177,15 @@ def test_running_apps(self): """ with patchers.patch_shell(None)[self.PATCH_KEY]: - running_apps = self.ftv.running_apps + running_apps = self.ftv.running_apps() self.assertIsNone(running_apps, None) with patchers.patch_shell('')[self.PATCH_KEY]: - running_apps = self.ftv.running_apps + running_apps = self.ftv.running_apps() self.assertIsNone(running_apps, None) with patchers.patch_shell(RUNNING_APPS_OUTPUT)[self.PATCH_KEY]: - running_apps = self.ftv.running_apps + running_apps = self.ftv.running_apps() self.assertListEqual(running_apps, RUNNING_APPS_LIST) def test_get_properties(self): From 28a8d8202f0c162e5b8ed2b7edf88caf60c7518d Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Tue, 5 May 2020 08:08:30 -0700 Subject: [PATCH 2/2] Update androidtv.py --- androidtv/androidtv.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/androidtv/androidtv.py b/androidtv/androidtv.py index 085bd066..f4d2bacd 100644 --- a/androidtv/androidtv.py +++ b/androidtv/androidtv.py @@ -50,7 +50,7 @@ def update(self, get_running_apps=True): Parameters ---------- get_running_apps : bool - Whether or not to get the :attr:`~androidtv.basetv.BaseTV.running_apps` property + Whether or not to get the :meth:`~androidtv.androidtv.AndroidTV.running_apps` property Returns ------- @@ -180,7 +180,7 @@ def update(self, get_running_apps=True): # ======================================================================= # # # - # properties # + # Properties # # # # ======================================================================= # def get_properties(self, get_running_apps=True, lazy=False): @@ -194,7 +194,7 @@ def get_properties(self, get_running_apps=True, lazy=False): Parameters ---------- get_running_apps : bool - Whether or not to get the :attr:`~androidtv.basetv.BaseTV.running_apps` property + Whether or not to get the :meth:`~androidtv.androidtv.AndroidTV.running_apps` property lazy : bool Whether or not to continue retrieving properties if the device is off or the screensaver is running @@ -302,7 +302,7 @@ def get_properties_dict(self, get_running_apps=True, lazy=True): Parameters ---------- get_running_apps : bool - Whether or not to get the :attr:`~androidtv.basetv.BaseTV.running_apps` property + Whether or not to get the :meth:`~androidtv.androidtv.AndroidTV.running_apps` property lazy : bool Whether or not to continue retrieving properties if the device is off or the screensaver is running @@ -326,11 +326,6 @@ def get_properties_dict(self, get_running_apps=True, lazy=True): 'volume': volume, 'running_apps': running_apps} - # ======================================================================= # - # # - # Properties # - # # - # ======================================================================= # def running_apps(self): """Return a list of running user applications.