Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The onkyo component."""
2 
3 from dataclasses import dataclass
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_HOST, Platform
7 from homeassistant.core import HomeAssistant
8 from homeassistant.exceptions import ConfigEntryNotReady
9 from homeassistant.helpers import config_validation as cv
10 from homeassistant.helpers.typing import ConfigType
11 
12 from .const import DOMAIN, OPTION_INPUT_SOURCES, InputSource
13 from .receiver import Receiver, async_interview
14 from .services import DATA_MP_ENTITIES, async_register_services
15 
16 PLATFORMS = [Platform.MEDIA_PLAYER]
17 
18 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
19 
20 
21 @dataclass
22 class OnkyoData:
23  """Config Entry data."""
24 
25  receiver: Receiver
26  sources: dict[InputSource, str]
27 
28 
29 type OnkyoConfigEntry = ConfigEntry[OnkyoData]
30 
31 
32 async def async_setup(hass: HomeAssistant, _: ConfigType) -> bool:
33  """Set up Onkyo component."""
34  await async_register_services(hass)
35  return True
36 
37 
38 async def async_setup_entry(hass: HomeAssistant, entry: OnkyoConfigEntry) -> bool:
39  """Set up the Onkyo config entry."""
40  entry.async_on_unload(entry.add_update_listener(update_listener))
41 
42  host = entry.data[CONF_HOST]
43 
44  info = await async_interview(host)
45  if info is None:
46  raise ConfigEntryNotReady(f"Unable to connect to: {host}")
47 
48  receiver = await Receiver.async_create(info)
49 
50  sources_store: dict[str, str] = entry.options[OPTION_INPUT_SOURCES]
51  sources = {InputSource(k): v for k, v in sources_store.items()}
52 
53  entry.runtime_data = OnkyoData(receiver, sources)
54 
55  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
56 
57  await receiver.conn.connect()
58 
59  return True
60 
61 
62 async def async_unload_entry(hass: HomeAssistant, entry: OnkyoConfigEntry) -> bool:
63  """Unload Onkyo config entry."""
64  del hass.data[DATA_MP_ENTITIES][entry.entry_id]
65 
66  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
67 
68  receiver = entry.runtime_data.receiver
69  receiver.conn.close()
70 
71  return unload_ok
72 
73 
74 async def update_listener(hass: HomeAssistant, entry: OnkyoConfigEntry) -> None:
75  """Handle options update."""
76  await hass.config_entries.async_reload(entry.entry_id)
None async_register_services(HomeAssistant hass)
Definition: services.py:80
ReceiverInfo|None async_interview(str host)
Definition: receiver.py:104
bool async_unload_entry(HomeAssistant hass, OnkyoConfigEntry entry)
Definition: __init__.py:62
bool async_setup_entry(HomeAssistant hass, OnkyoConfigEntry entry)
Definition: __init__.py:38
None update_listener(HomeAssistant hass, OnkyoConfigEntry entry)
Definition: __init__.py:74
bool async_setup(HomeAssistant hass, ConfigType _)
Definition: __init__.py:32