Refactor Tesla wall connector config flow (#60755)

This commit is contained in:
einarhauks 2021-12-01 15:07:24 +00:00 committed by GitHub
parent 76d906d17e
commit e7f00c2c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 77 deletions

View File

@ -10,17 +10,12 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.dhcp import IP_ADDRESS
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant, callback
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import (
DEFAULT_SCAN_INTERVAL,
DOMAIN,
WALLCONNECTOR_DEVICE_NAME,
WALLCONNECTOR_SERIAL_NUMBER,
)
from .const import DOMAIN, WALLCONNECTOR_DEVICE_NAME, WALLCONNECTOR_SERIAL_NUMBER
_LOGGER = logging.getLogger(__name__)
@ -112,49 +107,15 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown"
if not errors:
existing_entry = await self.async_set_unique_id(
info[WALLCONNECTOR_SERIAL_NUMBER]
await self.async_set_unique_id(
unique_id=info[WALLCONNECTOR_SERIAL_NUMBER], raise_on_progress=True
)
self._abort_if_unique_id_configured(
updates=user_input, reload_on_update=True
)
if existing_entry:
self.hass.config_entries.async_update_entry(
existing_entry, data=user_input
)
await self.hass.config_entries.async_reload(existing_entry.entry_id)
return self.async_abort(reason="already_configured")
return self.async_create_entry(title=info["title"], data=user_input)
return self.async_show_form(
step_id="user", data_schema=data_schema, errors=errors
)
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)
class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle a option flow for Tesla Wall Connector."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
"""Handle options flow."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
data_schema = vol.Schema(
{
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
): vol.All(vol.Coerce(int), vol.Clamp(min=1))
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)

View File

@ -6,7 +6,7 @@ from tesla_wall_connector.exceptions import WallConnectorConnectionError
from homeassistant import config_entries, setup
from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS
from homeassistant.components.tesla_wall_connector.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
@ -176,32 +176,3 @@ async def test_dhcp_error_from_wall_connector(mock_wall_connector_version, hass)
assert result["type"] == "abort"
assert result["reason"] == "cannot_connect"
async def test_option_flow(hass):
"""Test option flow."""
entry = MockConfigEntry(
domain=DOMAIN, unique_id="abc123", data={CONF_HOST: "1.2.3.4"}
)
entry.add_to_hass(hass)
assert not entry.options
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(
entry.entry_id,
data=None,
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={CONF_SCAN_INTERVAL: 30},
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {CONF_SCAN_INTERVAL: 30}