2019-03-17 03:44:05 +00:00
|
|
|
"""The vizio component."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-18 08:15:06 +00:00
|
|
|
import asyncio
|
2020-09-02 09:55:10 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2021-03-18 13:43:52 +00:00
|
|
|
from typing import Any
|
2020-01-18 08:15:06 +00:00
|
|
|
|
2020-09-02 09:55:10 +00:00
|
|
|
from pyvizio.const import APPS
|
|
|
|
from pyvizio.util import gen_apps_list_from_url
|
2020-01-07 01:32:31 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-03-05 21:34:12 +00:00
|
|
|
from homeassistant.components.media_player import DEVICE_CLASS_TV
|
2020-09-02 09:55:10 +00:00
|
|
|
from homeassistant.config_entries import ENTRY_STATE_LOADED, SOURCE_IMPORT, ConfigEntry
|
2021-04-22 14:53:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-01-07 01:32:31 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2020-09-02 09:55:10 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2021-04-22 14:53:57 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2020-09-02 09:55:10 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
2020-01-07 01:32:31 +00:00
|
|
|
|
2020-03-05 21:34:12 +00:00
|
|
|
from .const import CONF_APPS, CONF_DEVICE_CLASS, DOMAIN, VIZIO_SCHEMA
|
|
|
|
|
2020-09-02 09:55:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-03-05 21:34:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
):
|
|
|
|
raise vol.Invalid(
|
|
|
|
f"'{CONF_APPS}' can only be used if {CONF_DEVICE_CLASS}' is '{DEVICE_CLASS_TV}'"
|
|
|
|
)
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
2020-01-07 01:32:31 +00:00
|
|
|
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2020-03-05 21:34:12 +00:00
|
|
|
{DOMAIN: vol.All(cv.ensure_list, [vol.All(VIZIO_SCHEMA, validate_apps)])},
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
2020-01-18 08:15:06 +00:00
|
|
|
PLATFORMS = ["media_player"]
|
|
|
|
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
|
2021-04-22 14:53:57 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
"""Component setup, run import config flow for each entry in config."""
|
|
|
|
if DOMAIN in config:
|
|
|
|
for entry in config[DOMAIN]:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=entry
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-04-22 14:53:57 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
"""Load the saved entities."""
|
2020-09-02 09:55:10 +00:00
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
if (
|
|
|
|
CONF_APPS not in hass.data[DOMAIN]
|
|
|
|
and config_entry.data[CONF_DEVICE_CLASS] == DEVICE_CLASS_TV
|
|
|
|
):
|
|
|
|
coordinator = VizioAppsDataUpdateCoordinator(hass)
|
|
|
|
await coordinator.async_refresh()
|
|
|
|
hass.data[DOMAIN][CONF_APPS] = coordinator
|
|
|
|
|
2020-01-18 08:15:06 +00:00
|
|
|
for platform in PLATFORMS:
|
|
|
|
hass.async_create_task(
|
2020-01-23 00:55:34 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, platform)
|
2020-01-18 08:15:06 +00:00
|
|
|
)
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-04-22 14:53:57 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
"""Unload a config entry."""
|
2020-01-18 08:15:06 +00:00
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
2020-01-23 00:55:34 +00:00
|
|
|
hass.config_entries.async_forward_entry_unload(config_entry, platform)
|
2020-01-18 08:15:06 +00:00
|
|
|
for platform in PLATFORMS
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
Add Config Flow support, Device Registry support, available property to vizio component (#30653)
* add config flow support, device registry support, available property
* raise PlatformNotReady if HA cant connect to device
* remove test logging statement and fix integration title
* store import and last user input values so user can see errors next to value that caused error
* add PARALLEL_UPDATES
* add missing type hints
* add missing type hints to tests
* fix options config flow title
* changes based on review
* better key name for message when cant connect
* fix missed update to key name
* fix comments
* remove logger from test which was used to debug and update test function names and docstrings to be more accurate
* add __init__.py to vizio tests module
* readded options flow and updated main component to handle options updates, set unique ID to serial, fixes based on review
* pop hass.data in media_player unload instead of in __init__ since it is set in media_player
* update requirements_all and requirements_test_all
* make unique_id key name a constant
* remove additional line breaks after docstrings
* unload entries during test_user_flow and test_import_flow tests to hopefully reduce teardown time
* try to speed up tests
* remove unnecessary code, use event bus to track options updates, move patches to pytest fixtures and fix patch scoping
* fix comment
* remove translations from commit
* suppress API error logging when checking for device availability as it can spam logs
* update requirements_all and requirements_test_all
* dont pass hass to entity since it is passed to entity anyway, remove entity unload from tests, other misc changes from review
* fix clearing listeners
* use config_entry unique ID for unique ID and use config_entry entry ID as update signal
* update config flow based on suggested changes
* update volume step on config import if it doesn't match config_entry volume step
* update config_entry data and options with new volume step value
* copy entry.data and entry.options before updating when updating config_entry
* fix test_import_entity_already_configured
2020-01-15 10:43:55 +00:00
|
|
|
|
2020-09-02 09:55:10 +00:00
|
|
|
# Exclude this config entry because its not unloaded yet
|
|
|
|
if not any(
|
|
|
|
entry.state == ENTRY_STATE_LOADED
|
|
|
|
and entry.entry_id != config_entry.entry_id
|
|
|
|
and entry.data[CONF_DEVICE_CLASS] == DEVICE_CLASS_TV
|
|
|
|
for entry in hass.config_entries.async_entries(DOMAIN)
|
|
|
|
):
|
2020-09-18 11:18:56 +00:00
|
|
|
hass.data[DOMAIN].pop(CONF_APPS, None)
|
2020-09-02 09:55:10 +00:00
|
|
|
|
|
|
|
if not hass.data[DOMAIN]:
|
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
|
2020-01-18 08:15:06 +00:00
|
|
|
return unload_ok
|
2020-09-02 09:55:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VizioAppsDataUpdateCoordinator(DataUpdateCoordinator):
|
|
|
|
"""Define an object to hold Vizio app config data."""
|
|
|
|
|
2021-04-22 14:53:57 +00:00
|
|
|
def __init__(self, hass: HomeAssistant) -> None:
|
2020-09-02 09:55:10 +00:00
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=DOMAIN,
|
|
|
|
update_interval=timedelta(days=1),
|
|
|
|
update_method=self._async_update_data,
|
|
|
|
)
|
|
|
|
self.data = APPS
|
|
|
|
|
2021-03-18 13:43:52 +00:00
|
|
|
async def _async_update_data(self) -> list[dict[str, Any]]:
|
2020-09-02 09:55:10 +00:00
|
|
|
"""Update data via library."""
|
|
|
|
data = await gen_apps_list_from_url(session=async_get_clientsession(self.hass))
|
|
|
|
if not data:
|
|
|
|
raise UpdateFailed
|
|
|
|
return sorted(data, key=lambda app: app["name"])
|