Skip to content
Merged
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ omit =
homeassistant/components/viaggiatreno/sensor.py
homeassistant/components/vicare/*
homeassistant/components/vivotek/camera.py
homeassistant/components/vizio/media_player.py
homeassistant/components/vizio/*
homeassistant/components/vlc/media_player.py
homeassistant/components/vlc_telnet/media_player.py
homeassistant/components/volkszaehler/sensor.py
Expand Down
42 changes: 42 additions & 0 deletions homeassistant/components/vizio/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
"""The vizio component."""
import voluptuous as vol

from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DEVICE_CLASS,
CONF_HOST,
CONF_NAME,
)
from homeassistant.helpers import config_validation as cv

from .const import (
CONF_SUPPRESS_WARNING,
CONF_VOLUME_STEP,
DEFAULT_DEVICE_CLASS,
DEFAULT_NAME,
DEFAULT_VOLUME_STEP,
)


def validate_auth(config):
"""Validate presence of CONF_ACCESS_TOKEN when CONF_DEVICE_CLASS=tv."""
token = config.get(CONF_ACCESS_TOKEN)
if config[CONF_DEVICE_CLASS] == "tv" and (token is None or token == ""):
Comment thread
raman325 marked this conversation as resolved.
Outdated
raise vol.Invalid(
f"When '{CONF_DEVICE_CLASS}' is 'tv' then '{CONF_ACCESS_TOKEN}' is required.",
path=[CONF_ACCESS_TOKEN],
)
return config


VIZIO_SCHEMA = {
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_ACCESS_TOKEN): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_SUPPRESS_WARNING, default=False): cv.boolean,
vol.Optional(CONF_DEVICE_CLASS, default=DEFAULT_DEVICE_CLASS): vol.All(
cv.string, vol.Lower, vol.In(["tv", "soundbar"])
),
vol.Optional(CONF_VOLUME_STEP, default=DEFAULT_VOLUME_STEP): vol.All(
vol.Coerce(int), vol.Range(min=1, max=10)
),
}
14 changes: 14 additions & 0 deletions homeassistant/components/vizio/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Constants used by vizio component."""

CONF_SUPPRESS_WARNING = "suppress_warning"
CONF_VOLUME_STEP = "volume_step"

DEFAULT_NAME = "Vizio SmartCast"
DEFAULT_VOLUME_STEP = 1
DEFAULT_DEVICE_CLASS = "tv"
DEVICE_ID = "pyvizio"
DEVICE_NAME = "Python Vizio"

DOMAIN = "vizio"

ICON = {"tv": "mdi:television", "soundbar": "mdi:speaker"}
2 changes: 1 addition & 1 deletion homeassistant/components/vizio/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Vizio",
"documentation": "https://www.home-assistant.io/integrations/vizio",
"requirements": [
"pyvizio==0.0.9"
"pyvizio==0.0.11"
],
"dependencies": [],
"codeowners": ["@raman325"]
Expand Down
67 changes: 23 additions & 44 deletions homeassistant/components/vizio/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@
STATE_OFF,
STATE_ON,
)
from homeassistant.helpers import config_validation as cv

_LOGGER = logging.getLogger(__name__)

CONF_SUPPRESS_WARNING = "suppress_warning"
CONF_VOLUME_STEP = "volume_step"

DEFAULT_NAME = "Vizio SmartCast"
DEFAULT_VOLUME_STEP = 1
DEFAULT_DEVICE_CLASS = "tv"
DEVICE_ID = "pyvizio"
DEVICE_NAME = "Python Vizio"
from . import VIZIO_SCHEMA, validate_auth
from .const import (
CONF_SUPPRESS_WARNING,
CONF_VOLUME_STEP,
DEFAULT_NAME,
DEVICE_ID,
ICON,
)

ICON = "mdi:television"
_LOGGER = logging.getLogger(__name__)

MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
Expand All @@ -59,34 +56,7 @@
}


def validate_auth(config):
"""Validate presence of CONF_ACCESS_TOKEN when CONF_DEVICE_CLASS=tv."""
token = config.get(CONF_ACCESS_TOKEN)
if config[CONF_DEVICE_CLASS] == "tv" and (token is None or token == ""):
raise vol.Invalid(
f"When '{CONF_DEVICE_CLASS}' is 'tv' then '{CONF_ACCESS_TOKEN}' is required.",
path=[CONF_ACCESS_TOKEN],
)
return config


PLATFORM_SCHEMA = vol.All(
PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_ACCESS_TOKEN): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_SUPPRESS_WARNING, default=False): cv.boolean,
vol.Optional(CONF_DEVICE_CLASS, default=DEFAULT_DEVICE_CLASS): vol.All(
cv.string, vol.Lower, vol.In(["tv", "soundbar"])
),
vol.Optional(CONF_VOLUME_STEP, default=DEFAULT_VOLUME_STEP): vol.All(
vol.Coerce(int), vol.Range(min=1, max=10)
),
}
),
validate_auth,
)
PLATFORM_SCHEMA = vol.All(PLATFORM_SCHEMA.extend(VIZIO_SCHEMA), validate_auth)


def setup_platform(hass, config, add_entities, discovery_info=None):
Expand All @@ -97,9 +67,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
volume_step = config[CONF_VOLUME_STEP]
device_type = config[CONF_DEVICE_CLASS]
device = VizioDevice(host, token, name, volume_step, device_type)
if device.validate_setup() is False:
if not device.validate_setup():
fail_auth_msg = ""
if token is not None and token != "":
if token:
fail_auth_msg = " and auth token is correct"
_LOGGER.error(
"Failed to set up Vizio platform, please check if host "
Expand Down Expand Up @@ -133,13 +103,17 @@ def __init__(self, host, token, name, volume_step, device_type):
self._supported_commands = SUPPORTED_COMMANDS[device_type]
self._device = Vizio(DEVICE_ID, host, DEFAULT_NAME, token, device_type)
self._max_volume = float(self._device.get_max_volume())
self._unique_id = self._device.get_esn()
self._unique_id = None
self._icon = ICON[device_type]

@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
def update(self):
"""Retrieve latest state of the device."""
is_on = self._device.get_power_state()

if not self._unique_id:
self._unique_id = self._device.get_esn()

if is_on:
self._state = STATE_ON

Expand Down Expand Up @@ -175,6 +149,11 @@ def name(self):
"""Return the name of the device."""
return self._name

@property
def icon(self):
"""Return the icon of the device."""
return self._icon

@property
def volume_level(self):
"""Return the volume level of the device."""
Expand Down Expand Up @@ -245,7 +224,7 @@ def volume_down(self):

def validate_setup(self):
"""Validate if host is available and auth token is correct."""
return self._device.get_current_volume() is not None
return self._device.can_connect()

def set_volume_level(self, volume):
"""Set volume level."""
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ pyversasense==0.0.6
pyvesync==1.1.0

# homeassistant.components.vizio
pyvizio==0.0.9
pyvizio==0.0.11

# homeassistant.components.velux
pyvlx==0.2.12
Expand Down