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 authored and GitHub committed 2020-05-15 23:56:09 +02:00
1 parent 890013cecf
commit 714047f789
4 files changed
+42 -51

No files matched your search

+30 -48
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