1
0
mirror of https://github.com/natekspencer/hacs-oasis_mini.git synced 2025-11-18 01:53:41 -05:00

Add ability to reconfigure integration to handle updated host ip

This commit is contained in:
Nathan Spencer
2024-07-11 11:51:23 -06:00
parent 9b85d939c4
commit cda435070d
4 changed files with 52 additions and 20 deletions

View File

@@ -10,10 +10,9 @@ from aiohttp import ClientConnectorError
from httpx import ConnectError, HTTPStatusError
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_EMAIL, CONF_HOST, CONF_PASSWORD
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.schema_config_entry_flow import (
SchemaCommonFlowHandler,
SchemaFlowError,
@@ -30,16 +29,14 @@ _LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
OPTIONS_SCHEMA = vol.Schema(
{
vol.Optional(CONF_EMAIL): str,
vol.Optional(CONF_PASSWORD): str,
}
{vol.Required(CONF_EMAIL): str, vol.Required(CONF_PASSWORD): str}
)
async def cloud_login(
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
) -> dict[str, Any]:
"""Cloud login."""
coordinator: OasisMiniCoordinator = handler.parent_handler.hass.data[DOMAIN][
handler.parent_handler.config_entry.entry_id
]
@@ -49,8 +46,8 @@ async def cloud_login(
email=user_input[CONF_EMAIL], password=user_input[CONF_PASSWORD]
)
user_input[CONF_ACCESS_TOKEN] = coordinator.device.access_token
except:
raise SchemaFlowError("invalid_auth")
except Exception as ex:
raise SchemaFlowError("invalid_auth") from ex
del user_input[CONF_PASSWORD]
return user_input
@@ -75,7 +72,7 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
"""Get the options flow for this handler."""
return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
# async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
# async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> ConfigFlowResult:
# """Handle dhcp discovery."""
# self.host = discovery_info.ip
# self.name = discovery_info.hostname
@@ -85,13 +82,29 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle the initial step."""
return await self._async_step("user", STEP_USER_DATA_SCHEMA, user_input)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration."""
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
assert entry
suggested_values = user_input or entry.data
return await self._async_step(
"reconfigure", STEP_USER_DATA_SCHEMA, user_input, suggested_values
)
async def _async_step(
self, step_id: str, schema: vol.Schema, user_input: dict[str, Any] | None = None
) -> FlowResult:
self,
step_id: str,
schema: vol.Schema,
user_input: dict[str, Any] | None = None,
suggested_values: dict[str, Any] | None = None,
) -> ConfigFlowResult:
"""Handle step setup."""
if abort := self._abort_if_configured(user_input):
return abort
@@ -108,21 +121,26 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
existing_entry, data=data
)
await self.hass.config_entries.async_reload(existing_entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_abort(reason="reconfigure_successful")
return self.async_create_entry(
title=f"Oasis Mini {self.serial_number}",
data=data,
)
return self.async_show_form(step_id=step_id, data_schema=schema, errors=errors)
return self.async_show_form(
step_id=step_id,
data_schema=self.add_suggested_values_to_schema(schema, suggested_values),
errors=errors,
)
async def validate_client(self, user_input: dict[str, Any]) -> dict[str, str]:
"""Validate client setup."""
errors = {}
try:
client = create_client({"host": self.host} | user_input)
self.serial_number = await client.async_get_serial_number()
async with asyncio.timeout(10):
client = create_client({"host": self.host} | user_input)
self.serial_number = await client.async_get_serial_number()
if not self.serial_number:
errors["base"] = "invalid_host"
except asyncio.TimeoutError:
@@ -143,7 +161,7 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
@callback
def _abort_if_configured(
self, user_input: dict[str, Any] | None
) -> FlowResult | None:
) -> ConfigFlowResult | None:
"""Abort if configured."""
if self.host or user_input:
data = {CONF_HOST: self.host, **(user_input or {})}