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
19 changes: 19 additions & 0 deletions homeassistant/components/monoprice/__init__.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand All @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions homeassistant/components/monoprice/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,3 @@ async def async_step_user(self, user_input=None):

class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""


class InvalidAuth(exceptions.HomeAssistantError):
"""Error to indicate there is invalid auth."""
19 changes: 6 additions & 13 deletions homeassistant/components/monoprice/media_player.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 2 additions & 4 deletions tests/components/monoprice/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down