From 9b5a342d138b840637fbd8fb93f0805e5b2e6155 Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Thu, 13 Aug 2020 16:06:03 -0400 Subject: [PATCH 1/9] typehint fixes --- pyvizio/__init__.py | 45 ++++++++++++++++++++++++++-------------- pyvizio/api/_protocol.py | 4 ++-- pyvizio/api/apps.py | 6 ++++-- pyvizio/api/base.py | 4 ++-- pyvizio/api/item.py | 6 +++--- pyvizio/api/pair.py | 6 +++--- pyvizio/api/settings.py | 2 +- pyvizio/cli.py | 6 +++--- 8 files changed, 48 insertions(+), 31 deletions(-) diff --git a/pyvizio/__init__.py b/pyvizio/__init__.py index 429ca85..4da2179 100644 --- a/pyvizio/__init__.py +++ b/pyvizio/__init__.py @@ -2,7 +2,7 @@ from asyncio import sleep from datetime import datetime, timedelta import logging -from typing import Any, Dict, List, Optional, Union +from typing import Any, Dict, KeysView, List, Optional, Union from urllib.parse import urlsplit from aiohttp import ClientSession @@ -76,7 +76,7 @@ def __init__( device_id: str, ip: str, name: str, - auth_token: str = "", + auth_token: Optional[str] = "", device_type: str = DEFAULT_DEVICE_CLASS, session: Optional[ClientSession] = None, timeout: int = DEFAULT_TIMEOUT, @@ -340,7 +340,11 @@ async def stop_pair(self, log_api_exception: bool = True) -> Optional[bool]: ) async def pair( - self, ch_type: int, token: int, pin: str = "", log_api_exception: bool = True + self, + ch_type: Union[int, str], + token: Union[int, str], + pin: str = "", + log_api_exception: bool = True, ) -> Optional[PairChallengeResponse]: """Asynchronously complete pairing process to obtain auth token.""" if self.device_type == DEVICE_CLASS_SPEAKER: @@ -437,17 +441,20 @@ async def vol_down( async def get_current_volume(self, log_api_exception: bool = True) -> Optional[int]: """Asynchronously get device's current volume level.""" - return await VizioAsync.get_audio_setting( + volume = await VizioAsync.get_audio_setting( self, "volume", log_api_exception=log_api_exception ) + return int(volume) if volume else None async def is_muted(self, log_api_exception: bool = True) -> Optional[bool]: """Asynchronously determine whether or not device is muted.""" # If None is returned lower() will fail, if not we can do a simple boolean check try: return ( - await VizioAsync.get_audio_setting( - self, "mute", log_api_exception=log_api_exception + str( + await VizioAsync.get_audio_setting( + self, "mute", log_api_exception=log_api_exception + ) ).lower() == "on" ) @@ -502,7 +509,7 @@ async def remote(self, key: str, log_api_exception: bool = True) -> Optional[boo """Asynchronously emulate key press by key name.""" return await self.__remote(key, log_api_exception=log_api_exception) - def get_remote_keys_list(self) -> List[str]: + def get_remote_keys_list(self) -> KeysView[str]: """Get list of remote key names.""" return KEY_CODE[self.device_type].keys() @@ -582,7 +589,7 @@ async def get_setting( async def get_setting_options( self, setting_type: str, setting_name: str, log_api_exception: bool = True - ) -> Optional[Union[int, str]]: + ) -> Optional[Union[List[str], Dict[str, Union[int, str]]]]: """Asynchronously get options of named setting.""" return await self.__invoke_api_may_need_auth( GetSettingOptionsCommand(self.device_type, setting_type, setting_name), @@ -591,7 +598,7 @@ async def get_setting_options( async def get_setting_options_xlist( self, setting_type: str, setting_name: str, log_api_exception: bool = True - ) -> Optional[Union[int, str]]: + ) -> Optional[List[str]]: """Asynchronously get options of named setting for settings based on a user defined list.""" return await self.__invoke_api_may_need_auth( GetSettingOptionsXListCommand(self.device_type, setting_type, setting_name), @@ -652,7 +659,7 @@ async def get_audio_setting( async def get_audio_setting_options( self, setting_name: str, log_api_exception: bool = True - ) -> Optional[Union[int, str]]: + ) -> Optional[Union[List[str], Dict[str, Union[int, str]]]]: """Asynchronously get options of named audio setting.""" return await VizioAsync.get_setting_options( self, "audio", setting_name, log_api_exception=log_api_exception @@ -685,14 +692,14 @@ async def get_apps_list( APP_HOME["name"], *sorted( [ - app["name"] + str(app["name"]) for app in apps_list if "*" in app["country"] or country.lower() in app["country"] ] ), ] - return [APP_HOME["name"], *sorted([app["name"] for app in apps_list])] + return [APP_HOME["name"], *sorted([str(app["name"]) for app in apps_list])] async def launch_app( self, @@ -1021,7 +1028,7 @@ async def remote(self, key: str, log_api_exception: bool = True) -> Optional[boo """Emulate key press by key name.""" return await super(Vizio, self).remote(key, log_api_exception=log_api_exception) - def get_remote_keys_list(self) -> List[str]: + def get_remote_keys_list(self) -> KeysView[str]: """Get list of remote key names.""" return super(Vizio, self).get_remote_keys_list() @@ -1064,12 +1071,20 @@ async def get_setting( @async_to_sync async def get_setting_options( self, setting_type: str, setting_name: str, log_api_exception: bool = True - ) -> Optional[Union[int, str]]: + ) -> Optional[Union[List[str], Dict[str, Union[int, str]]]]: """Get options of named setting.""" return await super(Vizio, self).get_setting_options( setting_type, setting_name, log_api_exception=log_api_exception ) + async def get_setting_options_xlist( + self, setting_type: str, setting_name: str, log_api_exception: bool = True + ) -> Optional[List[str]]: + """Get options of named setting for settings based on a user defined list.""" + return await super(Vizio, self).get_setting_options_xlist( + setting_type, setting_name, log_api_exception=log_api_exception + ) + @async_to_sync async def set_setting( self, @@ -1113,7 +1128,7 @@ async def get_audio_setting( @async_to_sync async def get_audio_setting_options( self, setting_name: str, log_api_exception: bool = True - ) -> Optional[Union[int, str]]: + ) -> Optional[Union[List[str], Dict[str, Union[int, str]]]]: """Get options of named audio setting.""" return await super(Vizio, self).get_audio_setting_options( setting_name, log_api_exception=log_api_exception diff --git a/pyvizio/api/_protocol.py b/pyvizio/api/_protocol.py index 44a7b1c..d61a4fc 100644 --- a/pyvizio/api/_protocol.py +++ b/pyvizio/api/_protocol.py @@ -229,7 +229,7 @@ async def async_invoke_api( "method": "put", "url": url, "headers": headers, - "data": json.loads(data), + "data": json.loads(str(data)), }, ) response = await session.put( @@ -256,7 +256,7 @@ async def async_invoke_api( "method": "put", "url": url, "headers": headers, - "data": json.loads(data), + "data": json.loads(str(data)), }, ) response = await local_session.put( diff --git a/pyvizio/api/apps.py b/pyvizio/api/apps.py index 6f272df..026d0ea 100644 --- a/pyvizio/api/apps.py +++ b/pyvizio/api/apps.py @@ -1,6 +1,6 @@ """Vizio SmartCast API commands for apps.""" -from typing import Any, Dict, List, Union +from typing import Any, Dict, List, Optional, Union from pyvizio.api._protocol import ENDPOINT, ResponseKey from pyvizio.api.base import CommandBase @@ -35,7 +35,9 @@ def __bool__(self) -> bool: return self != AppConfig() -def find_app_name(config_to_check: AppConfig, app_list: List[Dict[str, Any]]) -> str: +def find_app_name( + config_to_check: Optional[AppConfig], app_list: List[Dict[str, Any]] +) -> str: """ Return the app name for a given AppConfig based on a list of apps. diff --git a/pyvizio/api/base.py b/pyvizio/api/base.py index 40621c8..00d47f2 100644 --- a/pyvizio/api/base.py +++ b/pyvizio/api/base.py @@ -40,7 +40,7 @@ def get_method(self) -> str: return self._method @abstractmethod - def process_response(self, json_obj: Dict[str, Any]) -> None: + def process_response(self, json_obj: Dict[str, Any]) -> Any: """Always return True when there is no custom process_response method for subclass.""" return True @@ -67,6 +67,6 @@ def url(self, new_url: str) -> None: """Set endpoint for command.""" CommandBase.url.fset(self, new_url) - def process_response(self, json_obj: Dict[str, Any]) -> None: + def process_response(self, json_obj: Dict[str, Any]) -> Any: """Always return None when there is no custom process_response method for subclass.""" return None diff --git a/pyvizio/api/item.py b/pyvizio/api/item.py index 314e0a0..52eae43 100644 --- a/pyvizio/api/item.py +++ b/pyvizio/api/item.py @@ -1,6 +1,6 @@ """Vizio SmartCast API commands and class for individual item settings.""" -from typing import Any, Dict, Union +from typing import Any, Dict, Optional, Union from pyvizio.api._protocol import ( ACTION_MODIFY, @@ -21,7 +21,7 @@ def __init__(self, device_type: str) -> None: super(GetDeviceInfoCommand, self).__init__(ENDPOINT[device_type]["DEVICE_INFO"]) self.paths = PATH_MODEL[device_type] - def process_response(self, json_obj: Dict[str, Any]) -> bool: + def process_response(self, json_obj: Dict[str, Any]) -> Dict[str, Any]: """Return response to command to get device info.""" return dict_get_case_insensitive(json_obj, ResponseKey.ITEMS, [{}])[0] @@ -33,7 +33,7 @@ def __init__(self, device_type: str) -> None: """Initialize command to get device model name.""" super(GetModelNameCommand, self).__init__(device_type) - def process_response(self, json_obj: Dict[str, Any]) -> bool: + def process_response(self, json_obj: Dict[str, Any]) -> Optional[str]: """Return response to command to get device model name.""" return get_value_from_path( dict_get_case_insensitive( diff --git a/pyvizio/api/pair.py b/pyvizio/api/pair.py index f9c4cce..4a2502a 100644 --- a/pyvizio/api/pair.py +++ b/pyvizio/api/pair.py @@ -1,6 +1,6 @@ """Vizio SmartCast API commands and class for pairing.""" -from typing import Any, Dict +from typing import Any, Dict, Union from pyvizio.api._protocol import ENDPOINT, PairingResponseKey, ResponseKey from pyvizio.api.base import CommandBase @@ -69,8 +69,8 @@ class PairChallengeCommand(PairCommandBase): def __init__( self, device_id: str, - challenge_type: int, - pairing_token: int, + challenge_type: Union[int, str], + pairing_token: Union[int, str], pin: str, device_type: str, ) -> None: diff --git a/pyvizio/api/settings.py b/pyvizio/api/settings.py index a0da03f..0df8ba4 100644 --- a/pyvizio/api/settings.py +++ b/pyvizio/api/settings.py @@ -161,7 +161,7 @@ def __init__(self, device_type: str, setting_type: str, setting_name: str) -> No device_type, self.setting_type ) - def process_response(self, json_obj: Dict[str, Any]) -> List[str]: + def process_response(self, json_obj: Dict[str, Any]) -> Optional[List[str]]: """Return response to command to get options of an audio setting by name (used for setting of type XList).""" return ( super(GetSettingOptionsXListCommand, self) diff --git a/pyvizio/cli.py b/pyvizio/cli.py index 90dd64b..6b59694 100644 --- a/pyvizio/cli.py +++ b/pyvizio/cli.py @@ -409,7 +409,7 @@ async def get_all_audio_settings_options(vizio: VizioAsync) -> None: for k, v in audio_settings_options.items(): if isinstance(v, dict): options.append([k, v.get("default"), v["min"], v["max"], None]) - else: + elif isinstance(v, list): options.append([k, None, None, None, ", ".join(v)]) table = tabulate(options, headers=["Name", "Default", "Min", "Max", "Choices"]) _LOGGER.info("\n%s", table) @@ -451,7 +451,7 @@ async def get_setting_options( else: table = tabulate([[value["min"], value["max"]]], headers=["Min", "Max"]) _LOGGER.info("For '%s' setting:\n%s", setting_name, table) - else: + elif isinstance(value, list): _LOGGER.info("Options for '%s' setting: %s", setting_name, ", ".join(value)) else: _LOGGER.error("Couldn't get options for '%s' setting", setting_name) @@ -563,7 +563,7 @@ async def get_audio_setting_options(vizio: VizioAsync, setting_name: str) -> Non else: table = tabulate([[value["min"], value["max"]]], headers=["Min", "Max"]) _LOGGER.info("For '%s' setting:\n%s", setting_name, table) - else: + elif isinstance(value, list): _LOGGER.info("Options for '%s' setting: %s", setting_name, ", ".join(value)) else: _LOGGER.error("Couldn't get options for '%s' setting", setting_name) From e040867eeff190467399305a4841765519e159e4 Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Fri, 13 Nov 2020 13:50:36 -0500 Subject: [PATCH 2/9] support alternative endpoints for serial number, ESN, and version --- pyvizio/__init__.py | 15 +++++++++++++-- pyvizio/api/_protocol.py | 6 ++++++ pyvizio/api/item.py | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/pyvizio/__init__.py b/pyvizio/__init__.py index 4da2179..eaefce2 100644 --- a/pyvizio/__init__.py +++ b/pyvizio/__init__.py @@ -25,6 +25,9 @@ InputItem, ) from pyvizio.api.item import ( + GetAltESNCommand, + GetAltSerialNumberCommand, + GetAltVersionCommand, GetCurrentPowerStateCommand, GetDeviceInfoCommand, GetESNCommand, @@ -193,8 +196,9 @@ async def __remote_multiple( return await self.__remote(key_codes, log_api_exception=log_api_exception) async def __get_cached_apps_list(self) -> List[str]: - if self._latest_apps and datetime.now() - self._latest_apps_last_updated < timedelta( - days=1 + if ( + self._latest_apps + and datetime.now() - self._latest_apps_last_updated < timedelta(days=1) ): await sleep(0) return self._latest_apps @@ -287,6 +291,8 @@ async def get_esn(self, log_api_exception: bool = True) -> Optional[str]: """Asynchronously get device's ESN (electronic serial number?).""" item = await self.__invoke_api_may_need_auth( GetESNCommand(self.device_type), log_api_exception=log_api_exception + ) or await self.__invoke_api_may_need_auth( + GetAltESNCommand(self.device_type), log_api_exception=log_api_exception ) if item: @@ -299,6 +305,9 @@ async def get_serial_number(self, log_api_exception: bool = True) -> Optional[st item = await self.__invoke_api( GetSerialNumberCommand(self.device_type), log_api_exception=log_api_exception, + ) or await self.__invoke_api( + GetAltSerialNumberCommand(self.device_type), + log_api_exception=log_api_exception, ) if item: @@ -310,6 +319,8 @@ async def get_version(self, log_api_exception: bool = True) -> Optional[str]: """Asynchronously get SmartCast software version on device.""" item = await self.__invoke_api( GetVersionCommand(self.device_type), log_api_exception=log_api_exception + ) or await self.__invoke_api( + GetAltVersionCommand(self.device_type), log_api_exception=log_api_exception ) if item: diff --git a/pyvizio/api/_protocol.py b/pyvizio/api/_protocol.py index d61a4fc..a017ee8 100644 --- a/pyvizio/api/_protocol.py +++ b/pyvizio/api/_protocol.py @@ -40,6 +40,9 @@ "ESN": "/menu_native/dynamic/tv_settings/system/system_information/uli_information/esn", "SERIAL_NUMBER": "/menu_native/dynamic/tv_settings/system/system_information/tv_information/serial_number", "VERSION": "/menu_native/dynamic/tv_settings/system/system_information/tv_information/version", + "_ALT_ESN": "/menu_native/dynamic/tv_settings/admin_and_privacy/system_information/uli_information/esn", + "_ALT_SERIAL_NUMBER": "/menu_native/dynamic/tv_settings/admin_and_privacy/system_information/tv_information/serial_number", + "_ALT_VERSION": "/menu_native/dynamic/tv_settings/admin_and_privacy/system_information/tv_information/version", "DEVICE_INFO": "/state/device/deviceinfo", "POWER_MODE": "/state/device/power_mode", "KEY_PRESS": "/key_command/", @@ -57,6 +60,9 @@ "ESN": "/menu_native/dynamic/audio_settings/system/system_information/uli_information/esn", "SERIAL_NUMBER": "/menu_native/dynamic/audio_settings/system/system_information/speaker_information/serial_number", "VERSION": "/menu_native/dynamic/audio_settings/system/system_information/speaker_information/version", + "_ALT_ESN": "/menu_native/dynamic/audio_settings/admin_and_privacy/system_information/uli_information/esn", + "_ALT_SERIAL_NUMBER": "/menu_native/dynamic/audio_settings/admin_and_privacy/system_information/speaker_information/serial_number", + "_ALT_VERSION": "/menu_native/dynamic/audio_settings/admin_and_privacy/system_information/speaker_information/version", "DEVICE_INFO": "/state/device/deviceinfo", "POWER_MODE": "/state/device/power_mode", "KEY_PRESS": "/key_command/", diff --git a/pyvizio/api/item.py b/pyvizio/api/item.py index 52eae43..54e2082 100644 --- a/pyvizio/api/item.py +++ b/pyvizio/api/item.py @@ -184,3 +184,29 @@ class GetVersionCommand(ItemInfoCommandBase): def __init__(self, device_type: str) -> None: """Initialize command to get SmartCast software version.""" super(GetVersionCommand, self).__init__(device_type, "VERSION") + + +class GetAltESNCommand(ItemInfoCommandBase): + """Command to get device ESN (electronic serial number?).""" + + def __init__(self, device_type: str) -> None: + """Initialize command to get device ESN (electronic serial number?).""" + super(GetAltESNCommand, self).__init__(device_type, "_ALT_ESN") + + +class GetAltSerialNumberCommand(ItemInfoCommandBase): + """Command to get device serial number.""" + + def __init__(self, device_type: str) -> None: + """Initialize command to get device serial number.""" + super(GetAltSerialNumberCommand, self).__init__( + device_type, "_ALT_SERIAL_NUMBER" + ) + + +class GetAltVersionCommand(ItemInfoCommandBase): + """Command to get SmartCast software version.""" + + def __init__(self, device_type: str) -> None: + """Initialize command to get SmartCast software version.""" + super(GetAltVersionCommand, self).__init__(device_type, "_ALT_VERSION") From 60df9d95bc7e79bdb42d0083b5ec100417c3480f Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Fri, 13 Nov 2020 13:56:13 -0500 Subject: [PATCH 3/9] version bump and add new CLI commands --- pyvizio/cli.py | 36 ++++++++++++++++++++++++++++++++++++ pyvizio/version.py | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/pyvizio/cli.py b/pyvizio/cli.py index 6b59694..5a3b567 100644 --- a/pyvizio/cli.py +++ b/pyvizio/cli.py @@ -674,5 +674,41 @@ async def get_current_app_config(vizio: VizioAsync) -> None: _LOGGER.info("No currently running app") +@cli.command() +@async_to_sync +@pass_vizio +async def get_version(vizio: VizioAsync) -> None: + item = await vizio.get_version(False) + + if item is None: + _LOGGER.error("Couldn't get version") + else: + _LOGGER.info("Current version: %s", item) + + +@cli.command() +@async_to_sync +@pass_vizio +async def get_esn(vizio: VizioAsync) -> None: + item = await vizio.get_esn(False) + + if item is None: + _LOGGER.error("Couldn't get ESN") + else: + _LOGGER.info("ESN: %s", item) + + +@cli.command() +@async_to_sync +@pass_vizio +async def get_serial_number(vizio: VizioAsync) -> None: + item = await vizio.get_serial_number(False) + + if item is None: + _LOGGER.error("Couldn't get serial number") + else: + _LOGGER.info("Serial Number: %s", item) + + if __name__ == "__main__": cli() diff --git a/pyvizio/version.py b/pyvizio/version.py index 6380479..12291ce 100644 --- a/pyvizio/version.py +++ b/pyvizio/version.py @@ -1,3 +1,3 @@ """pyvizio version.""" -__version__ = "0.1.56" +__version__ = "0.1.57" From 0c6a649f80a12184565d33d042dd5bf5d9edb80c Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Mon, 16 Nov 2020 22:51:05 -0500 Subject: [PATCH 4/9] switch strategy for getting alternate version, esn, and serial number endpoints --- pyvizio/__init__.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pyvizio/__init__.py b/pyvizio/__init__.py index eaefce2..6dd5f0d 100644 --- a/pyvizio/__init__.py +++ b/pyvizio/__init__.py @@ -291,7 +291,12 @@ async def get_esn(self, log_api_exception: bool = True) -> Optional[str]: """Asynchronously get device's ESN (electronic serial number?).""" item = await self.__invoke_api_may_need_auth( GetESNCommand(self.device_type), log_api_exception=log_api_exception - ) or await self.__invoke_api_may_need_auth( + ) + + if item: + return item.value + + item = await self.__invoke_api_may_need_auth( GetAltESNCommand(self.device_type), log_api_exception=log_api_exception ) @@ -305,7 +310,12 @@ async def get_serial_number(self, log_api_exception: bool = True) -> Optional[st item = await self.__invoke_api( GetSerialNumberCommand(self.device_type), log_api_exception=log_api_exception, - ) or await self.__invoke_api( + ) + + if item: + return item.value + + item = await self.__invoke_api( GetAltSerialNumberCommand(self.device_type), log_api_exception=log_api_exception, ) @@ -319,7 +329,12 @@ async def get_version(self, log_api_exception: bool = True) -> Optional[str]: """Asynchronously get SmartCast software version on device.""" item = await self.__invoke_api( GetVersionCommand(self.device_type), log_api_exception=log_api_exception - ) or await self.__invoke_api( + ) + + if item: + return item.value + + item = await self.__invoke_api( GetAltVersionCommand(self.device_type), log_api_exception=log_api_exception ) From 7cbd41b95a89bd8e9568e11451525e65291d2f4e Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Tue, 17 Nov 2020 16:15:06 -0500 Subject: [PATCH 5/9] try something --- pyvizio/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pyvizio/__init__.py b/pyvizio/__init__.py index 6dd5f0d..38319a6 100644 --- a/pyvizio/__init__.py +++ b/pyvizio/__init__.py @@ -293,14 +293,14 @@ async def get_esn(self, log_api_exception: bool = True) -> Optional[str]: GetESNCommand(self.device_type), log_api_exception=log_api_exception ) - if item: + if item and item.value: return item.value item = await self.__invoke_api_may_need_auth( GetAltESNCommand(self.device_type), log_api_exception=log_api_exception ) - if item: + if item and item.value: return item.value return None @@ -312,7 +312,7 @@ async def get_serial_number(self, log_api_exception: bool = True) -> Optional[st log_api_exception=log_api_exception, ) - if item: + if item and item.value: return item.value item = await self.__invoke_api( @@ -320,7 +320,7 @@ async def get_serial_number(self, log_api_exception: bool = True) -> Optional[st log_api_exception=log_api_exception, ) - if item: + if item and item.value: return item.value return None @@ -331,14 +331,14 @@ async def get_version(self, log_api_exception: bool = True) -> Optional[str]: GetVersionCommand(self.device_type), log_api_exception=log_api_exception ) - if item: + if item and item.value: return item.value item = await self.__invoke_api( GetAltVersionCommand(self.device_type), log_api_exception=log_api_exception ) - if item: + if item and item.value: return item.value return None From 0a24b761ffc09b84e72440b05a302ff07fdf6955 Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Wed, 18 Nov 2020 11:23:40 -0500 Subject: [PATCH 6/9] add alt commands --- pyvizio/__init__.py | 34 ++++++++++++++++++++++++++++++++++ pyvizio/cli.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/pyvizio/__init__.py b/pyvizio/__init__.py index 38319a6..de2bf6b 100644 --- a/pyvizio/__init__.py +++ b/pyvizio/__init__.py @@ -343,6 +343,40 @@ async def get_version(self, log_api_exception: bool = True) -> Optional[str]: return None + async def get_alt_esn(self, log_api_exception: bool = True) -> Optional[str]: + """Asynchronously get device's ESN (electronic serial number?).""" + item = await self.__invoke_api_may_need_auth( + GetAltESNCommand(self.device_type), log_api_exception=log_api_exception + ) + + if item and item.value: + return item.value + + return None + + async def get_alt_serial_number(self, log_api_exception: bool = True) -> Optional[str]: + """Asynchronously get device's serial number.""" + item = await self.__invoke_api( + GetAltSerialNumberCommand(self.device_type), + log_api_exception=log_api_exception, + ) + + if item and item.value: + return item.value + + return None + + async def get_alt_version(self, log_api_exception: bool = True) -> Optional[str]: + """Asynchronously get SmartCast software version on device.""" + item = await self.__invoke_api( + GetAltVersionCommand(self.device_type), log_api_exception=log_api_exception + ) + + if item and item.value: + return item.value + + return None + async def get_model_name(self, log_api_exception: bool = True) -> Optional[str]: """Asynchronously get device's model number.""" return await self.__invoke_api( diff --git a/pyvizio/cli.py b/pyvizio/cli.py index 5a3b567..da04c82 100644 --- a/pyvizio/cli.py +++ b/pyvizio/cli.py @@ -710,5 +710,41 @@ async def get_serial_number(vizio: VizioAsync) -> None: _LOGGER.info("Serial Number: %s", item) +@cli.command() +@async_to_sync +@pass_vizio +async def get_alt_version(vizio: VizioAsync) -> None: + item = await vizio.get_alt_version(False) + + if item is None: + _LOGGER.error("Couldn't get version") + else: + _LOGGER.info("Current version: %s", item) + + +@cli.command() +@async_to_sync +@pass_vizio +async def get_alt_esn(vizio: VizioAsync) -> None: + item = await vizio.get_alt_esn(False) + + if item is None: + _LOGGER.error("Couldn't get ESN") + else: + _LOGGER.info("ESN: %s", item) + + +@cli.command() +@async_to_sync +@pass_vizio +async def get_alt_serial_number(vizio: VizioAsync) -> None: + item = await vizio.get_alt_serial_number(False) + + if item is None: + _LOGGER.error("Couldn't get serial number") + else: + _LOGGER.info("Serial Number: %s", item) + + if __name__ == "__main__": cli() From b87071edd7d63ce9a852ae49c1035c90c5170aaa Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Wed, 18 Nov 2020 15:30:22 -0500 Subject: [PATCH 7/9] fix item command --- pyvizio/__init__.py | 4 +++- pyvizio/api/item.py | 30 ++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/pyvizio/__init__.py b/pyvizio/__init__.py index de2bf6b..3d8ad6d 100644 --- a/pyvizio/__init__.py +++ b/pyvizio/__init__.py @@ -354,7 +354,9 @@ async def get_alt_esn(self, log_api_exception: bool = True) -> Optional[str]: return None - async def get_alt_serial_number(self, log_api_exception: bool = True) -> Optional[str]: + async def get_alt_serial_number( + self, log_api_exception: bool = True + ) -> Optional[str]: """Asynchronously get device's serial number.""" item = await self.__invoke_api( GetAltSerialNumberCommand(self.device_type), diff --git a/pyvizio/api/item.py b/pyvizio/api/item.py index 54e2082..448b5f5 100644 --- a/pyvizio/api/item.py +++ b/pyvizio/api/item.py @@ -138,6 +138,22 @@ def process_response(self, json_obj: Dict[str, Any]) -> Any: return None +class AltItemInfoCommandBase(ItemInfoCommandBase): + """Command to get individual item setting.""" + + def __init__( + self, + device_type: str, + endpoint_name: str, + item_name: str, + default_return: Union[int, str] = None, + ) -> None: + """Initialize command to get individual item setting.""" + super(ItemInfoCommandBase, self).__init__(ENDPOINT[device_type][endpoint_name]) + self.item_name = item_name.upper() + self.default_return = default_return + + class ItemCommandBase(CommandBase): """Command to set value of individual item setting.""" @@ -186,27 +202,29 @@ def __init__(self, device_type: str) -> None: super(GetVersionCommand, self).__init__(device_type, "VERSION") -class GetAltESNCommand(ItemInfoCommandBase): +class GetAltESNCommand(AltItemInfoCommandBase): """Command to get device ESN (electronic serial number?).""" def __init__(self, device_type: str) -> None: """Initialize command to get device ESN (electronic serial number?).""" - super(GetAltESNCommand, self).__init__(device_type, "_ALT_ESN") + super(GetAltESNCommand, self).__init__(device_type, "_ALT_ESN", "ESN") -class GetAltSerialNumberCommand(ItemInfoCommandBase): +class GetAltSerialNumberCommand(AltItemInfoCommandBase): """Command to get device serial number.""" def __init__(self, device_type: str) -> None: """Initialize command to get device serial number.""" super(GetAltSerialNumberCommand, self).__init__( - device_type, "_ALT_SERIAL_NUMBER" + device_type, "_ALT_SERIAL_NUMBER", "SERIAL_NUMBER" ) -class GetAltVersionCommand(ItemInfoCommandBase): +class GetAltVersionCommand(AltItemInfoCommandBase): """Command to get SmartCast software version.""" def __init__(self, device_type: str) -> None: """Initialize command to get SmartCast software version.""" - super(GetAltVersionCommand, self).__init__(device_type, "_ALT_VERSION") + super(GetAltVersionCommand, self).__init__( + device_type, "_ALT_VERSION", "VERSION" + ) From 551bdf9e712653dff724ce58d58fd90bcc14086a Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Wed, 18 Nov 2020 15:31:20 -0500 Subject: [PATCH 8/9] fix item command --- pyvizio/api/item.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pyvizio/api/item.py b/pyvizio/api/item.py index 448b5f5..d7add69 100644 --- a/pyvizio/api/item.py +++ b/pyvizio/api/item.py @@ -138,22 +138,6 @@ def process_response(self, json_obj: Dict[str, Any]) -> Any: return None -class AltItemInfoCommandBase(ItemInfoCommandBase): - """Command to get individual item setting.""" - - def __init__( - self, - device_type: str, - endpoint_name: str, - item_name: str, - default_return: Union[int, str] = None, - ) -> None: - """Initialize command to get individual item setting.""" - super(ItemInfoCommandBase, self).__init__(ENDPOINT[device_type][endpoint_name]) - self.item_name = item_name.upper() - self.default_return = default_return - - class ItemCommandBase(CommandBase): """Command to set value of individual item setting.""" @@ -202,6 +186,22 @@ def __init__(self, device_type: str) -> None: super(GetVersionCommand, self).__init__(device_type, "VERSION") +class AltItemInfoCommandBase(ItemInfoCommandBase): + """Command to get individual item setting.""" + + def __init__( + self, + device_type: str, + endpoint_name: str, + item_name: str, + default_return: Union[int, str] = None, + ) -> None: + """Initialize command to get individual item setting.""" + super(ItemInfoCommandBase, self).__init__(ENDPOINT[device_type][endpoint_name]) + self.item_name = item_name.upper() + self.default_return = default_return + + class GetAltESNCommand(AltItemInfoCommandBase): """Command to get device ESN (electronic serial number?).""" From de858a5e9fd14c4c947bc6f1ded8f34565e66671 Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Wed, 18 Nov 2020 16:17:54 -0500 Subject: [PATCH 9/9] revert test code --- pyvizio/__init__.py | 57 +++------------------------------------------ pyvizio/cli.py | 36 ---------------------------- 2 files changed, 3 insertions(+), 90 deletions(-) diff --git a/pyvizio/__init__.py b/pyvizio/__init__.py index 3d8ad6d..a49dacb 100644 --- a/pyvizio/__init__.py +++ b/pyvizio/__init__.py @@ -291,12 +291,7 @@ async def get_esn(self, log_api_exception: bool = True) -> Optional[str]: """Asynchronously get device's ESN (electronic serial number?).""" item = await self.__invoke_api_may_need_auth( GetESNCommand(self.device_type), log_api_exception=log_api_exception - ) - - if item and item.value: - return item.value - - item = await self.__invoke_api_may_need_auth( + ) or await self.__invoke_api_may_need_auth( GetAltESNCommand(self.device_type), log_api_exception=log_api_exception ) @@ -310,12 +305,7 @@ async def get_serial_number(self, log_api_exception: bool = True) -> Optional[st item = await self.__invoke_api( GetSerialNumberCommand(self.device_type), log_api_exception=log_api_exception, - ) - - if item and item.value: - return item.value - - item = await self.__invoke_api( + ) or await self.__invoke_api( GetAltSerialNumberCommand(self.device_type), log_api_exception=log_api_exception, ) @@ -329,48 +319,7 @@ async def get_version(self, log_api_exception: bool = True) -> Optional[str]: """Asynchronously get SmartCast software version on device.""" item = await self.__invoke_api( GetVersionCommand(self.device_type), log_api_exception=log_api_exception - ) - - if item and item.value: - return item.value - - item = await self.__invoke_api( - GetAltVersionCommand(self.device_type), log_api_exception=log_api_exception - ) - - if item and item.value: - return item.value - - return None - - async def get_alt_esn(self, log_api_exception: bool = True) -> Optional[str]: - """Asynchronously get device's ESN (electronic serial number?).""" - item = await self.__invoke_api_may_need_auth( - GetAltESNCommand(self.device_type), log_api_exception=log_api_exception - ) - - if item and item.value: - return item.value - - return None - - async def get_alt_serial_number( - self, log_api_exception: bool = True - ) -> Optional[str]: - """Asynchronously get device's serial number.""" - item = await self.__invoke_api( - GetAltSerialNumberCommand(self.device_type), - log_api_exception=log_api_exception, - ) - - if item and item.value: - return item.value - - return None - - async def get_alt_version(self, log_api_exception: bool = True) -> Optional[str]: - """Asynchronously get SmartCast software version on device.""" - item = await self.__invoke_api( + ) or await self.__invoke_api( GetAltVersionCommand(self.device_type), log_api_exception=log_api_exception ) diff --git a/pyvizio/cli.py b/pyvizio/cli.py index da04c82..5a3b567 100644 --- a/pyvizio/cli.py +++ b/pyvizio/cli.py @@ -710,41 +710,5 @@ async def get_serial_number(vizio: VizioAsync) -> None: _LOGGER.info("Serial Number: %s", item) -@cli.command() -@async_to_sync -@pass_vizio -async def get_alt_version(vizio: VizioAsync) -> None: - item = await vizio.get_alt_version(False) - - if item is None: - _LOGGER.error("Couldn't get version") - else: - _LOGGER.info("Current version: %s", item) - - -@cli.command() -@async_to_sync -@pass_vizio -async def get_alt_esn(vizio: VizioAsync) -> None: - item = await vizio.get_alt_esn(False) - - if item is None: - _LOGGER.error("Couldn't get ESN") - else: - _LOGGER.info("ESN: %s", item) - - -@cli.command() -@async_to_sync -@pass_vizio -async def get_alt_serial_number(vizio: VizioAsync) -> None: - item = await vizio.get_alt_serial_number(False) - - if item is None: - _LOGGER.error("Couldn't get serial number") - else: - _LOGGER.info("Serial Number: %s", item) - - if __name__ == "__main__": cli()