Better handling of balboa spa connection (#71909)

* Better handling of balboa spa connection

* Send a single message for keep alive task rather than multiple
This commit is contained in:
Nathan Spencer 2022-05-25 00:51:58 -06:00 committed by GitHub
parent c1ddde3764
commit a98af2ad58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 27 deletions

View file

@ -1,12 +1,16 @@
"""Config flow for Balboa Spa Client integration."""
from __future__ import annotations
import asyncio
from typing import Any
from pybalboa import BalboaSpaWifi
import voluptuous as vol
from homeassistant import config_entries, core, exceptions
from homeassistant import config_entries, exceptions
from homeassistant.const import CONF_HOST
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.device_registry import format_mac
from .const import _LOGGER, CONF_SYNC_TIME, DOMAIN
@ -14,9 +18,8 @@ from .const import _LOGGER, CONF_SYNC_TIME, DOMAIN
DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
async def validate_input(hass: core.HomeAssistant, data):
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()
@ -24,16 +27,12 @@ async def validate_input(hass: core.HomeAssistant, data):
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())
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}
@ -46,17 +45,21 @@ class BalboaSpaClientFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
@staticmethod
@callback
def async_get_options_flow(config_entry):
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Get the options flow for this handler."""
return BalboaSpaClientOptionsFlowHandler(config_entry)
async def async_step_user(self, user_input=None):
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initialized by the user."""
errors = {}
if user_input is not None:
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
try:
info = await validate_input(self.hass, user_input)
info = await validate_input(user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
@ -79,11 +82,13 @@ class CannotConnect(exceptions.HomeAssistantError):
class BalboaSpaClientOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle Balboa Spa Client options."""
def __init__(self, config_entry):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize Balboa Spa Client options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage Balboa Spa Client options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)