From e3e7fb5ff61b217c1c629e610b105481882d8d22 Mon Sep 17 00:00:00 2001 From: Andrew Sayre <6730289+andrewsayre@users.noreply.github.com> Date: Wed, 24 Apr 2019 23:31:32 -0500 Subject: [PATCH] Bump pyheos to 0.4.1 (#23360) * Bump pyheos==0.4.1 * Refresh player after reconnection --- homeassistant/components/heos/manifest.json | 2 +- homeassistant/components/heos/media_player.py | 10 +++++- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/heos/test_media_player.py | 31 +++++++++++++++++-- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/heos/manifest.json b/homeassistant/components/heos/manifest.json index 97b53935614..5b0a8e67893 100644 --- a/homeassistant/components/heos/manifest.json +++ b/homeassistant/components/heos/manifest.json @@ -3,7 +3,7 @@ "name": "Heos", "documentation": "https://www.home-assistant.io/components/heos", "requirements": [ - "pyheos==0.4.0" + "pyheos==0.4.1" ], "dependencies": [], "codeowners": [ diff --git a/homeassistant/components/heos/media_player.py b/homeassistant/components/heos/media_player.py index 56e9647df50..ae1b1c32003 100644 --- a/homeassistant/components/heos/media_player.py +++ b/homeassistant/components/heos/media_player.py @@ -1,4 +1,5 @@ """Denon HEOS Media Player.""" +import asyncio from functools import reduce, wraps import logging from operator import ior @@ -48,7 +49,7 @@ def log_command_error(command: str): from pyheos import CommandError try: await func(*args, **kwargs) - except CommandError as ex: + except (CommandError, asyncio.TimeoutError, ConnectionError) as ex: _LOGGER.error("Unable to %s: %s", command, ex) return wrapper return decorator @@ -86,6 +87,13 @@ class HeosMediaPlayer(MediaPlayerDevice): async def _heos_event(self, event): """Handle connection event.""" + from pyheos import CommandError, const + if event == const.EVENT_CONNECTED: + try: + await self._player.refresh() + except (CommandError, asyncio.TimeoutError, ConnectionError) as ex: + _LOGGER.error("Unable to refresh player %s: %s", + self._player, ex) await self.async_update_ha_state(True) async def _player_update(self, player_id, event): diff --git a/requirements_all.txt b/requirements_all.txt index 23d0c480074..902e921b74e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1079,7 +1079,7 @@ pygtt==1.1.2 pyhaversion==2.2.1 # homeassistant.components.heos -pyheos==0.4.0 +pyheos==0.4.1 # homeassistant.components.hikvision pyhik==0.2.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index cd20177bcdd..dd44ba61575 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -223,7 +223,7 @@ pydeconz==54 pydispatcher==2.0.5 # homeassistant.components.heos -pyheos==0.4.0 +pyheos==0.4.1 # homeassistant.components.homematic pyhomematic==0.1.58 diff --git a/tests/components/heos/test_media_player.py b/tests/components/heos/test_media_player.py index 4cf871f5ed0..e3e02110258 100644 --- a/tests/components/heos/test_media_player.py +++ b/tests/components/heos/test_media_player.py @@ -109,13 +109,40 @@ async def test_updates_start_from_signals( state = hass.states.get('media_player.test_player') assert state.state == STATE_UNAVAILABLE - # Test heos events update + +async def test_updates_from_connection_event( + hass, config_entry, config, controller, input_sources, caplog): + """Tests player updates from connection event after connection failure.""" + # Connected + await setup_platform(hass, config_entry, config) + player = controller.players[1] player.available = True player.heos.dispatcher.send( const.SIGNAL_HEOS_EVENT, const.EVENT_CONNECTED) await hass.async_block_till_done() state = hass.states.get('media_player.test_player') - assert state.state == STATE_PLAYING + assert state.state == STATE_IDLE + assert player.refresh.call_count == 1 + + # Connected handles refresh failure + player.reset_mock() + player.refresh.side_effect = CommandError(None, "Failure", 1) + player.heos.dispatcher.send( + const.SIGNAL_HEOS_EVENT, const.EVENT_CONNECTED) + await hass.async_block_till_done() + state = hass.states.get('media_player.test_player') + assert player.refresh.call_count == 1 + assert "Unable to refresh player" in caplog.text + + # Disconnected + player.reset_mock() + player.available = False + player.heos.dispatcher.send( + const.SIGNAL_HEOS_EVENT, const.EVENT_DISCONNECTED) + await hass.async_block_till_done() + state = hass.states.get('media_player.test_player') + assert state.state == STATE_UNAVAILABLE + assert player.refresh.call_count == 0 async def test_updates_from_sources_updated(