Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions homeassistant/components/monoprice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady

from .const import DOMAIN
from .const import DOMAIN, MONOPRICE_OBJECT, UNDO_UPDATE_LISTENER

PLATFORMS = ["media_player"]

Expand All @@ -28,12 +28,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

try:
monoprice = await hass.async_add_executor_job(get_monoprice, port)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = monoprice
except SerialException:
_LOGGER.error("Error connecting to Monoprice controller at %s", port)
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:
hass.async_create_task(
Expand All @@ -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


Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/monoprice/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

SERVICE_SNAPSHOT = "snapshot"
SERVICE_RESTORE = "restore"

MONOPRICE_OBJECT = "monoprice_object"
UNDO_UPDATE_LISTENER = "update_update_listener"
10 changes: 8 additions & 2 deletions homeassistant/components/monoprice/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
from homeassistant.const import CONF_PORT, STATE_OFF, STATE_ON
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__)

Expand Down Expand Up @@ -58,7 +64,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Monoprice 6-zone amplifier platform."""
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)

Expand Down