1 """The Monoprice 6-Zone Amplifier integration."""
5 from pymonoprice
import get_monoprice
6 from serial
import SerialException
21 PLATFORMS = [Platform.MEDIA_PLAYER]
23 _LOGGER = logging.getLogger(__name__)
27 """Set up Monoprice 6-Zone Amplifier from a config entry."""
28 port = entry.data[CONF_PORT]
31 monoprice = await hass.async_add_executor_job(get_monoprice, port)
32 except SerialException
as err:
33 _LOGGER.error(
"Error connecting to Monoprice controller at %s", port)
34 raise ConfigEntryNotReady
from err
37 first_run =
not bool(entry.data.get(CONF_NOT_FIRST_RUN))
40 hass.config_entries.async_update_entry(
41 entry, data={**entry.data, CONF_NOT_FIRST_RUN:
True}
44 undo_listener = entry.add_update_listener(_update_listener)
46 hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
47 MONOPRICE_OBJECT: monoprice,
48 UNDO_UPDATE_LISTENER: undo_listener,
52 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
58 """Unload a config entry."""
59 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
63 hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
65 def _cleanup(monoprice) -> None:
66 """Destroy the Monoprice object.
68 Destroying the Monoprice closes the serial connection, do it in an executor so the garbage
69 collection does not block.
73 monoprice = hass.data[DOMAIN][entry.entry_id][MONOPRICE_OBJECT]
74 hass.data[DOMAIN].pop(entry.entry_id)
76 await hass.async_add_executor_job(_cleanup, monoprice)
82 """Handle options update."""
83 await hass.config_entries.async_reload(entry.entry_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None _update_listener(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)