Fix ness_alarm tasks being fired before required sensors and panel are loaded asynchronously (#94590)

Co-authored-by: Erik Montnemery <erik@montnemery.com>
pull/94752/head^2
hcross13 2023-06-28 15:30:48 +08:00 committed by GitHub
parent 7a82176670
commit 5955be46a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 4 deletions

View File

@ -1,6 +1,7 @@
"""Support for Ness D8X/D16X devices.""" """Support for Ness D8X/D16X devices."""
from collections import namedtuple from collections import namedtuple
import datetime import datetime
import logging
from nessclient import ArmingState, Client from nessclient import ArmingState, Client
import voluptuous as vol import voluptuous as vol
@ -21,8 +22,11 @@ from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.start import async_at_started
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__)
DOMAIN = "ness_alarm" DOMAIN = "ness_alarm"
DATA_NESS = "ness_alarm" DATA_NESS = "ness_alarm"
@ -109,6 +113,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close)
async def _started(event):
# Force update for current arming status and current zone states (once Home Assistant has finished loading required sensors and panel)
_LOGGER.debug("invoking client keepalive() & update()")
hass.loop.create_task(client.keepalive())
hass.loop.create_task(client.update())
async_at_started(hass, _started)
hass.async_create_task( hass.async_create_task(
async_load_platform( async_load_platform(
hass, Platform.BINARY_SENSOR, DOMAIN, {CONF_ZONES: zones}, config hass, Platform.BINARY_SENSOR, DOMAIN, {CONF_ZONES: zones}, config
@ -131,10 +143,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
client.on_zone_change(on_zone_change) client.on_zone_change(on_zone_change)
client.on_state_change(on_state_change) client.on_state_change(on_state_change)
# Force update for current arming status and current zone states
hass.loop.create_task(client.keepalive())
hass.loop.create_task(client.update())
async def handle_panic(call: ServiceCall) -> None: async def handle_panic(call: ServiceCall) -> None:
await client.panic(call.data[ATTR_CODE]) await client.panic(call.data[ATTR_CODE])