Esempio n. 1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Balboa Spa from a config entry."""
    host = entry.data[CONF_HOST]

    _LOGGER.debug("Attempting to connect to %s", host)
    spa = BalboaSpaWifi(host)
    connected = await spa.connect()
    if not connected:
        _LOGGER.error("Failed to connect to spa at %s", host)
        raise ConfigEntryNotReady

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = spa

    async def _async_balboa_update_cb() -> None:
        """Primary update callback called from pybalboa."""
        _LOGGER.debug("Primary update callback triggered")
        async_dispatcher_send(hass, SIGNAL_UPDATE.format(entry.entry_id))

    # set the callback so we know we have new data
    spa.new_data_cb = _async_balboa_update_cb

    _LOGGER.debug("Starting listener and monitor tasks")
    monitoring_tasks = [asyncio.create_task(spa.listen())]
    await spa.spa_configured()
    monitoring_tasks.append(asyncio.create_task(spa.check_connection_status()))

    def stop_monitoring() -> None:
        """Stop monitoring the spa connection."""
        _LOGGER.debug("Canceling listener and monitor tasks")
        for task in monitoring_tasks:
            task.cancel()

    entry.async_on_unload(stop_monitoring)

    # At this point we have a configured spa.
    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

    async def keep_alive(now: datetime) -> None:
        """Keep alive task."""
        _LOGGER.debug("Keep alive")
        await spa.send_mod_ident_req()

    entry.async_on_unload(
        async_track_time_interval(hass, keep_alive, KEEP_ALIVE_INTERVAL)
    )

    # call update_listener on startup and for options change as well.
    await async_setup_time_sync(hass, entry)
    entry.async_on_unload(entry.add_update_listener(update_listener))

    return True
Esempio n. 2
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up Balboa Spa from a config entry."""
    host = entry.data[CONF_HOST]

    unsub = entry.add_update_listener(update_listener)

    _LOGGER.info("Attempting to connect to %s", host)
    spa = BalboaSpaWifi(host)
    hass.data[DOMAIN][entry.entry_id] = {SPA: spa, UNSUB: unsub}

    connected = await spa.connect()
    if not connected:
        _LOGGER.error("Failed to connect to spa at %s", host)
        raise ConfigEntryNotReady

    # send config requests, and then listen until we are configured.
    await spa.send_mod_ident_req()
    await spa.send_panel_req(0, 1)
    # configured = await spa.listen_until_configured()

    _LOGGER.info("Starting listener and monitor tasks.")
    hass.loop.create_task(spa.listen())
    await spa.spa_configured()
    hass.loop.create_task(spa.check_connection_status())

    # At this point we have a configured spa.
    forward_setup = hass.config_entries.async_forward_entry_setup
    for component in PLATFORMS:
        hass.async_create_task(forward_setup(entry, component))

    async def _async_balboa_update_cb():
        """Primary update callback called from pybalboa."""
        _LOGGER.debug("Primary update callback triggered")
        async_dispatcher_send(hass, DOMAIN)

    spa.new_data_cb = _async_balboa_update_cb

    # call update_listener on startup
    await update_listener(hass, entry)

    return True
Esempio n. 3
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Balboa Spa from a config entry."""
    host = entry.data[CONF_HOST]

    _LOGGER.debug("Attempting to connect to %s", host)
    spa = BalboaSpaWifi(host)

    connected = await spa.connect()
    if not connected:
        _LOGGER.error("Failed to connect to spa at %s", host)
        raise ConfigEntryNotReady

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = spa

    # send config requests, and then listen until we are configured.
    await spa.send_mod_ident_req()
    await spa.send_panel_req(0, 1)

    async def _async_balboa_update_cb():
        """Primary update callback called from pybalboa."""
        _LOGGER.debug("Primary update callback triggered")
        async_dispatcher_send(hass, SIGNAL_UPDATE.format(entry.entry_id))

    # set the callback so we know we have new data
    spa.new_data_cb = _async_balboa_update_cb

    _LOGGER.debug("Starting listener and monitor tasks")
    asyncio.create_task(spa.listen())
    await spa.spa_configured()
    asyncio.create_task(spa.check_connection_status())

    # At this point we have a configured spa.
    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    # call update_listener on startup and for options change as well.
    await async_setup_time_sync(hass, entry)
    entry.async_on_unload(entry.add_update_listener(update_listener))

    return True