Skip to content

Commit

Permalink
Review AndroidTV decorator exception management
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 committed Apr 12, 2024
1 parent 6eaf340 commit 77dffd4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
19 changes: 2 additions & 17 deletions homeassistant/components/androidtv/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
from homeassistant.helpers.entity import Entity

from . import ADB_PYTHON_EXCEPTIONS, ADB_TCP_EXCEPTIONS, get_androidtv_mac
from . import get_androidtv_mac
from .const import DEVICE_ANDROIDTV, DOMAIN

PREFIX_ANDROIDTV = "Android TV"
Expand Down Expand Up @@ -73,7 +73,7 @@ async def _adb_exception_catcher(
func.__name__,
)
return None
except self.exceptions as err:
except Exception as err: # pylint: disable=broad-except
_LOGGER.error(
(
"Failed to execute an ADB command. ADB connection re-"
Expand All @@ -85,13 +85,6 @@ async def _adb_exception_catcher(
# pylint: disable-next=protected-access
self._attr_available = False
return None
except Exception:
# An unforeseen exception occurred. Close the ADB connection so that
# it doesn't happen over and over again, then raise the exception.
await self.aftv.adb_close()
# pylint: disable-next=protected-access
self._attr_available = False
raise

return _adb_exception_catcher

Expand Down Expand Up @@ -136,11 +129,3 @@ def __init__(
self._attr_device_info[ATTR_SW_VERSION] = sw_version
if mac := get_androidtv_mac(info):
self._attr_device_info[ATTR_CONNECTIONS] = {(CONNECTION_NETWORK_MAC, mac)}

# ADB exceptions to catch
if not aftv.adb_server_ip:
# Using "adb_shell" (Python ADB implementation)
self.exceptions = ADB_PYTHON_EXCEPTIONS
else:
# Using "pure-python-adb" (communicate with ADB server)
self.exceptions = ADB_TCP_EXCEPTIONS
16 changes: 9 additions & 7 deletions tests/components/androidtv/test_media_player.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""The tests for the androidtv platform."""

from collections.abc import Generator
from datetime import timedelta
import logging
from typing import Any
Expand Down Expand Up @@ -170,7 +171,7 @@


@pytest.fixture(autouse=True)
def adb_device_tcp_fixture() -> None:
def adb_device_tcp_fixture() -> Generator[None, patchers.AdbDeviceTcpAsyncFake, None]:
"""Patch ADB Device TCP."""
with patch(
"androidtv.adb_manager.adb_manager_async.AdbDeviceTcpAsync",
Expand All @@ -180,7 +181,7 @@ def adb_device_tcp_fixture() -> None:


@pytest.fixture(autouse=True)
def load_adbkey_fixture() -> None:
def load_adbkey_fixture() -> Generator[None, str, None]:
"""Patch load_adbkey."""
with patch(
"homeassistant.components.androidtv.ADBPythonSync.load_adbkey",
Expand All @@ -190,7 +191,7 @@ def load_adbkey_fixture() -> None:


@pytest.fixture(autouse=True)
def keygen_fixture() -> None:
def keygen_fixture() -> Generator[None, Mock, None]:
"""Patch keygen."""
with patch(
"homeassistant.components.androidtv.keygen",
Expand Down Expand Up @@ -1202,11 +1203,12 @@ async def test_exception(hass: HomeAssistant) -> None:
assert state.state == STATE_OFF

# When an unforeseen exception occurs, we close the ADB connection and raise the exception
with patchers.PATCH_ANDROIDTV_UPDATE_EXCEPTION, pytest.raises(Exception):
with patchers.PATCH_ANDROIDTV_UPDATE_EXCEPTION:
await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNAVAILABLE

state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNAVAILABLE

# On the next update, HA will reconnect to the device
await async_update_entity(hass, entity_id)
Expand Down

0 comments on commit 77dffd4

Please sign in to comment.