Пример #1
0
async def _gw_start(opp: OpenPeerPower, entry: ConfigEntry,
                    gateway: BaseAsyncGateway):
    """Start the gateway."""
    gateway_ready = asyncio.Event()

    def gateway_connected(_: BaseAsyncGateway):
        gateway_ready.set()

    gateway.on_conn_made = gateway_connected
    # Don't use opp.async_create_task to avoid holding up setup indefinitely.
    opp.data[DOMAIN][MYSENSORS_GATEWAY_START_TASK.format(
        entry.entry_id)] = asyncio.create_task(gateway.start(
        ))  # store the connect task so it can be cancelled in gw_stop

    async def stop_this_gw(_: Event):
        await gw_stop(opp, entry, gateway)

    await on_unload(
        opp,
        entry.entry_id,
        opp.bus.async_listen_once(EVENT_OPENPEERPOWER_STOP, stop_this_gw),
    )

    if entry.data[CONF_DEVICE] == MQTT_COMPONENT:
        # Gatways connected via mqtt doesn't send gateway ready message.
        return
    try:
        with async_timeout.timeout(GATEWAY_READY_TIMEOUT):
            await gateway_ready.wait()
    except asyncio.TimeoutError:
        _LOGGER.warning(
            "Gateway %s not connected after %s secs so continuing with setup",
            entry.data[CONF_DEVICE],
            GATEWAY_READY_TIMEOUT,
        )
Пример #2
0
async def _gw_start(hass: HomeAssistant, entry: ConfigEntry,
                    gateway: BaseAsyncGateway) -> None:
    """Start the gateway."""
    gateway_ready = asyncio.Event()

    def gateway_connected(_: BaseAsyncGateway) -> None:
        """Handle gateway connected."""
        gateway_ready.set()

    gateway.on_conn_made = gateway_connected
    # Don't use hass.async_create_task to avoid holding up setup indefinitely.
    hass.data[DOMAIN][MYSENSORS_GATEWAY_START_TASK.format(
        entry.entry_id)] = asyncio.create_task(gateway.start(
        ))  # store the connect task so it can be cancelled in gw_stop

    async def stop_this_gw(_: Event) -> None:
        """Stop the gateway."""
        await gw_stop(hass, entry, gateway)

    on_unload(
        hass,
        entry.entry_id,
        hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_this_gw),
    )

    if entry.data[CONF_DEVICE] == MQTT_COMPONENT:
        # Gatways connected via mqtt doesn't send gateway ready message.
        return
    try:
        async with async_timeout.timeout(GATEWAY_READY_TIMEOUT):
            await gateway_ready.wait()
    except asyncio.TimeoutError:
        _LOGGER.warning(
            "Gateway %s not connected after %s secs so continuing with setup",
            entry.data[CONF_DEVICE],
            GATEWAY_READY_TIMEOUT,
        )