Axis - Streamline setup and teardown of integration (#35675)

* Streamline setup and teardown of integration

* Dont remove integration twice
This commit is contained in:
Robert Svensson 2020-05-15 23:56:09 +02:00 committed by GitHub
parent 890013cecf
commit 714047f789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 51 deletions

View File

@ -32,6 +32,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
if not device.option_camera:
return
config = {
CONF_NAME: config_entry.data[CONF_NAME],
CONF_USERNAME: config_entry.data[CONF_USERNAME],

View File

@ -1,6 +1,10 @@
"""Constants for the Axis component."""
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__)
DOMAIN = "axis"
@ -13,3 +17,5 @@ CONF_MODEL = "model"
DEFAULT_EVENTS = True
DEFAULT_TRIGGER_TIME = 0
PLATFORMS = [BINARY_SENSOR_DOMAIN, CAMERA_DOMAIN, SWITCH_DOMAIN]

View File

@ -7,9 +7,6 @@ import axis
from axis.event_stream import OPERATION_INITIALIZED
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 (
CONF_HOST,
CONF_NAME,
@ -32,6 +29,7 @@ from .const import (
DEFAULT_TRIGGER_TIME,
DOMAIN as AXIS_DOMAIN,
LOGGER,
PLATFORMS,
)
from .errors import AuthenticationRequired, CannotConnect
@ -165,38 +163,28 @@ class AxisNetworkDevice:
self.fw_version = self.api.vapix.params.firmware_version
self.product_type = self.api.vapix.params.prodtype
if self.option_camera:
self.hass.async_create_task(
self.hass.config_entries.async_forward_entry_setup(
self.config_entry, CAMERA_DOMAIN
)
async def start_platforms():
await asyncio.gather(
*[
self.hass.config_entries.async_forward_entry_setup(
self.config_entry, platform
)
for platform in PLATFORMS
]
)
if self.option_events:
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
if self.option_events:
self.api.stream.connection_status_callback = (
self.async_connection_status_callback
)
for platform in [BINARY_SENSOR_DOMAIN, SWITCH_DOMAIN]
]
self.hass.async_create_task(self.start(platform_tasks))
self.api.enable_events(event_callback=self.async_event_callback)
self.api.start()
self.hass.async_create_task(start_platforms())
self.config_entry.add_update_listener(self.async_new_address_callback)
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
def shutdown(self, event):
"""Stop the event stream."""
@ -204,29 +192,23 @@ class AxisNetworkDevice:
async def async_reset(self):
"""Reset this device to default state."""
platform_tasks = []
self.api.stop()
if self.config_entry.options[CONF_CAMERA]:
platform_tasks.append(
self.hass.config_entries.async_forward_entry_unload(
self.config_entry, CAMERA_DOMAIN
)
unload_ok = all(
await asyncio.gather(
*[
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]:
self.api.stop()
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 = []
for unsubscribe_listener in self.listeners:
unsubscribe_listener()
return True

View File

@ -110,7 +110,7 @@ async def setup_axis_integration(
await hass.config_entries.async_setup(config_entry.entry_id)
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):
@ -124,8 +124,8 @@ async def test_device_setup(hass):
entry = device.config_entry
assert len(forward_entry_setup.mock_calls) == 3
assert forward_entry_setup.mock_calls[0][1] == (entry, "camera")
assert forward_entry_setup.mock_calls[1][1] == (entry, "binary_sensor")
assert forward_entry_setup.mock_calls[0][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 device.host == ENTRY_CONFIG[CONF_HOST]