Ejemplo 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
Ejemplo n.º 2
0
async def validate_input(data: dict[str, Any]) -> dict[str, str]:
    """Validate the user input allows us to connect."""
    _LOGGER.debug("Attempting to connect to %s", data[CONF_HOST])
    spa = BalboaSpaWifi(data[CONF_HOST])
    connected = await spa.connect()
    _LOGGER.debug("Got connected = %d", connected)
    if not connected:
        raise CannotConnect

    task = asyncio.create_task(spa.listen())
    await spa.spa_configured()

    mac_addr = format_mac(spa.get_macaddr())
    model = spa.get_model_name()
    task.cancel()
    await spa.disconnect()

    return {"title": model, "formatted_mac": mac_addr}
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect."""

    _LOGGER.debug("Attempting to connect to %s", data[CONF_HOST])
    spa = BalboaSpaWifi(data[CONF_HOST])
    connected = await spa.connect()
    _LOGGER.debug("Got connected = %d", connected)
    if not connected:
        raise CannotConnect

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

    asyncio.create_task(spa.listen())

    await spa.spa_configured()

    mac_addr = format_mac(spa.get_macaddr())
    model = spa.get_model_name()
    await spa.disconnect()

    return {"title": model, "formatted_mac": mac_addr}
Ejemplo n.º 6
0
async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect."""
    for entry in hass.config_entries.async_entries(DOMAIN):
        if entry.data[CONF_HOST] == data[CONF_HOST]:
            raise AlreadyConfigured

    _LOGGER.debug("Attempting to connect to %s", data[CONF_HOST])
    spa = BalboaSpaWifi(data[CONF_HOST])
    connected = await spa.connect()
    _LOGGER.debug("Got connected = %d", connected)
    if not connected:
        raise CannotConnect
    await spa.disconnect()

    return {"title": data[CONF_NAME]}