From 716dda5da6958fcdb02a671c33db6006fad48701 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Wed, 15 Dec 2021 20:05:41 -0500 Subject: [PATCH 1/5] Use enums in vizio --- homeassistant/components/vizio/__init__.py | 10 +++++----- homeassistant/components/vizio/config_flow.py | 14 ++++++++++---- homeassistant/components/vizio/media_player.py | 7 +++---- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/vizio/__init__.py b/homeassistant/components/vizio/__init__.py index e66a8f3a55422f..4105467aa5d3a5 100644 --- a/homeassistant/components/vizio/__init__.py +++ b/homeassistant/components/vizio/__init__.py @@ -9,7 +9,7 @@ from pyvizio.util import gen_apps_list_from_url import voluptuous as vol -from homeassistant.components.media_player import DEVICE_CLASS_TV +from homeassistant.components.media_player import MediaPlayerDeviceClass from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry, ConfigEntryState from homeassistant.const import Platform from homeassistant.core import HomeAssistant @@ -27,10 +27,10 @@ def validate_apps(config: ConfigType) -> ConfigType: """Validate CONF_APPS is only used when CONF_DEVICE_CLASS == DEVICE_CLASS_TV.""" if ( config.get(CONF_APPS) is not None - and config[CONF_DEVICE_CLASS] != DEVICE_CLASS_TV + and config[CONF_DEVICE_CLASS] != MediaPlayerDeviceClass.TV ): raise vol.Invalid( - f"'{CONF_APPS}' can only be used if {CONF_DEVICE_CLASS}' is '{DEVICE_CLASS_TV}'" + f"'{CONF_APPS}' can only be used if {CONF_DEVICE_CLASS}' is '{MediaPlayerDeviceClass.TV}'" ) return config @@ -63,7 +63,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {}) if ( CONF_APPS not in hass.data[DOMAIN] - and entry.data[CONF_DEVICE_CLASS] == DEVICE_CLASS_TV + and entry.data[CONF_DEVICE_CLASS] == MediaPlayerDeviceClass.TV ): coordinator = VizioAppsDataUpdateCoordinator(hass) await coordinator.async_refresh() @@ -83,7 +83,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> if not any( entry.state is ConfigEntryState.LOADED and entry.entry_id != config_entry.entry_id - and entry.data[CONF_DEVICE_CLASS] == DEVICE_CLASS_TV + and entry.data[CONF_DEVICE_CLASS] == MediaPlayerDeviceClass.TV for entry in hass.config_entries.async_entries(DOMAIN) ): hass.data[DOMAIN].pop(CONF_APPS, None) diff --git a/homeassistant/components/vizio/config_flow.py b/homeassistant/components/vizio/config_flow.py index 9cca89f77aa742..019d016eb2a54f 100644 --- a/homeassistant/components/vizio/config_flow.py +++ b/homeassistant/components/vizio/config_flow.py @@ -12,7 +12,7 @@ from homeassistant import config_entries from homeassistant.components import zeroconf -from homeassistant.components.media_player import DEVICE_CLASS_SPEAKER, DEVICE_CLASS_TV +from homeassistant.components.media_player import MediaPlayerDeviceClass from homeassistant.config_entries import ( SOURCE_IGNORE, SOURCE_IMPORT, @@ -68,7 +68,11 @@ def _get_config_schema(input_dict: dict[str, Any] = None) -> vol.Schema: vol.Required( CONF_DEVICE_CLASS, default=input_dict.get(CONF_DEVICE_CLASS, DEFAULT_DEVICE_CLASS), - ): vol.All(str, vol.Lower, vol.In([DEVICE_CLASS_TV, DEVICE_CLASS_SPEAKER])), + ): vol.All( + str, + vol.Lower, + vol.In([MediaPlayerDeviceClass.TV, MediaPlayerDeviceClass.SPEAKER]), + ), vol.Optional( CONF_ACCESS_TOKEN, default=input_dict.get(CONF_ACCESS_TOKEN, "") ): str, @@ -134,7 +138,7 @@ async def async_step_init(self, user_input: dict[str, Any] = None) -> FlowResult } ) - if self.config_entry.data[CONF_DEVICE_CLASS] == DEVICE_CLASS_TV: + if self.config_entry.data[CONF_DEVICE_CLASS] == MediaPlayerDeviceClass.TV: default_include_or_exclude = ( CONF_EXCLUDE if self.config_entry.options @@ -233,7 +237,9 @@ async def async_step_user(self, user_input: dict[str, Any] = None) -> FlowResult self._must_show_form = False elif user_input[ CONF_DEVICE_CLASS - ] == DEVICE_CLASS_SPEAKER or user_input.get(CONF_ACCESS_TOKEN): + ] == MediaPlayerDeviceClass.SPEAKER or user_input.get( + CONF_ACCESS_TOKEN + ): # Ensure config is valid for a device if not await VizioAsync.validate_ha_config( user_input[CONF_HOST], diff --git a/homeassistant/components/vizio/media_player.py b/homeassistant/components/vizio/media_player.py index 0bff362ad31771..0651c2bc1c70a1 100644 --- a/homeassistant/components/vizio/media_player.py +++ b/homeassistant/components/vizio/media_player.py @@ -10,9 +10,8 @@ from pyvizio.const import APP_HOME, INPUT_APPS, NO_APP_RUNNING, UNKNOWN_APP from homeassistant.components.media_player import ( - DEVICE_CLASS_SPEAKER, - DEVICE_CLASS_TV, SUPPORT_SELECT_SOUND_MODE, + MediaPlayerDeviceClass, MediaPlayerEntity, ) from homeassistant.config_entries import ConfigEntry @@ -256,7 +255,7 @@ async def async_update(self) -> None: self._available_inputs = [input_.name for input_ in inputs] # Return before setting app variables if INPUT_APPS isn't in available inputs - if self._attr_device_class == DEVICE_CLASS_SPEAKER or not any( + if self._attr_device_class == MediaPlayerDeviceClass.SPEAKER or not any( app for app in INPUT_APPS if app in self._available_inputs ): return @@ -332,7 +331,7 @@ def apps_list_update(): self._all_apps = self._apps_coordinator.data self.async_write_ha_state() - if self._attr_device_class == DEVICE_CLASS_TV: + if self._attr_device_class == MediaPlayerDeviceClass.TV: self.async_on_remove( self._apps_coordinator.async_add_listener(apps_list_update) ) From 626d4e029fd645fb63d8bf315b008e49bba0ab9a Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Wed, 15 Dec 2021 21:07:27 -0500 Subject: [PATCH 2/5] Update homeassistant/components/vizio/config_flow.py Co-authored-by: Raman Gupta <7243222+raman325@users.noreply.github.com> --- homeassistant/components/vizio/config_flow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/vizio/config_flow.py b/homeassistant/components/vizio/config_flow.py index 019d016eb2a54f..bf5c58eaea667a 100644 --- a/homeassistant/components/vizio/config_flow.py +++ b/homeassistant/components/vizio/config_flow.py @@ -71,7 +71,7 @@ def _get_config_schema(input_dict: dict[str, Any] = None) -> vol.Schema: ): vol.All( str, vol.Lower, - vol.In([MediaPlayerDeviceClass.TV, MediaPlayerDeviceClass.SPEAKER]), + vol.Coerce(MediaPlayerDeviceClass), ), vol.Optional( CONF_ACCESS_TOKEN, default=input_dict.get(CONF_ACCESS_TOKEN, "") From dbfe11412f58bdd0891ef14d501da6c1c3cacec3 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Thu, 16 Dec 2021 04:50:10 -0500 Subject: [PATCH 3/5] uno mas --- homeassistant/components/vizio/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/vizio/__init__.py b/homeassistant/components/vizio/__init__.py index 4105467aa5d3a5..30710daddbce68 100644 --- a/homeassistant/components/vizio/__init__.py +++ b/homeassistant/components/vizio/__init__.py @@ -24,10 +24,10 @@ def validate_apps(config: ConfigType) -> ConfigType: - """Validate CONF_APPS is only used when CONF_DEVICE_CLASS == DEVICE_CLASS_TV.""" + """Validate CONF_APPS is only used when CONF_DEVICE_CLASS is MediaPlayerDeviceClass.TV.""" if ( config.get(CONF_APPS) is not None - and config[CONF_DEVICE_CLASS] != MediaPlayerDeviceClass.TV + and config[CONF_DEVICE_CLASS] is not MediaPlayerDeviceClass.TV ): raise vol.Invalid( f"'{CONF_APPS}' can only be used if {CONF_DEVICE_CLASS}' is '{MediaPlayerDeviceClass.TV}'" From 9a5ed82901445278ad9c0d6256d6968e751b22e8 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Thu, 16 Dec 2021 13:35:51 -0500 Subject: [PATCH 4/5] revert suggestion --- homeassistant/components/vizio/config_flow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/vizio/config_flow.py b/homeassistant/components/vizio/config_flow.py index bf5c58eaea667a..019d016eb2a54f 100644 --- a/homeassistant/components/vizio/config_flow.py +++ b/homeassistant/components/vizio/config_flow.py @@ -71,7 +71,7 @@ def _get_config_schema(input_dict: dict[str, Any] = None) -> vol.Schema: ): vol.All( str, vol.Lower, - vol.Coerce(MediaPlayerDeviceClass), + vol.In([MediaPlayerDeviceClass.TV, MediaPlayerDeviceClass.SPEAKER]), ), vol.Optional( CONF_ACCESS_TOKEN, default=input_dict.get(CONF_ACCESS_TOKEN, "") From 40a8cba3a2193c7fd97236ab70f208aff0fee66a Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Mon, 20 Dec 2021 18:33:20 -0500 Subject: [PATCH 5/5] uno mas --- homeassistant/components/vizio/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/vizio/__init__.py b/homeassistant/components/vizio/__init__.py index 30710daddbce68..1e0d5a322fb690 100644 --- a/homeassistant/components/vizio/__init__.py +++ b/homeassistant/components/vizio/__init__.py @@ -27,7 +27,7 @@ def validate_apps(config: ConfigType) -> ConfigType: """Validate CONF_APPS is only used when CONF_DEVICE_CLASS is MediaPlayerDeviceClass.TV.""" if ( config.get(CONF_APPS) is not None - and config[CONF_DEVICE_CLASS] is not MediaPlayerDeviceClass.TV + and config[CONF_DEVICE_CLASS] != MediaPlayerDeviceClass.TV ): raise vol.Invalid( f"'{CONF_APPS}' can only be used if {CONF_DEVICE_CLASS}' is '{MediaPlayerDeviceClass.TV}'"