Skip to content

Commit

Permalink
Migrate integrations n-r to generic flowhandler (home-assistant#111864)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Feb 29, 2024
1 parent 52e7912 commit e0c1feb
Show file tree
Hide file tree
Showing 113 changed files with 890 additions and 746 deletions.
21 changes: 11 additions & 10 deletions homeassistant/components/nam/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
)
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac

Expand Down Expand Up @@ -70,20 +69,20 @@ async def async_check_credentials(
await nam.async_check_credentials()


class NAMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
"""Config flow for Nettigo Air Monitor."""

VERSION = 1

def __init__(self) -> None:
"""Initialize flow."""
self.host: str
self.entry: config_entries.ConfigEntry
self.entry: ConfigEntry
self._config: NamConfig

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
errors: dict[str, str] = {}

Expand Down Expand Up @@ -119,7 +118,7 @@ async def async_step_user(

async def async_step_credentials(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle the credentials step."""
errors: dict[str, str] = {}

Expand All @@ -145,7 +144,7 @@ async def async_step_credentials(

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle zeroconf discovery."""
self.host = discovery_info.host
self.context["title_placeholders"] = {"host": self.host}
Expand All @@ -167,7 +166,7 @@ async def async_step_zeroconf(

async def async_step_confirm_discovery(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle discovery confirm."""
errors: dict[str, str] = {}

Expand All @@ -188,7 +187,9 @@ async def async_step_confirm_discovery(
errors=errors,
)

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle configuration by re-auth."""
if entry := self.hass.config_entries.async_get_entry(self.context["entry_id"]):
self.entry = entry
Expand All @@ -198,7 +199,7 @@ async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Dialog that informs the user that reauth is required."""
errors: dict[str, str] = {}

Expand Down
31 changes: 17 additions & 14 deletions homeassistant/components/nanoleaf/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from aionanoleaf import InvalidToken, Nanoleaf, Unauthorized, Unavailable
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import ssdp, zeroconf
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_TOKEN
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.json import save_json
from homeassistant.util.json import JsonObjectType, JsonValueType, load_json_object
Expand All @@ -31,10 +30,10 @@
)


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class NanoleafConfigFlow(ConfigFlow, domain=DOMAIN):
"""Nanoleaf config flow."""

reauth_entry: config_entries.ConfigEntry | None = None
reauth_entry: ConfigEntry | None = None

nanoleaf: Nanoleaf

Expand All @@ -46,7 +45,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle Nanoleaf flow initiated by the user."""
if user_input is None:
return self.async_show_form(
Expand Down Expand Up @@ -77,10 +76,12 @@ async def async_step_user(
)
return await self.async_step_link()

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle Nanoleaf reauth flow if token is invalid."""
self.reauth_entry = cast(
config_entries.ConfigEntry,
ConfigEntry,
self.hass.config_entries.async_get_entry(self.context["entry_id"]),
)
self.nanoleaf = Nanoleaf(
Expand All @@ -91,29 +92,31 @@ async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle Nanoleaf Zeroconf discovery."""
_LOGGER.debug("Zeroconf discovered: %s", discovery_info)
return await self._async_homekit_zeroconf_discovery_handler(discovery_info)

async def async_step_homekit(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle Nanoleaf Homekit discovery."""
_LOGGER.debug("Homekit discovered: %s", discovery_info)
return await self._async_homekit_zeroconf_discovery_handler(discovery_info)

async def _async_homekit_zeroconf_discovery_handler(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle Nanoleaf Homekit and Zeroconf discovery."""
return await self._async_discovery_handler(
discovery_info.host,
discovery_info.name.replace(f".{discovery_info.type}", ""),
discovery_info.properties[zeroconf.ATTR_PROPERTIES_ID],
)

async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle Nanoleaf SSDP discovery."""
_LOGGER.debug("SSDP discovered: %s", discovery_info)
return await self._async_discovery_handler(
Expand All @@ -124,7 +127,7 @@ async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowRes

async def _async_discovery_handler(
self, host: str, name: str, device_id: str
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle Nanoleaf discovery."""
# The name is unique and printed on the device and cannot be changed.
await self.async_set_unique_id(name)
Expand Down Expand Up @@ -156,7 +159,7 @@ async def _async_discovery_handler(

async def async_step_link(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle Nanoleaf link step."""
if user_input is None:
return self.async_show_form(step_id="link")
Expand Down Expand Up @@ -188,7 +191,7 @@ async def async_step_link(

async def async_setup_finish(
self, discovery_integration_import: bool = False
) -> FlowResult:
) -> ConfigFlowResult:
"""Finish Nanoleaf config flow."""
try:
await self.nanoleaf.get_info()
Expand Down
13 changes: 7 additions & 6 deletions homeassistant/components/neato/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import logging
from typing import Any

from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.data_entry_flow import FlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.helpers import config_entry_oauth2_flow

from .const import NEATO_DOMAIN
Expand All @@ -26,7 +25,7 @@ def logger(self) -> logging.Logger:

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Create an entry for the flow."""
current_entries = self._async_current_entries()
if self.source != SOURCE_REAUTH and current_entries:
Expand All @@ -35,19 +34,21 @@ async def async_step_user(

return await super().async_step_user(user_input=user_input)

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon migration of old entries."""
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Confirm reauth upon migration of old entries."""
if user_input is None:
return self.async_show_form(step_id="reauth_confirm")
return await self.async_step_user()

async def async_oauth_create_entry(self, data: dict[str, Any]) -> FlowResult:
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
"""Create an entry for the flow. Update an entry if one already exist."""
current_entries = self._async_current_entries()
if self.source == SOURCE_REAUTH and current_entries:
Expand Down
25 changes: 14 additions & 11 deletions homeassistant/components/nest/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from google_nest_sdm.structure import InfoTrait, Structure
import voluptuous as vol

from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
from homeassistant.data_entry_flow import FlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry, ConfigFlowResult
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.util import get_random_string

Expand Down Expand Up @@ -133,7 +132,7 @@ async def async_generate_authorize_url(self) -> str:
authorize_url = OAUTH2_AUTHORIZE.format(project_id=project_id)
return f"{authorize_url}{query}"

async def async_oauth_create_entry(self, data: dict[str, Any]) -> FlowResult:
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
"""Complete OAuth setup and finish pubsub or finish."""
_LOGGER.debug("Finishing post-oauth configuration")
self._data.update(data)
Expand All @@ -142,23 +141,25 @@ async def async_oauth_create_entry(self, data: dict[str, Any]) -> FlowResult:
return await self.async_step_finish()
return await self.async_step_pubsub()

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error."""
self._data.update(entry_data)

return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Confirm reauth dialog."""
if user_input is None:
return self.async_show_form(step_id="reauth_confirm")
return await self.async_step_user()

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
self._data[DATA_SDM] = {}
if self.source == SOURCE_REAUTH:
Expand All @@ -169,7 +170,7 @@ async def async_step_user(

async def async_step_create_cloud_project(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle initial step in app credentails flow."""
implementations = await config_entry_oauth2_flow.async_get_implementations(
self.hass, self.DOMAIN
Expand All @@ -196,7 +197,7 @@ async def async_step_create_cloud_project(

async def async_step_cloud_project(
self, user_input: dict | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle cloud project in user input."""
if user_input is not None:
self._data.update(user_input)
Expand All @@ -216,7 +217,7 @@ async def async_step_cloud_project(

async def async_step_device_project(
self, user_input: dict | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Collect device access project from user input."""
errors = {}
if user_input is not None:
Expand Down Expand Up @@ -249,7 +250,7 @@ async def async_step_device_project(

async def async_step_pubsub(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Configure and create Pub/Sub subscriber."""
data = {
**self._data,
Expand Down Expand Up @@ -313,7 +314,9 @@ async def async_step_pubsub(
errors=errors,
)

async def async_step_finish(self, data: dict[str, Any] | None = None) -> FlowResult:
async def async_step_finish(
self, data: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Create an entry for the SDM flow."""
_LOGGER.debug("Creating/updating configuration entry")
# Update existing config entry when in the reauth flow.
Expand Down
Loading

0 comments on commit e0c1feb

Please sign in to comment.