From 72fdcd1cb137ce44333ef5a242b659d56024c6cd Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 26 Jul 2024 12:29:47 +0200 Subject: [PATCH] Final steps to runtime_data in Axis integration (#122641) * Rework connection error test to check config entry status * Remove final dependencies to hass.data[AXIS_DOMAIN] --- homeassistant/components/axis/__init__.py | 4 +- tests/components/axis/test_camera.py | 18 +---- tests/components/axis/test_hub.py | 84 +++++++---------------- 3 files changed, 28 insertions(+), 78 deletions(-) diff --git a/homeassistant/components/axis/__init__.py b/homeassistant/components/axis/__init__.py index 94752182d10..f1d8d1d4b63 100644 --- a/homeassistant/components/axis/__init__.py +++ b/homeassistant/components/axis/__init__.py @@ -7,7 +7,7 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from .const import DOMAIN as AXIS_DOMAIN, PLATFORMS +from .const import PLATFORMS from .errors import AuthenticationRequired, CannotConnect from .hub import AxisHub, get_axis_api @@ -18,8 +18,6 @@ type AxisConfigEntry = ConfigEntry[AxisHub] async def async_setup_entry(hass: HomeAssistant, config_entry: AxisConfigEntry) -> bool: """Set up the Axis integration.""" - hass.data.setdefault(AXIS_DOMAIN, {}) - try: api = await get_axis_api(hass, config_entry.data) except CannotConnect as err: diff --git a/tests/components/axis/test_camera.py b/tests/components/axis/test_camera.py index c1590717983..00fe4391b0c 100644 --- a/tests/components/axis/test_camera.py +++ b/tests/components/axis/test_camera.py @@ -3,30 +3,14 @@ import pytest from homeassistant.components import camera -from homeassistant.components.axis.const import ( - CONF_STREAM_PROFILE, - DOMAIN as AXIS_DOMAIN, -) +from homeassistant.components.axis.const import CONF_STREAM_PROFILE from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN from homeassistant.const import STATE_IDLE from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from .const import MAC, NAME -async def test_platform_manually_configured(hass: HomeAssistant) -> None: - """Test that nothing happens when platform is manually configured.""" - assert ( - await async_setup_component( - hass, CAMERA_DOMAIN, {CAMERA_DOMAIN: {"platform": AXIS_DOMAIN}} - ) - is True - ) - - assert AXIS_DOMAIN not in hass.data - - @pytest.mark.usefixtures("config_entry_setup") async def test_camera(hass: HomeAssistant) -> None: """Test that Axis camera platform is loaded properly.""" diff --git a/tests/components/axis/test_hub.py b/tests/components/axis/test_hub.py index d7f303539e4..d0911ed6adb 100644 --- a/tests/components/axis/test_hub.py +++ b/tests/components/axis/test_hub.py @@ -14,7 +14,7 @@ from syrupy import SnapshotAssertion from homeassistant.components import axis, zeroconf from homeassistant.components.axis.const import DOMAIN as AXIS_DOMAIN from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN -from homeassistant.config_entries import SOURCE_ZEROCONF +from homeassistant.config_entries import SOURCE_ZEROCONF, ConfigEntryState from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr @@ -146,18 +146,6 @@ async def test_device_unavailable( assert hass.states.get(f"{BINARY_SENSOR_DOMAIN}.{NAME}_sound_1").state == STATE_OFF -@pytest.mark.usefixtures("mock_default_requests") -async def test_device_not_accessible( - hass: HomeAssistant, config_entry: MockConfigEntry -) -> None: - """Failed setup schedules a retry of setup.""" - config_entry.add_to_hass(hass) - with patch.object(axis, "get_axis_api", side_effect=axis.errors.CannotConnect): - await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() - assert hass.data[AXIS_DOMAIN] == {} - - @pytest.mark.usefixtures("mock_default_requests") async def test_device_trigger_reauth_flow( hass: HomeAssistant, config_entry: MockConfigEntry @@ -173,19 +161,7 @@ async def test_device_trigger_reauth_flow( await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() mock_flow_init.assert_called_once() - assert hass.data[AXIS_DOMAIN] == {} - - -@pytest.mark.usefixtures("mock_default_requests") -async def test_device_unknown_error( - hass: HomeAssistant, config_entry: MockConfigEntry -) -> None: - """Unknown errors are handled.""" - config_entry.add_to_hass(hass) - with patch.object(axis, "get_axis_api", side_effect=Exception): - await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() - assert hass.data[AXIS_DOMAIN] == {} + assert config_entry.state == ConfigEntryState.SETUP_ERROR async def test_shutdown(config_entry_data: MappingProxyType[str, Any]) -> None: @@ -203,36 +179,28 @@ async def test_shutdown(config_entry_data: MappingProxyType[str, Any]) -> None: assert len(axis_device.api.stream.stop.mock_calls) == 1 -async def test_get_device_fails( - hass: HomeAssistant, config_entry_data: MappingProxyType[str, Any] +@pytest.mark.parametrize( + ("side_effect", "state"), + [ + # Device unauthorized yields authentication required error + (axislib.Unauthorized, ConfigEntryState.SETUP_ERROR), + # Device unavailable yields cannot connect error + (TimeoutError, ConfigEntryState.SETUP_RETRY), + (axislib.RequestError, ConfigEntryState.SETUP_RETRY), + # Device yield unknown error + (axislib.AxisException, ConfigEntryState.SETUP_ERROR), + ], +) +@pytest.mark.usefixtures("mock_default_requests") +async def test_get_axis_api_errors( + hass: HomeAssistant, + config_entry: MockConfigEntry, + side_effect: Exception, + state: ConfigEntryState, ) -> None: - """Device unauthorized yields authentication required error.""" - with ( - patch( - "axis.interfaces.vapix.Vapix.initialize", side_effect=axislib.Unauthorized - ), - pytest.raises(axis.errors.AuthenticationRequired), - ): - await axis.hub.get_axis_api(hass, config_entry_data) - - -async def test_get_device_device_unavailable( - hass: HomeAssistant, config_entry_data: MappingProxyType[str, Any] -) -> None: - """Device unavailable yields cannot connect error.""" - with ( - patch("axis.interfaces.vapix.Vapix.request", side_effect=axislib.RequestError), - pytest.raises(axis.errors.CannotConnect), - ): - await axis.hub.get_axis_api(hass, config_entry_data) - - -async def test_get_device_unknown_error( - hass: HomeAssistant, config_entry_data: MappingProxyType[str, Any] -) -> None: - """Device yield unknown error.""" - with ( - patch("axis.interfaces.vapix.Vapix.request", side_effect=axislib.AxisException), - pytest.raises(axis.errors.AuthenticationRequired), - ): - await axis.hub.get_axis_api(hass, config_entry_data) + """Failed setup schedules a retry of setup.""" + config_entry.add_to_hass(hass) + with patch("axis.interfaces.vapix.Vapix.initialize", side_effect=side_effect): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state == state