Skip to content

Commit

Permalink
Use enums in vizio (home-assistant#61996)
Browse files Browse the repository at this point in the history
Co-authored-by: Raman Gupta <[email protected]>
  • Loading branch information
tkdrob and raman325 authored Feb 15, 2022
1 parent 2538af4 commit 7eed4af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
12 changes: 6 additions & 6 deletions homeassistant/components/vizio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,13 +24,13 @@


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] != 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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions homeassistant/components/vizio/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down
7 changes: 3 additions & 4 deletions homeassistant/components/vizio/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -252,7 +251,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
Expand Down Expand Up @@ -329,7 +328,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)
)
Expand Down

0 comments on commit 7eed4af

Please sign in to comment.