2019-03-17 03:44:05 +00:00
|
|
|
"""The denonavr component."""
|
2020-06-16 12:46:39 +00:00
|
|
|
import logging
|
|
|
|
|
2021-04-02 17:47:16 +00:00
|
|
|
from denonavr.exceptions import AvrNetworkError, AvrTimoutError
|
2020-01-02 21:30:20 +00:00
|
|
|
|
2020-06-16 12:46:39 +00:00
|
|
|
from homeassistant import config_entries, core
|
2021-04-02 17:47:16 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2020-06-16 12:46:39 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2021-04-02 17:47:16 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from homeassistant.helpers.httpx_client import get_async_client
|
2020-01-02 21:30:20 +00:00
|
|
|
|
2020-06-16 12:46:39 +00:00
|
|
|
from .config_flow import (
|
|
|
|
CONF_SHOW_ALL_SOURCES,
|
|
|
|
CONF_ZONE2,
|
|
|
|
CONF_ZONE3,
|
|
|
|
DEFAULT_SHOW_SOURCES,
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
DEFAULT_ZONE2,
|
|
|
|
DEFAULT_ZONE3,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
|
|
|
from .receiver import ConnectDenonAVR
|
2020-01-02 21:30:20 +00:00
|
|
|
|
2020-06-16 12:46:39 +00:00
|
|
|
CONF_RECEIVER = "receiver"
|
|
|
|
UNDO_UPDATE_LISTENER = "undo_update_listener"
|
2021-04-27 06:46:49 +00:00
|
|
|
PLATFORMS = ["media_player"]
|
2020-01-02 21:30:20 +00:00
|
|
|
|
2020-06-16 12:46:39 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
|
|
|
|
):
|
|
|
|
"""Set up the denonavr components from a config entry."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
# Connect to receiver
|
|
|
|
connect_denonavr = ConnectDenonAVR(
|
|
|
|
entry.data[CONF_HOST],
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
entry.options.get(CONF_SHOW_ALL_SOURCES, DEFAULT_SHOW_SOURCES),
|
|
|
|
entry.options.get(CONF_ZONE2, DEFAULT_ZONE2),
|
|
|
|
entry.options.get(CONF_ZONE3, DEFAULT_ZONE3),
|
2021-04-02 17:47:16 +00:00
|
|
|
lambda: get_async_client(hass),
|
2020-06-16 12:46:39 +00:00
|
|
|
)
|
2021-04-02 17:47:16 +00:00
|
|
|
try:
|
|
|
|
await connect_denonavr.async_connect_receiver()
|
|
|
|
except (AvrNetworkError, AvrTimoutError) as ex:
|
|
|
|
raise ConfigEntryNotReady from ex
|
2020-06-16 12:46:39 +00:00
|
|
|
receiver = connect_denonavr.receiver
|
|
|
|
|
|
|
|
undo_listener = entry.add_update_listener(update_listener)
|
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
CONF_RECEIVER: receiver,
|
|
|
|
UNDO_UPDATE_LISTENER: undo_listener,
|
|
|
|
}
|
|
|
|
|
2021-04-27 06:46:49 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2020-06-16 12:46:39 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(
|
|
|
|
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
|
|
|
|
):
|
|
|
|
"""Unload a config entry."""
|
2021-04-27 06:46:49 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(
|
|
|
|
config_entry, PLATFORMS
|
2020-06-16 12:46:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()
|
|
|
|
|
|
|
|
# Remove zone2 and zone3 entities if needed
|
|
|
|
entity_registry = await er.async_get_registry(hass)
|
|
|
|
entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
|
2021-04-02 17:47:16 +00:00
|
|
|
unique_id = config_entry.unique_id or config_entry.entry_id
|
|
|
|
zone2_id = f"{unique_id}-Zone2"
|
|
|
|
zone3_id = f"{unique_id}-Zone3"
|
2020-06-16 12:46:39 +00:00
|
|
|
for entry in entries:
|
|
|
|
if entry.unique_id == zone2_id and not config_entry.options.get(CONF_ZONE2):
|
|
|
|
entity_registry.async_remove(entry.entity_id)
|
|
|
|
_LOGGER.debug("Removing zone2 from DenonAvr")
|
|
|
|
if entry.unique_id == zone3_id and not config_entry.options.get(CONF_ZONE3):
|
|
|
|
entity_registry.async_remove(entry.entity_id)
|
|
|
|
_LOGGER.debug("Removing zone3 from DenonAvr")
|
|
|
|
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(config_entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
|
|
|
async def update_listener(
|
|
|
|
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
|
|
|
|
):
|
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|