diff --git a/homeassistant/components/monoprice/__init__.py b/homeassistant/components/monoprice/__init__.py index 7845c70f1a8d80ed2d89c304b7d1c8eb66c390e7..d18229e3d09bba95479b7e6f84803a3dd6dd39a5 100644 --- a/homeassistant/components/monoprice/__init__.py +++ b/homeassistant/components/monoprice/__init__.py @@ -1,11 +1,21 @@ """The Monoprice 6-Zone Amplifier integration.""" import asyncio +import logging + +from pymonoprice import get_monoprice +from serial import SerialException from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_PORT from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady + +from .const import DOMAIN PLATFORMS = ["media_player"] +_LOGGER = logging.getLogger(__name__) + async def async_setup(hass: HomeAssistant, config: dict): """Set up the Monoprice 6-Zone Amplifier component.""" @@ -14,6 +24,15 @@ async def async_setup(hass: HomeAssistant, config: dict): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Monoprice 6-Zone Amplifier from a config entry.""" + port = entry.data[CONF_PORT] + + 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 + for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component) diff --git a/homeassistant/components/monoprice/config_flow.py b/homeassistant/components/monoprice/config_flow.py index 1ff02b3529f664d7ce2f986c6cc3c19a94830d5d..45434fac13165aef16cd09ebaeb632149f420885 100644 --- a/homeassistant/components/monoprice/config_flow.py +++ b/homeassistant/components/monoprice/config_flow.py @@ -89,7 +89,3 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): class CannotConnect(exceptions.HomeAssistantError): """Error to indicate we cannot connect.""" - - -class InvalidAuth(exceptions.HomeAssistantError): - """Error to indicate there is invalid auth.""" diff --git a/homeassistant/components/monoprice/media_player.py b/homeassistant/components/monoprice/media_player.py index e0585705ad8cd039b15aa9cdc6361ee6635a34dc..6e898cd6d4c7e7ddf9af3dd6c1d0ed28aca40965 100644 --- a/homeassistant/components/monoprice/media_player.py +++ b/homeassistant/components/monoprice/media_player.py @@ -1,9 +1,6 @@ """Support for interfacing with Monoprice 6 zone home audio controller.""" import logging -from pymonoprice import get_monoprice -from serial import SerialException - from homeassistant.components.media_player import MediaPlayerDevice from homeassistant.components.media_player.const import ( SUPPORT_SELECT_SOURCE, @@ -40,28 +37,24 @@ def _get_sources(sources_config): return [source_id_name, source_name_id, source_names] -async def async_setup_entry(hass, config_entry, async_add_devices): +async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the Monoprice 6-zone amplifier platform.""" - port = config_entry.data.get(CONF_PORT) + port = config_entry.data[CONF_PORT] - try: - monoprice = await hass.async_add_executor_job(get_monoprice, port) - except SerialException: - _LOGGER.error("Error connecting to Monoprice controller") - return + monoprice = hass.data[DOMAIN][config_entry.entry_id] sources = _get_sources(config_entry.data.get(CONF_SOURCES)) - devices = [] + entities = [] for i in range(1, 4): for j in range(1, 7): zone_id = (i * 10) + j _LOGGER.info("Adding zone %d for port %s", zone_id, port) - devices.append( + entities.append( MonopriceZone(monoprice, sources, config_entry.entry_id, zone_id) ) - async_add_devices(devices, True) + async_add_entities(entities, True) platform = entity_platform.current_platform.get() diff --git a/tests/components/monoprice/test_media_player.py b/tests/components/monoprice/test_media_player.py index 80c6f7db169b7f60f3311179659d0dc5fa1cd5fc..2aad854652ba1885bff47a3bc0f99b73ba0d3cce 100644 --- a/tests/components/monoprice/test_media_player.py +++ b/tests/components/monoprice/test_media_player.py @@ -94,8 +94,7 @@ async def test_cannot_connect(hass): """Test connection error.""" with patch( - "homeassistant.components.monoprice.media_player.get_monoprice", - side_effect=SerialException, + "homeassistant.components.monoprice.get_monoprice", side_effect=SerialException, ): config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG) config_entry.add_to_hass(hass) @@ -108,8 +107,7 @@ async def test_cannot_connect(hass): async def _setup_monoprice(hass, monoprice): with patch( - "homeassistant.components.monoprice.media_player.get_monoprice", - new=lambda *a: monoprice, + "homeassistant.components.monoprice.get_monoprice", new=lambda *a: monoprice, ): config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG) config_entry.add_to_hass(hass)