mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Clean up setup and teardown of Axis integration (#112120)
This commit is contained in:
parent
74f1420410
commit
4eb24b2db7
@ -28,9 +28,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
hass.data[AXIS_DOMAIN][config_entry.entry_id] = hub
|
hass.data[AXIS_DOMAIN][config_entry.entry_id] = hub
|
||||||
await hub.async_update_device_registry()
|
await hub.async_update_device_registry()
|
||||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
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.add_update_listener(hub.async_new_address_callback)
|
||||||
|
config_entry.async_on_unload(hub.teardown)
|
||||||
config_entry.async_on_unload(
|
config_entry.async_on_unload(
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hub.shutdown)
|
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:
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||||
"""Unload Axis device config entry."""
|
"""Unload Axis device config entry."""
|
||||||
hub: AxisHub = hass.data[AXIS_DOMAIN].pop(config_entry.entry_id)
|
hass.data[AXIS_DOMAIN].pop(config_entry.entry_id)
|
||||||
return await hub.async_reset()
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||||
|
|
||||||
|
|
||||||
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||||
|
@ -9,7 +9,6 @@ DOMAIN = "axis"
|
|||||||
|
|
||||||
ATTR_MANUFACTURER = "Axis Communications AB"
|
ATTR_MANUFACTURER = "Axis Communications AB"
|
||||||
|
|
||||||
CONF_EVENTS = "events"
|
|
||||||
CONF_STREAM_PROFILE = "stream_profile"
|
CONF_STREAM_PROFILE = "stream_profile"
|
||||||
CONF_VIDEO_SOURCE = "video_source"
|
CONF_VIDEO_SOURCE = "video_source"
|
||||||
|
|
||||||
|
@ -31,15 +31,12 @@ from homeassistant.setup import async_when_setup
|
|||||||
|
|
||||||
from ..const import (
|
from ..const import (
|
||||||
ATTR_MANUFACTURER,
|
ATTR_MANUFACTURER,
|
||||||
CONF_EVENTS,
|
|
||||||
CONF_STREAM_PROFILE,
|
CONF_STREAM_PROFILE,
|
||||||
CONF_VIDEO_SOURCE,
|
CONF_VIDEO_SOURCE,
|
||||||
DEFAULT_EVENTS,
|
|
||||||
DEFAULT_STREAM_PROFILE,
|
DEFAULT_STREAM_PROFILE,
|
||||||
DEFAULT_TRIGGER_TIME,
|
DEFAULT_TRIGGER_TIME,
|
||||||
DEFAULT_VIDEO_SOURCE,
|
DEFAULT_VIDEO_SOURCE,
|
||||||
DOMAIN as AXIS_DOMAIN,
|
DOMAIN as AXIS_DOMAIN,
|
||||||
PLATFORMS,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -110,11 +107,6 @@ class AxisHub:
|
|||||||
|
|
||||||
# Options
|
# 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
|
@property
|
||||||
def option_stream_profile(self) -> str:
|
def option_stream_profile(self) -> str:
|
||||||
"""Config entry option defining what stream profile camera platform should use."""
|
"""Config entry option defining what stream profile camera platform should use."""
|
||||||
@ -147,7 +139,7 @@ class AxisHub:
|
|||||||
# Callbacks
|
# Callbacks
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_connection_status_callback(self, status: Signal) -> None:
|
def connection_status_callback(self, status: Signal) -> None:
|
||||||
"""Handle signals of device connection status.
|
"""Handle signals of device connection status.
|
||||||
|
|
||||||
This is called on every RTSP keep-alive message.
|
This is called on every RTSP keep-alive message.
|
||||||
@ -210,11 +202,11 @@ class AxisHub:
|
|||||||
|
|
||||||
# Setup and teardown methods
|
# Setup and teardown methods
|
||||||
|
|
||||||
def async_setup_events(self) -> None:
|
@callback
|
||||||
|
def setup(self) -> None:
|
||||||
"""Set up the device events."""
|
"""Set up the device events."""
|
||||||
if self.option_events:
|
|
||||||
self.api.stream.connection_status_callback.append(
|
self.api.stream.connection_status_callback.append(
|
||||||
self.async_connection_status_callback
|
self.connection_status_callback
|
||||||
)
|
)
|
||||||
self.api.enable_events()
|
self.api.enable_events()
|
||||||
self.api.stream.start()
|
self.api.stream.start()
|
||||||
@ -233,10 +225,7 @@ class AxisHub:
|
|||||||
"""Stop the event stream."""
|
"""Stop the event stream."""
|
||||||
self.disconnect_from_stream()
|
self.disconnect_from_stream()
|
||||||
|
|
||||||
async def async_reset(self) -> bool:
|
@callback
|
||||||
|
def teardown(self) -> None:
|
||||||
"""Reset this device to default state."""
|
"""Reset this device to default state."""
|
||||||
self.disconnect_from_stream()
|
self.disconnect_from_stream()
|
||||||
|
|
||||||
return await self.hass.config_entries.async_unload_platforms(
|
|
||||||
self.config_entry, PLATFORMS
|
|
||||||
)
|
|
||||||
|
@ -9,7 +9,7 @@ from axis.rtsp import Signal, State
|
|||||||
import pytest
|
import pytest
|
||||||
import respx
|
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 (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_MODEL,
|
CONF_MODEL,
|
||||||
@ -93,7 +93,7 @@ def config_fixture():
|
|||||||
@pytest.fixture(name="options")
|
@pytest.fixture(name="options")
|
||||||
def options_fixture(request):
|
def options_fixture(request):
|
||||||
"""Define a config entry options fixture."""
|
"""Define a config entry options fixture."""
|
||||||
return {CONF_EVENTS: True}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
# Axis API fixtures
|
# Axis API fixtures
|
||||||
|
@ -41,7 +41,6 @@
|
|||||||
'entry_id': '676abe5b73621446e6550a2e86ffe3dd',
|
'entry_id': '676abe5b73621446e6550a2e86ffe3dd',
|
||||||
'minor_version': 1,
|
'minor_version': 1,
|
||||||
'options': dict({
|
'options': dict({
|
||||||
'events': True,
|
|
||||||
}),
|
}),
|
||||||
'pref_disable_new_entities': False,
|
'pref_disable_new_entities': False,
|
||||||
'pref_disable_polling': False,
|
'pref_disable_polling': False,
|
||||||
|
@ -7,7 +7,6 @@ import pytest
|
|||||||
from homeassistant.components import dhcp, ssdp, zeroconf
|
from homeassistant.components import dhcp, ssdp, zeroconf
|
||||||
from homeassistant.components.axis import config_flow
|
from homeassistant.components.axis import config_flow
|
||||||
from homeassistant.components.axis.const import (
|
from homeassistant.components.axis.const import (
|
||||||
CONF_EVENTS,
|
|
||||||
CONF_STREAM_PROFILE,
|
CONF_STREAM_PROFILE,
|
||||||
CONF_VIDEO_SOURCE,
|
CONF_VIDEO_SOURCE,
|
||||||
DEFAULT_STREAM_PROFILE,
|
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["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
CONF_EVENTS: True,
|
|
||||||
CONF_STREAM_PROFILE: "profile_1",
|
CONF_STREAM_PROFILE: "profile_1",
|
||||||
CONF_VIDEO_SOURCE: 1,
|
CONF_VIDEO_SOURCE: 1,
|
||||||
}
|
}
|
||||||
|
@ -171,13 +171,6 @@ async def test_device_unavailable(
|
|||||||
assert hass.states.get(f"{BINARY_SENSOR_DOMAIN}.{NAME}_sound_1").state == STATE_OFF
|
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(
|
async def test_device_not_accessible(
|
||||||
hass: HomeAssistant, config_entry, setup_default_vapix_requests
|
hass: HomeAssistant, config_entry, setup_default_vapix_requests
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user