Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The denonavr component."""
2 
3 import logging
4 
5 from denonavr import DenonAVR
6 from denonavr.exceptions import AvrNetworkError, AvrTimoutError
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STOP, Platform
10 from homeassistant.core import Event, HomeAssistant
11 from homeassistant.exceptions import ConfigEntryNotReady
12 from homeassistant.helpers import entity_registry as er
13 from homeassistant.helpers.httpx_client import get_async_client
14 
15 from .config_flow import (
16  CONF_SHOW_ALL_SOURCES,
17  CONF_UPDATE_AUDYSSEY,
18  CONF_USE_TELNET,
19  CONF_ZONE2,
20  CONF_ZONE3,
21  DEFAULT_SHOW_SOURCES,
22  DEFAULT_TIMEOUT,
23  DEFAULT_UPDATE_AUDYSSEY,
24  DEFAULT_USE_TELNET,
25  DEFAULT_ZONE2,
26  DEFAULT_ZONE3,
27  DOMAIN,
28 )
29 from .receiver import ConnectDenonAVR
30 
31 CONF_RECEIVER = "receiver"
32 UNDO_UPDATE_LISTENER = "undo_update_listener"
33 PLATFORMS = [Platform.MEDIA_PLAYER]
34 
35 _LOGGER = logging.getLogger(__name__)
36 
37 
38 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
39  """Set up the denonavr components from a config entry."""
40  hass.data.setdefault(DOMAIN, {})
41 
42  # Connect to receiver
43  connect_denonavr = ConnectDenonAVR(
44  entry.data[CONF_HOST],
45  DEFAULT_TIMEOUT,
46  entry.options.get(CONF_SHOW_ALL_SOURCES, DEFAULT_SHOW_SOURCES),
47  entry.options.get(CONF_ZONE2, DEFAULT_ZONE2),
48  entry.options.get(CONF_ZONE3, DEFAULT_ZONE3),
49  entry.options.get(CONF_USE_TELNET, DEFAULT_USE_TELNET),
50  entry.options.get(CONF_UPDATE_AUDYSSEY, DEFAULT_UPDATE_AUDYSSEY),
51  lambda: get_async_client(hass),
52  )
53  try:
54  await connect_denonavr.async_connect_receiver()
55  except (AvrNetworkError, AvrTimoutError) as ex:
56  raise ConfigEntryNotReady from ex
57  receiver = connect_denonavr.receiver
58 
59  undo_listener = entry.add_update_listener(update_listener)
60 
61  hass.data[DOMAIN][entry.entry_id] = {
62  CONF_RECEIVER: receiver,
63  UNDO_UPDATE_LISTENER: undo_listener,
64  }
65 
66  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
67  use_telnet = entry.options.get(CONF_USE_TELNET, DEFAULT_USE_TELNET)
68 
69  async def _async_disconnect(event: Event) -> None:
70  """Disconnect from Telnet."""
71  if use_telnet and receiver is not None:
72  await receiver.async_telnet_disconnect()
73 
74  if use_telnet:
75  entry.async_on_unload(
76  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_disconnect)
77  )
78 
79  return True
80 
81 
82 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
83  """Unload a config entry."""
84  unload_ok = await hass.config_entries.async_unload_platforms(
85  config_entry, PLATFORMS
86  )
87 
88  if config_entry.options.get(CONF_USE_TELNET, DEFAULT_USE_TELNET):
89  receiver: DenonAVR = hass.data[DOMAIN][config_entry.entry_id][CONF_RECEIVER]
90  await receiver.async_telnet_disconnect()
91 
92  hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()
93 
94  # Remove zone2 and zone3 entities if needed
95  entity_registry = er.async_get(hass)
96  entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
97  unique_id = config_entry.unique_id or config_entry.entry_id
98  zone2_id = f"{unique_id}-Zone2"
99  zone3_id = f"{unique_id}-Zone3"
100  for entry in entries:
101  if entry.unique_id == zone2_id and not config_entry.options.get(CONF_ZONE2):
102  entity_registry.async_remove(entry.entity_id)
103  _LOGGER.debug("Removing zone2 from DenonAvr")
104  if entry.unique_id == zone3_id and not config_entry.options.get(CONF_ZONE3):
105  entity_registry.async_remove(entry.entity_id)
106  _LOGGER.debug("Removing zone3 from DenonAvr")
107 
108  if unload_ok:
109  hass.data[DOMAIN].pop(config_entry.entry_id)
110 
111  return unload_ok
112 
113 
114 async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
115  """Handle options update."""
116  await hass.config_entries.async_reload(config_entry.entry_id)
None update_listener(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:114
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:38
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:82
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)
Definition: httpx_client.py:41