Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The vizio component."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant.components.media_player import MediaPlayerDeviceClass
10 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry, ConfigEntryState
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import config_validation as cv
14 from homeassistant.helpers.storage import Store
15 from homeassistant.helpers.typing import ConfigType
16 
17 from .const import CONF_APPS, CONF_DEVICE_CLASS, DOMAIN, VIZIO_SCHEMA
18 from .coordinator import VizioAppsDataUpdateCoordinator
19 
20 
21 def validate_apps(config: ConfigType) -> ConfigType:
22  """Validate CONF_APPS is only used when CONF_DEVICE_CLASS is MediaPlayerDeviceClass.TV."""
23  if (
24  config.get(CONF_APPS) is not None
25  and config[CONF_DEVICE_CLASS] != MediaPlayerDeviceClass.TV
26  ):
27  raise vol.Invalid(
28  f"'{CONF_APPS}' can only be used if {CONF_DEVICE_CLASS}' is"
29  f" '{MediaPlayerDeviceClass.TV}'"
30  )
31 
32  return config
33 
34 
35 CONFIG_SCHEMA = vol.Schema(
36  {DOMAIN: vol.All(cv.ensure_list, [vol.All(VIZIO_SCHEMA, validate_apps)])},
37  extra=vol.ALLOW_EXTRA,
38 )
39 
40 PLATFORMS = [Platform.MEDIA_PLAYER]
41 
42 
43 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
44  """Component setup, run import config flow for each entry in config."""
45  if DOMAIN in config:
46  for entry in config[DOMAIN]:
47  hass.async_create_task(
48  hass.config_entries.flow.async_init(
49  DOMAIN, context={"source": SOURCE_IMPORT}, data=entry
50  )
51  )
52 
53  return True
54 
55 
56 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
57  """Load the saved entities."""
58 
59  hass.data.setdefault(DOMAIN, {})
60  if (
61  CONF_APPS not in hass.data[DOMAIN]
62  and entry.data[CONF_DEVICE_CLASS] == MediaPlayerDeviceClass.TV
63  ):
64  store: Store[list[dict[str, Any]]] = Store(hass, 1, DOMAIN)
65  coordinator = VizioAppsDataUpdateCoordinator(hass, store)
66  await coordinator.async_config_entry_first_refresh()
67  hass.data[DOMAIN][CONF_APPS] = coordinator
68 
69  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
70 
71  return True
72 
73 
74 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
75  """Unload a config entry."""
76  unload_ok = await hass.config_entries.async_unload_platforms(
77  config_entry, PLATFORMS
78  )
79  # Exclude this config entry because its not unloaded yet
80  if not any(
81  entry.state is ConfigEntryState.LOADED
82  and entry.entry_id != config_entry.entry_id
83  and entry.data[CONF_DEVICE_CLASS] == MediaPlayerDeviceClass.TV
84  for entry in hass.config_entries.async_entries(DOMAIN)
85  ):
86  hass.data[DOMAIN].pop(CONF_APPS, None)
87 
88  if not hass.data[DOMAIN]:
89  hass.data.pop(DOMAIN)
90 
91  return unload_ok
ConfigType validate_apps(ConfigType config)
Definition: __init__.py:21
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:43
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:74
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:56