From 4eb24b2db720149bd713e74f2eda0b21f12b4086 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sun, 3 Mar 2024 18:42:44 +0100 Subject: [PATCH] Clean up setup and teardown of Axis integration (#112120) --- homeassistant/components/axis/__init__.py | 7 ++-- homeassistant/components/axis/const.py | 1 - homeassistant/components/axis/hub/hub.py | 35 +++++++------------ tests/components/axis/conftest.py | 4 +-- .../axis/snapshots/test_diagnostics.ambr | 1 - tests/components/axis/test_config_flow.py | 2 -- tests/components/axis/test_device.py | 7 ---- 7 files changed, 18 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/axis/__init__.py b/homeassistant/components/axis/__init__.py index 9fda3aad84d..d37b2ebe5ac 100644 --- a/homeassistant/components/axis/__init__.py +++ b/homeassistant/components/axis/__init__.py @@ -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: diff --git a/homeassistant/components/axis/const.py b/homeassistant/components/axis/const.py index a13cae0dd0a..f4cc35dd7d2 100644 --- a/homeassistant/components/axis/const.py +++ b/homeassistant/components/axis/const.py @@ -9,7 +9,6 @@ DOMAIN = "axis" ATTR_MANUFACTURER = "Axis Communications AB" -CONF_EVENTS = "events" CONF_STREAM_PROFILE = "stream_profile" CONF_VIDEO_SOURCE = "video_source" diff --git a/homeassistant/components/axis/hub/hub.py b/homeassistant/components/axis/hub/hub.py index b81d3498255..5a0f45a1682 100644 --- a/homeassistant/components/axis/hub/hub.py +++ b/homeassistant/components/axis/hub/hub.py @@ -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,17 +202,17 @@ 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.api.enable_events() - self.api.stream.start() + self.api.stream.connection_status_callback.append( + self.connection_status_callback + ) + self.api.enable_events() + self.api.stream.start() - if self.api.vapix.mqtt.supported: - async_when_setup(self.hass, MQTT_DOMAIN, self.async_use_mqtt) + if self.api.vapix.mqtt.supported: + async_when_setup(self.hass, MQTT_DOMAIN, self.async_use_mqtt) @callback def disconnect_from_stream(self) -> None: @@ -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 - ) diff --git a/tests/components/axis/conftest.py b/tests/components/axis/conftest.py index 0e83f9007bf..a15abb224a4 100644 --- a/tests/components/axis/conftest.py +++ b/tests/components/axis/conftest.py @@ -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 diff --git a/tests/components/axis/snapshots/test_diagnostics.ambr b/tests/components/axis/snapshots/test_diagnostics.ambr index b5647a08543..3902cee6a0b 100644 --- a/tests/components/axis/snapshots/test_diagnostics.ambr +++ b/tests/components/axis/snapshots/test_diagnostics.ambr @@ -41,7 +41,6 @@ 'entry_id': '676abe5b73621446e6550a2e86ffe3dd', 'minor_version': 1, 'options': dict({ - 'events': True, }), 'pref_disable_new_entities': False, 'pref_disable_polling': False, diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index e570c1ecee8..41af5a58876 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -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, } diff --git a/tests/components/axis/test_device.py b/tests/components/axis/test_device.py index 9912b30f9c7..b41155e88b1 100644 --- a/tests/components/axis/test_device.py +++ b/tests/components/axis/test_device.py @@ -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: