Fix unloading of Monoprice config entries (#35112)
This commit is contained in:
parent
a44724fdcc
commit
e0bcd0049c
3 changed files with 22 additions and 5 deletions
|
@ -10,7 +10,7 @@ from homeassistant.const import CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN, MONOPRICE_OBJECT, UNDO_UPDATE_LISTENER
|
||||||
|
|
||||||
PLATFORMS = ["media_player"]
|
PLATFORMS = ["media_player"]
|
||||||
|
|
||||||
|
@ -28,12 +28,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
monoprice = await hass.async_add_executor_job(get_monoprice, port)
|
monoprice = await hass.async_add_executor_job(get_monoprice, port)
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = monoprice
|
|
||||||
except SerialException:
|
except SerialException:
|
||||||
_LOGGER.error("Error connecting to Monoprice controller at %s", port)
|
_LOGGER.error("Error connecting to Monoprice controller at %s", port)
|
||||||
raise ConfigEntryNotReady
|
raise ConfigEntryNotReady
|
||||||
|
|
||||||
entry.add_update_listener(_update_listener)
|
undo_listener = entry.add_update_listener(_update_listener)
|
||||||
|
|
||||||
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
|
||||||
|
MONOPRICE_OBJECT: monoprice,
|
||||||
|
UNDO_UPDATE_LISTENER: undo_listener,
|
||||||
|
}
|
||||||
|
|
||||||
for component in PLATFORMS:
|
for component in PLATFORMS:
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
|
@ -54,6 +58,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if unload_ok:
|
||||||
|
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
|
||||||
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,3 +13,6 @@ CONF_SOURCE_6 = "source_6"
|
||||||
|
|
||||||
SERVICE_SNAPSHOT = "snapshot"
|
SERVICE_SNAPSHOT = "snapshot"
|
||||||
SERVICE_RESTORE = "restore"
|
SERVICE_RESTORE = "restore"
|
||||||
|
|
||||||
|
MONOPRICE_OBJECT = "monoprice_object"
|
||||||
|
UNDO_UPDATE_LISTENER = "update_update_listener"
|
||||||
|
|
|
@ -16,7 +16,13 @@ from homeassistant.components.media_player.const import (
|
||||||
from homeassistant.const import CONF_PORT, STATE_OFF, STATE_ON
|
from homeassistant.const import CONF_PORT, STATE_OFF, STATE_ON
|
||||||
from homeassistant.helpers import config_validation as cv, entity_platform, service
|
from homeassistant.helpers import config_validation as cv, entity_platform, service
|
||||||
|
|
||||||
from .const import CONF_SOURCES, DOMAIN, SERVICE_RESTORE, SERVICE_SNAPSHOT
|
from .const import (
|
||||||
|
CONF_SOURCES,
|
||||||
|
DOMAIN,
|
||||||
|
MONOPRICE_OBJECT,
|
||||||
|
SERVICE_RESTORE,
|
||||||
|
SERVICE_SNAPSHOT,
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -58,7 +64,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up the Monoprice 6-zone amplifier platform."""
|
"""Set up the Monoprice 6-zone amplifier platform."""
|
||||||
port = config_entry.data[CONF_PORT]
|
port = config_entry.data[CONF_PORT]
|
||||||
|
|
||||||
monoprice = hass.data[DOMAIN][config_entry.entry_id]
|
monoprice = hass.data[DOMAIN][config_entry.entry_id][MONOPRICE_OBJECT]
|
||||||
|
|
||||||
sources = _get_sources(config_entry)
|
sources = _get_sources(config_entry)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue