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
15 changes: 12 additions & 3 deletions homeassistant/components/monoprice/media_player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Support for interfacing with Monoprice 6 zone home audio controller."""
import logging

from serial import SerialException

from homeassistant import core
from homeassistant.components.media_player import MediaPlayerDevice
from homeassistant.components.media_player.const import (
Expand All @@ -18,6 +20,8 @@

_LOGGER = logging.getLogger(__name__)

PARALLEL_UPDATES = 1

SUPPORT_MONOPRICE = (
SUPPORT_VOLUME_MUTE
| SUPPORT_VOLUME_SET
Expand Down Expand Up @@ -127,9 +131,15 @@ def __init__(self, monoprice, sources, namespace, zone_id):

def update(self):
"""Retrieve latest state."""
state = self._monoprice.zone_status(self._zone_id)
try:
state = self._monoprice.zone_status(self._zone_id)
except SerialException:
_LOGGER.warning("Could not update zone %d", self._zone_id)
return

if not state:
return False
return

self._state = STATE_ON if state.power else STATE_OFF
self._volume = state.volume
self._mute = state.mute
Expand All @@ -138,7 +148,6 @@ def update(self):
self._source = self._source_id_name[idx]
else:
self._source = None
return True

@property
def entity_registry_enabled_default(self):
Expand Down
52 changes: 52 additions & 0 deletions tests/components/monoprice/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,58 @@ async def test_update(hass):
assert state.attributes[ATTR_INPUT_SOURCE] == "three"


async def test_failed_update(hass):
"""Test updating failure from monoprice."""
monoprice = MockMonoprice()
await _setup_monoprice(hass, monoprice)

# Changing media player to new state
await _call_media_player_service(
hass, SERVICE_VOLUME_SET, {"entity_id": ZONE_1_ID, "volume_level": 0.0}
)
await _call_media_player_service(
hass, SERVICE_SELECT_SOURCE, {"entity_id": ZONE_1_ID, "source": "one"}
)

monoprice.set_source(11, 3)
monoprice.set_volume(11, 38)

with patch.object(MockMonoprice, "zone_status", side_effect=SerialException):
await async_update_entity(hass, ZONE_1_ID)
await hass.async_block_till_done()

state = hass.states.get(ZONE_1_ID)

assert state.attributes[ATTR_MEDIA_VOLUME_LEVEL] == 0.0
assert state.attributes[ATTR_INPUT_SOURCE] == "one"


async def test_empty_update(hass):
"""Test updating with no state from monoprice."""
monoprice = MockMonoprice()
await _setup_monoprice(hass, monoprice)

# Changing media player to new state
await _call_media_player_service(
hass, SERVICE_VOLUME_SET, {"entity_id": ZONE_1_ID, "volume_level": 0.0}
)
await _call_media_player_service(
hass, SERVICE_SELECT_SOURCE, {"entity_id": ZONE_1_ID, "source": "one"}
)

monoprice.set_source(11, 3)
monoprice.set_volume(11, 38)

with patch.object(MockMonoprice, "zone_status", return_value=None):
await async_update_entity(hass, ZONE_1_ID)
await hass.async_block_till_done()

state = hass.states.get(ZONE_1_ID)

assert state.attributes[ATTR_MEDIA_VOLUME_LEVEL] == 0.0
assert state.attributes[ATTR_INPUT_SOURCE] == "one"


async def test_supported_features(hass):
"""Test supported features property."""
await _setup_monoprice(hass, MockMonoprice())
Expand Down