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]
This commit is contained in:
Robert Svensson 2024-07-26 12:29:47 +02:00 committed by GitHub
parent 33ea67e1d0
commit 72fdcd1cb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 78 deletions

View file

@ -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:

View file

@ -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."""

View file

@ -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