Monoprice PR followups (#33133)

pull/33156/head
On Freund 2020-03-22 14:25:27 +02:00 committed by GitHub
parent 99877c32b1
commit 66402b9b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 21 deletions

View File

@ -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)

View File

@ -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."""

View File

@ -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()

View File

@ -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)