Create tasks to load Amcrest platforms (#64848)
* Create tasks to load Amcrest platforms * Move closure definition out of for loop
This commit is contained in:
parent
6d5c2bb34b
commit
6af7425051
1 changed files with 41 additions and 22 deletions
|
@ -30,10 +30,11 @@ from homeassistant.const import (
|
|||
CONF_USERNAME,
|
||||
ENTITY_MATCH_ALL,
|
||||
ENTITY_MATCH_NONE,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
HTTP_BASIC_AUTHENTICATION,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.exceptions import Unauthorized, UnknownUser
|
||||
from homeassistant.helpers import discovery
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
@ -267,6 +268,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
"""Set up the Amcrest IP Camera component."""
|
||||
hass.data.setdefault(DATA_AMCREST, {DEVICES: {}, CAMERAS: []})
|
||||
|
||||
monitor_tasks = []
|
||||
for device in config[DOMAIN]:
|
||||
name: str = device[CONF_NAME]
|
||||
username: str = device[CONF_USERNAME]
|
||||
|
@ -302,18 +304,22 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
control_light,
|
||||
)
|
||||
|
||||
await discovery.async_load_platform(
|
||||
hass, Platform.CAMERA, DOMAIN, {CONF_NAME: name}, config
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(
|
||||
hass, Platform.CAMERA, DOMAIN, {CONF_NAME: name}, config
|
||||
)
|
||||
)
|
||||
|
||||
event_codes = set()
|
||||
if binary_sensors:
|
||||
await discovery.async_load_platform(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
DOMAIN,
|
||||
{CONF_NAME: name, CONF_BINARY_SENSORS: binary_sensors},
|
||||
config,
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(
|
||||
hass,
|
||||
Platform.BINARY_SENSOR,
|
||||
DOMAIN,
|
||||
{CONF_NAME: name, CONF_BINARY_SENSORS: binary_sensors},
|
||||
config,
|
||||
)
|
||||
)
|
||||
event_codes = {
|
||||
sensor.event_code
|
||||
|
@ -323,26 +329,39 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
and sensor.event_code is not None
|
||||
}
|
||||
|
||||
asyncio.create_task(_monitor_events(hass, name, api, event_codes))
|
||||
monitor_tasks.append(
|
||||
asyncio.create_task(_monitor_events(hass, name, api, event_codes))
|
||||
)
|
||||
|
||||
if sensors:
|
||||
await discovery.async_load_platform(
|
||||
hass,
|
||||
Platform.SENSOR,
|
||||
DOMAIN,
|
||||
{CONF_NAME: name, CONF_SENSORS: sensors},
|
||||
config,
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(
|
||||
hass,
|
||||
Platform.SENSOR,
|
||||
DOMAIN,
|
||||
{CONF_NAME: name, CONF_SENSORS: sensors},
|
||||
config,
|
||||
)
|
||||
)
|
||||
|
||||
if switches:
|
||||
await discovery.async_load_platform(
|
||||
hass,
|
||||
Platform.SWITCH,
|
||||
DOMAIN,
|
||||
{CONF_NAME: name, CONF_SWITCHES: switches},
|
||||
config,
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(
|
||||
hass,
|
||||
Platform.SWITCH,
|
||||
DOMAIN,
|
||||
{CONF_NAME: name, CONF_SWITCHES: switches},
|
||||
config,
|
||||
)
|
||||
)
|
||||
|
||||
@callback
|
||||
def cancel_monitors(event: Event) -> None:
|
||||
for monitor_task in monitor_tasks:
|
||||
monitor_task.cancel()
|
||||
|
||||
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, cancel_monitors)
|
||||
|
||||
if not hass.data[DATA_AMCREST][DEVICES]:
|
||||
return False
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue