Clean up setup and teardown of Axis integration (#112120)

This commit is contained in:
Robert Svensson 2024-03-03 18:42:44 +01:00 committed by GitHub
parent 74f1420410
commit 4eb24b2db7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 18 additions and 39 deletions

View File

@ -28,9 +28,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
hass.data[AXIS_DOMAIN][config_entry.entry_id] = hub
await hub.async_update_device_registry()
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
hub.async_setup_events()
hub.setup()
config_entry.add_update_listener(hub.async_new_address_callback)
config_entry.async_on_unload(hub.teardown)
config_entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hub.shutdown)
)
@ -40,8 +41,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload Axis device config entry."""
hub: AxisHub = hass.data[AXIS_DOMAIN].pop(config_entry.entry_id)
return await hub.async_reset()
hass.data[AXIS_DOMAIN].pop(config_entry.entry_id)
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:

View File

@ -9,7 +9,6 @@ DOMAIN = "axis"
ATTR_MANUFACTURER = "Axis Communications AB"
CONF_EVENTS = "events"
CONF_STREAM_PROFILE = "stream_profile"
CONF_VIDEO_SOURCE = "video_source"

View File

@ -31,15 +31,12 @@ from homeassistant.setup import async_when_setup
from ..const import (
ATTR_MANUFACTURER,
CONF_EVENTS,
CONF_STREAM_PROFILE,
CONF_VIDEO_SOURCE,
DEFAULT_EVENTS,
DEFAULT_STREAM_PROFILE,
DEFAULT_TRIGGER_TIME,
DEFAULT_VIDEO_SOURCE,
DOMAIN as AXIS_DOMAIN,
PLATFORMS,
)
@ -110,11 +107,6 @@ class AxisHub:
# Options
@property
def option_events(self) -> bool:
"""Config entry option defining if platforms based on events should be created."""
return self.config_entry.options.get(CONF_EVENTS, DEFAULT_EVENTS)
@property
def option_stream_profile(self) -> str:
"""Config entry option defining what stream profile camera platform should use."""
@ -147,7 +139,7 @@ class AxisHub:
# Callbacks
@callback
def async_connection_status_callback(self, status: Signal) -> None:
def connection_status_callback(self, status: Signal) -> None:
"""Handle signals of device connection status.
This is called on every RTSP keep-alive message.
@ -210,11 +202,11 @@ class AxisHub:
# Setup and teardown methods
def async_setup_events(self) -> None:
@callback
def setup(self) -> None:
"""Set up the device events."""
if self.option_events:
self.api.stream.connection_status_callback.append(
self.async_connection_status_callback
self.connection_status_callback
)
self.api.enable_events()
self.api.stream.start()
@ -233,10 +225,7 @@ class AxisHub:
"""Stop the event stream."""
self.disconnect_from_stream()
async def async_reset(self) -> bool:
@callback
def teardown(self) -> None:
"""Reset this device to default state."""
self.disconnect_from_stream()
return await self.hass.config_entries.async_unload_platforms(
self.config_entry, PLATFORMS
)

View File

@ -9,7 +9,7 @@ from axis.rtsp import Signal, State
import pytest
import respx
from homeassistant.components.axis.const import CONF_EVENTS, DOMAIN as AXIS_DOMAIN
from homeassistant.components.axis.const import DOMAIN as AXIS_DOMAIN
from homeassistant.const import (
CONF_HOST,
CONF_MODEL,
@ -93,7 +93,7 @@ def config_fixture():
@pytest.fixture(name="options")
def options_fixture(request):
"""Define a config entry options fixture."""
return {CONF_EVENTS: True}
return {}
# Axis API fixtures

View File

@ -41,7 +41,6 @@
'entry_id': '676abe5b73621446e6550a2e86ffe3dd',
'minor_version': 1,
'options': dict({
'events': True,
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,

View File

@ -7,7 +7,6 @@ import pytest
from homeassistant.components import dhcp, ssdp, zeroconf
from homeassistant.components.axis import config_flow
from homeassistant.components.axis.const import (
CONF_EVENTS,
CONF_STREAM_PROFILE,
CONF_VIDEO_SOURCE,
DEFAULT_STREAM_PROFILE,
@ -607,7 +606,6 @@ async def test_option_flow(hass: HomeAssistant, setup_config_entry) -> None:
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_EVENTS: True,
CONF_STREAM_PROFILE: "profile_1",
CONF_VIDEO_SOURCE: 1,
}

View File

@ -171,13 +171,6 @@ async def test_device_unavailable(
assert hass.states.get(f"{BINARY_SENSOR_DOMAIN}.{NAME}_sound_1").state == STATE_OFF
async def test_device_reset(hass: HomeAssistant, setup_config_entry) -> None:
"""Successfully reset device."""
device = hass.data[AXIS_DOMAIN][setup_config_entry.entry_id]
result = await device.async_reset()
assert result is True
async def test_device_not_accessible(
hass: HomeAssistant, config_entry, setup_default_vapix_requests
) -> None: