hass-core/homeassistant/components/anthemav/__init__.py
Alex Henry f5c6a6be3a
Add config flow to AnthemAV integration (#53268)
* Add config flow to AnthemAV integration

* Add importing of existing configuration

* Change setting to optional and add default value

* Use entity attribute

* Reduce changes by removing additional media player properties

* Remove title from translation

* Refactor config flow and fix PR comments

* Fix a failing test because of wrong renaming

* Add typing and use existing class and enum

* Bump dependency to v1.3.1

* Remove unecessary async_reload_entry

* Fix requirements_test_all after rebase

* Add const for timeout and remove async_block in test

* Reapply CodeOwner and configflow after rebase

* Remove name from configflow

* Fix manifest prettier failure

* Simplify code and avoid catching broad exception

* Removed unused strings and translations

* Avoid asserting hass.data
2022-06-30 07:13:08 -07:00

59 lines
1.8 KiB
Python

"""The Anthem A/V Receivers integration."""
from __future__ import annotations
import logging
import anthemav
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import async_dispatcher_send
from .const import ANTHEMAV_UDATE_SIGNAL, DOMAIN
PLATFORMS = [Platform.MEDIA_PLAYER]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Anthem A/V Receivers from a config entry."""
@callback
def async_anthemav_update_callback(message):
"""Receive notification from transport that new data exists."""
_LOGGER.debug("Received update callback from AVR: %s", message)
async_dispatcher_send(hass, f"{ANTHEMAV_UDATE_SIGNAL}_{entry.data[CONF_NAME]}")
try:
avr = await anthemav.Connection.create(
host=entry.data[CONF_HOST],
port=entry.data[CONF_PORT],
update_callback=async_anthemav_update_callback,
)
except OSError as err:
raise ConfigEntryNotReady from err
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = avr
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
avr = hass.data[DOMAIN][entry.entry_id]
if avr is not None:
_LOGGER.debug("Close avr connection")
avr.close()
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok