Axis - Streamline setup and teardown of integration (#35675)
* Streamline setup and teardown of integration * Dont remove integration twice
This commit is contained in:
parent
890013cecf
commit
714047f789
4 changed files with 42 additions and 51 deletions
|
@ -32,6 +32,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
|
|
||||||
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
|
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
|
||||||
|
|
||||||
|
if not device.option_camera:
|
||||||
|
return
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
CONF_NAME: config_entry.data[CONF_NAME],
|
CONF_NAME: config_entry.data[CONF_NAME],
|
||||||
CONF_USERNAME: config_entry.data[CONF_USERNAME],
|
CONF_USERNAME: config_entry.data[CONF_USERNAME],
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
"""Constants for the Axis component."""
|
"""Constants for the Axis component."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||||
|
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
|
||||||
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
DOMAIN = "axis"
|
DOMAIN = "axis"
|
||||||
|
@ -13,3 +17,5 @@ CONF_MODEL = "model"
|
||||||
|
|
||||||
DEFAULT_EVENTS = True
|
DEFAULT_EVENTS = True
|
||||||
DEFAULT_TRIGGER_TIME = 0
|
DEFAULT_TRIGGER_TIME = 0
|
||||||
|
|
||||||
|
PLATFORMS = [BINARY_SENSOR_DOMAIN, CAMERA_DOMAIN, SWITCH_DOMAIN]
|
||||||
|
|
|
@ -7,9 +7,6 @@ import axis
|
||||||
from axis.event_stream import OPERATION_INITIALIZED
|
from axis.event_stream import OPERATION_INITIALIZED
|
||||||
from axis.streammanager import SIGNAL_PLAYING
|
from axis.streammanager import SIGNAL_PLAYING
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
|
||||||
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
|
|
||||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
|
@ -32,6 +29,7 @@ from .const import (
|
||||||
DEFAULT_TRIGGER_TIME,
|
DEFAULT_TRIGGER_TIME,
|
||||||
DOMAIN as AXIS_DOMAIN,
|
DOMAIN as AXIS_DOMAIN,
|
||||||
LOGGER,
|
LOGGER,
|
||||||
|
PLATFORMS,
|
||||||
)
|
)
|
||||||
from .errors import AuthenticationRequired, CannotConnect
|
from .errors import AuthenticationRequired, CannotConnect
|
||||||
|
|
||||||
|
@ -165,38 +163,28 @@ class AxisNetworkDevice:
|
||||||
self.fw_version = self.api.vapix.params.firmware_version
|
self.fw_version = self.api.vapix.params.firmware_version
|
||||||
self.product_type = self.api.vapix.params.prodtype
|
self.product_type = self.api.vapix.params.prodtype
|
||||||
|
|
||||||
if self.option_camera:
|
async def start_platforms():
|
||||||
|
await asyncio.gather(
|
||||||
self.hass.async_create_task(
|
*[
|
||||||
self.hass.config_entries.async_forward_entry_setup(
|
self.hass.config_entries.async_forward_entry_setup(
|
||||||
self.config_entry, CAMERA_DOMAIN
|
self.config_entry, platform
|
||||||
)
|
)
|
||||||
|
for platform in PLATFORMS
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
if self.option_events:
|
||||||
if self.option_events:
|
self.api.stream.connection_status_callback = (
|
||||||
|
self.async_connection_status_callback
|
||||||
self.api.stream.connection_status_callback = (
|
|
||||||
self.async_connection_status_callback
|
|
||||||
)
|
|
||||||
self.api.enable_events(event_callback=self.async_event_callback)
|
|
||||||
|
|
||||||
platform_tasks = [
|
|
||||||
self.hass.config_entries.async_forward_entry_setup(
|
|
||||||
self.config_entry, platform
|
|
||||||
)
|
)
|
||||||
for platform in [BINARY_SENSOR_DOMAIN, SWITCH_DOMAIN]
|
self.api.enable_events(event_callback=self.async_event_callback)
|
||||||
]
|
self.api.start()
|
||||||
self.hass.async_create_task(self.start(platform_tasks))
|
|
||||||
|
self.hass.async_create_task(start_platforms())
|
||||||
|
|
||||||
self.config_entry.add_update_listener(self.async_new_address_callback)
|
self.config_entry.add_update_listener(self.async_new_address_callback)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def start(self, platform_tasks):
|
|
||||||
"""Start the event stream when all platforms are loaded."""
|
|
||||||
await asyncio.gather(*platform_tasks)
|
|
||||||
self.api.start()
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def shutdown(self, event):
|
def shutdown(self, event):
|
||||||
"""Stop the event stream."""
|
"""Stop the event stream."""
|
||||||
|
@ -204,29 +192,23 @@ class AxisNetworkDevice:
|
||||||
|
|
||||||
async def async_reset(self):
|
async def async_reset(self):
|
||||||
"""Reset this device to default state."""
|
"""Reset this device to default state."""
|
||||||
platform_tasks = []
|
self.api.stop()
|
||||||
|
|
||||||
if self.config_entry.options[CONF_CAMERA]:
|
unload_ok = all(
|
||||||
platform_tasks.append(
|
await asyncio.gather(
|
||||||
self.hass.config_entries.async_forward_entry_unload(
|
*[
|
||||||
self.config_entry, CAMERA_DOMAIN
|
self.hass.config_entries.async_forward_entry_unload(
|
||||||
)
|
self.config_entry, platform
|
||||||
|
)
|
||||||
|
for platform in PLATFORMS
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
if not unload_ok:
|
||||||
|
return False
|
||||||
|
|
||||||
if self.config_entry.options[CONF_EVENTS]:
|
for unsubscribe_listener in self.listeners:
|
||||||
self.api.stop()
|
unsubscribe_listener()
|
||||||
platform_tasks += [
|
|
||||||
self.hass.config_entries.async_forward_entry_unload(
|
|
||||||
self.config_entry, platform
|
|
||||||
)
|
|
||||||
for platform in [BINARY_SENSOR_DOMAIN, SWITCH_DOMAIN]
|
|
||||||
]
|
|
||||||
|
|
||||||
await asyncio.gather(*platform_tasks)
|
|
||||||
|
|
||||||
for unsub_dispatcher in self.listeners:
|
|
||||||
unsub_dispatcher()
|
|
||||||
self.listeners = []
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ async def setup_axis_integration(
|
||||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
return hass.data[AXIS_DOMAIN].get(config[CONF_MAC])
|
return hass.data[AXIS_DOMAIN].get(config_entry.unique_id)
|
||||||
|
|
||||||
|
|
||||||
async def test_device_setup(hass):
|
async def test_device_setup(hass):
|
||||||
|
@ -124,8 +124,8 @@ async def test_device_setup(hass):
|
||||||
entry = device.config_entry
|
entry = device.config_entry
|
||||||
|
|
||||||
assert len(forward_entry_setup.mock_calls) == 3
|
assert len(forward_entry_setup.mock_calls) == 3
|
||||||
assert forward_entry_setup.mock_calls[0][1] == (entry, "camera")
|
assert forward_entry_setup.mock_calls[0][1] == (entry, "binary_sensor")
|
||||||
assert forward_entry_setup.mock_calls[1][1] == (entry, "binary_sensor")
|
assert forward_entry_setup.mock_calls[1][1] == (entry, "camera")
|
||||||
assert forward_entry_setup.mock_calls[2][1] == (entry, "switch")
|
assert forward_entry_setup.mock_calls[2][1] == (entry, "switch")
|
||||||
|
|
||||||
assert device.host == ENTRY_CONFIG[CONF_HOST]
|
assert device.host == ENTRY_CONFIG[CONF_HOST]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue