Update media_player and add tests to qualify vizio integration for platinum quality score (#31187)

* add media player test and update media player logic to qualify vizio for platinum quality score

* add SCAN_INTERVAL and log message once when device goes from available to unavailable and vice versa

* move test constants into one file to avoid defining dupes in each test file

* move constant to shrink diff

* move pytest fixtures to conftest.py

* remove commented out code

* move unload test to test_init

* updates to tests and logging based on review

* bypass notification service

* add fixture so component setup makes it through config flow

* split failure tests into two

* fix setup and entity check for test_init and setup failures in test_media_player

* make domain references consistent across test files

* remove logging steps that were introduced to help debug

* move common patches out to new fixture and use config entry everywhere appropriate

* fix docstring

* add test for update options to increase code coverage

* add one more assert

* refactor test_media_player to move boiler plate logic out of each test into _test_init function

* final refactor of test_media_player to move repeat logic into separate function

* update docstrings

* refactor setup failure tests to move shared logic into private function

* fix last new functions code to use variable instead of static config variable

* remove trailing comma

* test that volume_step gets properly passed to Vizio volume function

* fix comment language

* assert with unittest.mock.call in _test_service and use config_entries.async_setup instead of config_entries.async_add

* replace config_entries.async_add with config_entries.async_setup everywhere

* simplify if statement for argument assertion

* fix logging based on style guide

* remove accidentally committed changes

* update scan interval to something more reasonable and add tests for availability changes

* change filter function to list comprehension, simplify log messages, remove default entity id from logs since it is user configurable, fix docstrings
This commit is contained in:
Raman Gupta authored and GitHub committed 2020-01-30 22:13:45 +01:00
1 parent 9ab6d08b97
commit 73ea34e417
8 files changed
+559 -145

No files matched your search

+2 -1
View File
@@ -6,5 +6,6 @@
"dependencies": [],
"codeowners": ["@raman325"],
"config_flow": true,
"zeroconf": ["_viziocast._tcp.local."]
"zeroconf": ["_viziocast._tcp.local."],
"quality_scale": "platinum"
}
+19 -21
View File
@@ -1,4 +1,5 @@
"""Vizio SmartCast Device support."""
from datetime import timedelta
import logging
from typing import Callable, List
@@ -39,7 +40,7 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=10)
PARALLEL_UPDATES = 0
@@ -55,13 +56,13 @@ async def async_setup_entry(
device_class = config_entry.data[CONF_DEVICE_CLASS]
# If config entry options not set up, set them up, otherwise assign values managed in options
volume_step = config_entry.options.get(
CONF_VOLUME_STEP, config_entry.data.get(CONF_VOLUME_STEP, DEFAULT_VOLUME_STEP),
)
if not config_entry.options:
volume_step = config_entry.data.get(CONF_VOLUME_STEP, DEFAULT_VOLUME_STEP)
hass.config_entries.async_update_entry(
config_entry, options={CONF_VOLUME_STEP: volume_step}
)
else:
volume_step = config_entry.options[CONF_VOLUME_STEP]
device = VizioAsync(
DEVICE_ID,
@@ -74,18 +75,7 @@ async def async_setup_entry(
)
if not await device.can_connect():
fail_auth_msg = ""
if token:
fail_auth_msg = f"and auth token '{token}' are correct."
else:
fail_auth_msg = "is correct."
_LOGGER.warning(
"Failed to connect to Vizio device, please check if host '%s' "
"is valid and available. Also check if device class '%s' %s",
host,
device_class,
fail_auth_msg,
)
_LOGGER.warning("Failed to connect to %s", host)
raise PlatformNotReady
entity = VizioDevice(config_entry, device, name, volume_step, device_class)
@@ -127,10 +117,18 @@ class VizioDevice(MediaPlayerDevice):
is_on = await self._device.get_power_state(log_api_exception=False)
if is_on is None:
self._available = False
if self._available:
_LOGGER.warning(
"Lost connection to %s", self._config_entry.data[CONF_HOST]
)
self._available = False
return
self._available = True
if not self._available:
_LOGGER.info(
"Restored connection to %s", self._config_entry.data[CONF_HOST]
)
self._available = True
if not is_on:
self._state = STATE_OFF
@@ -157,7 +155,7 @@ class VizioDevice(MediaPlayerDevice):
async def _async_send_update_options_signal(
hass: HomeAssistantType, config_entry: ConfigEntry
) -> None:
"""Send update event when when Vizio config entry is updated."""
"""Send update event when Vizio config entry is updated."""
# Move this method to component level if another entity ever gets added for a single config entry.
# See here: https://github.com/home-assistant/home-assistant/pull/30653#discussion_r366426121
async_dispatcher_send(hass, config_entry.entry_id, config_entry)
@@ -276,7 +274,7 @@ class VizioDevice(MediaPlayerDevice):
await self._device.input_switch(source)
async def async_volume_up(self) -> None:
"""Increasing volume of the device."""
"""Increase volume of the device."""
await self._device.vol_up(num=self._volume_step)
if self._volume_level is not None:
@@ -285,7 +283,7 @@ class VizioDevice(MediaPlayerDevice):
)
async def async_volume_down(self) -> None:
"""Decreasing volume of the device."""
"""Decrease volume of the device."""
await self._device.vol_down(num=self._volume_step)
if self._volume_level is not None: