mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Catch UnicodeDecodeError exceptions in 'androidtv.adb_command' service (#30538)
* Catch UnicodeDecodeError exceptions in 'androidtv.adb_command' service * Replace "adb_command" with SERVICE_ADB_COMMAND
This commit is contained in:
parent
98bb400f3a
commit
73af75cb41
@ -528,7 +528,13 @@ class ADBDevice(MediaPlayerDevice):
|
|||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
return self._adb_response
|
return self._adb_response
|
||||||
|
|
||||||
|
try:
|
||||||
response = self.aftv.adb_shell(cmd)
|
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():
|
if isinstance(response, str) and response.strip():
|
||||||
self._adb_response = response.strip()
|
self._adb_response = response.strip()
|
||||||
else:
|
else:
|
||||||
|
@ -623,6 +623,34 @@ async def test_adb_command(hass):
|
|||||||
assert state.attributes["adb_response"] == response
|
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):
|
async def test_adb_command_key(hass):
|
||||||
"""Test sending a key command via the `androidtv.adb_command` service."""
|
"""Test sending a key command via the `androidtv.adb_command` service."""
|
||||||
patch_key = "server"
|
patch_key = "server"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user