From 73af75cb41ab190acc21fc3e17a1e98adac7c3eb Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Tue, 7 Jan 2020 04:30:34 -0800 Subject: [PATCH] Catch UnicodeDecodeError exceptions in 'androidtv.adb_command' service (#30538) * Catch UnicodeDecodeError exceptions in 'androidtv.adb_command' service * Replace "adb_command" with SERVICE_ADB_COMMAND --- .../components/androidtv/media_player.py | 8 +++++- .../components/androidtv/test_media_player.py | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/androidtv/media_player.py b/homeassistant/components/androidtv/media_player.py index 24f8282f1b9..63b27f17bb2 100644 --- a/homeassistant/components/androidtv/media_player.py +++ b/homeassistant/components/androidtv/media_player.py @@ -528,7 +528,13 @@ class ADBDevice(MediaPlayerDevice): self.schedule_update_ha_state() return self._adb_response - response = self.aftv.adb_shell(cmd) + try: + response = self.aftv.adb_shell(cmd) + except UnicodeDecodeError: + self._adb_response = None + self.schedule_update_ha_state() + return + if isinstance(response, str) and response.strip(): self._adb_response = response.strip() else: diff --git a/tests/components/androidtv/test_media_player.py b/tests/components/androidtv/test_media_player.py index b4030172939..0aaa870c57b 100644 --- a/tests/components/androidtv/test_media_player.py +++ b/tests/components/androidtv/test_media_player.py @@ -623,6 +623,34 @@ async def test_adb_command(hass): assert state.attributes["adb_response"] == response +async def test_adb_command_unicode_decode_error(hass): + """Test sending a command via the `androidtv.adb_command` service that raises a UnicodeDecodeError exception.""" + patch_key, entity_id = _setup(CONFIG_ANDROIDTV_ADB_SERVER) + command = "test command" + response = b"test response" + + with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[ + patch_key + ], patchers.patch_shell("")[patch_key]: + assert await async_setup_component(hass, DOMAIN, CONFIG_ANDROIDTV_ADB_SERVER) + + with patch( + "androidtv.basetv.BaseTV.adb_shell", + side_effect=UnicodeDecodeError("utf-8", response, 0, len(response), "TEST"), + ): + await hass.services.async_call( + ANDROIDTV_DOMAIN, + SERVICE_ADB_COMMAND, + {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: command}, + blocking=True, + ) + + # patch_shell.assert_called_with(command) + state = hass.states.get(entity_id) + assert state is not None + assert state.attributes["adb_response"] is None + + async def test_adb_command_key(hass): """Test sending a key command via the `androidtv.adb_command` service.""" patch_key = "server"