Skip to content

Commit

Permalink
Migrate integrations s-t to generic flowhandler (#111865)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Feb 29, 2024
1 parent e0c1feb commit b0ed8c4
Show file tree
Hide file tree
Showing 106 changed files with 792 additions and 661 deletions.
7 changes: 3 additions & 4 deletions homeassistant/components/sabnzbd/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import voluptuous as vol

from homeassistant import config_entries
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import (
CONF_API_KEY,
CONF_HOST,
Expand All @@ -15,7 +15,6 @@
CONF_SSL,
CONF_URL,
)
from homeassistant.data_entry_flow import FlowResult

from .const import DEFAULT_NAME, DOMAIN
from .sab import get_client
Expand All @@ -31,7 +30,7 @@
)


class SABnzbdConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class SABnzbdConfigFlow(ConfigFlow, domain=DOMAIN):
"""Sabnzbd config flow."""

VERSION = 1
Expand All @@ -47,7 +46,7 @@ async def _async_validate_input(self, user_input):

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

errors = {}
Expand Down
51 changes: 31 additions & 20 deletions homeassistant/components/samsungtv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
from samsungtvws.encrypted.authenticator import SamsungTVEncryptedWSAsyncAuthenticator
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import dhcp, ssdp, zeroconf
from homeassistant.config_entries import (
ConfigEntry,
ConfigEntryState,
ConfigFlow,
ConfigFlowResult,
)
from homeassistant.const import (
CONF_HOST,
CONF_MAC,
Expand All @@ -23,7 +28,7 @@
CONF_TOKEN,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac

Expand Down Expand Up @@ -57,7 +62,7 @@ def _strip_uuid(udn: str) -> str:


def _entry_is_complete(
entry: config_entries.ConfigEntry,
entry: ConfigEntry,
ssdp_rendering_control_location: str | None,
ssdp_main_tv_agent_location: str | None,
) -> bool:
Expand Down Expand Up @@ -91,14 +96,14 @@ def _mac_is_same_with_incorrect_formatting(
)


class SamsungTVConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class SamsungTVConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a Samsung TV config flow."""

VERSION = 2

def __init__(self) -> None:
"""Initialize flow."""
self._reauth_entry: config_entries.ConfigEntry | None = None
self._reauth_entry: ConfigEntry | None = None
self._host: str = ""
self._mac: str | None = None
self._udn: str | None = None
Expand Down Expand Up @@ -131,7 +136,7 @@ def _base_config_entry(self) -> dict[str, Any]:
CONF_SSDP_MAIN_TV_AGENT_LOCATION: self._ssdp_main_tv_agent_location,
}

def _get_entry_from_bridge(self) -> FlowResult:
def _get_entry_from_bridge(self) -> ConfigFlowResult:
"""Get device entry."""
assert self._bridge
data = self._base_config_entry()
Expand Down Expand Up @@ -252,7 +257,7 @@ async def _async_set_name_host_from_input(self, user_input: dict[str, Any]) -> N

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
if user_input is not None:
await self._async_set_name_host_from_input(user_input)
Expand All @@ -270,7 +275,7 @@ async def async_step_user(

async def async_step_pairing(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a pairing by accepting the message on the TV."""
assert self._bridge is not None
errors: dict[str, str] = {}
Expand All @@ -292,7 +297,7 @@ async def async_step_pairing(

async def async_step_encrypted_pairing(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a encrypted pairing."""
assert self._host is not None
await self._async_start_encrypted_pairing(self._host)
Expand Down Expand Up @@ -326,9 +331,9 @@ async def async_step_encrypted_pairing(
@callback
def _async_get_existing_matching_entry(
self,
) -> tuple[config_entries.ConfigEntry | None, bool]:
) -> tuple[ConfigEntry | None, bool]:
"""Get first existing matching entry (prefer unique id)."""
matching_host_entry: config_entries.ConfigEntry | None = None
matching_host_entry: ConfigEntry | None = None
for entry in self._async_current_entries(include_ignore=False):
if (self._mac and self._mac == entry.data.get(CONF_MAC)) or (
self._upnp_udn and self._upnp_udn == entry.unique_id
Expand All @@ -345,7 +350,7 @@ def _async_get_existing_matching_entry(
@callback
def _async_update_existing_matching_entry(
self,
) -> config_entries.ConfigEntry | None:
) -> ConfigEntry | None:
"""Check existing entries and update them.
Returns the existing entry if it was updated.
Expand Down Expand Up @@ -398,7 +403,7 @@ def _async_update_existing_matching_entry(
return None
LOGGER.debug("Updating existing config entry with %s", entry_kw_args)
self.hass.config_entries.async_update_entry(entry, **entry_kw_args)
if entry.state != config_entries.ConfigEntryState.LOADED:
if entry.state != ConfigEntryState.LOADED:
# If its loaded it already has a reload listener in place
# and we do not want to trigger multiple reloads
self.hass.async_create_task(
Expand Down Expand Up @@ -430,7 +435,9 @@ def _abort_if_manufacturer_is_not_samsung(self) -> None:
):
raise AbortFlow(RESULT_NOT_SUPPORTED)

async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle a flow initialized by ssdp discovery."""
LOGGER.debug("Samsung device found via SSDP: %s", discovery_info)
model_name: str = discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NAME) or ""
Expand Down Expand Up @@ -475,7 +482,9 @@ async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowRes
self.context["title_placeholders"] = {"device": self._title}
return await self.async_step_confirm()

async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle a flow initialized by dhcp discovery."""
LOGGER.debug("Samsung device found via DHCP: %s", discovery_info)
self._mac = format_mac(discovery_info.macaddress)
Expand All @@ -487,7 +496,7 @@ async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowRes

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a flow initialized by zeroconf discovery."""
LOGGER.debug("Samsung device found via ZEROCONF: %s", discovery_info)
self._mac = format_mac(discovery_info.properties["deviceid"])
Expand All @@ -499,7 +508,7 @@ async def async_step_zeroconf(

async def async_step_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle user-confirmation of discovered node."""
if user_input is not None:
await self._async_create_bridge()
Expand All @@ -512,7 +521,9 @@ async def async_step_confirm(
step_id="confirm", description_placeholders={"device": self._title}
)

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."""
self._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
Expand All @@ -525,7 +536,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:
"""Confirm reauth."""
errors = {}
assert self._reauth_entry
Expand Down Expand Up @@ -569,7 +580,7 @@ async def _async_start_encrypted_pairing(self, host: str) -> None:

async def async_step_reauth_confirm_encrypted(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Confirm reauth (encrypted method)."""
errors = {}
assert self._reauth_entry
Expand Down
18 changes: 9 additions & 9 deletions homeassistant/components/schlage/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
from pyschlage.exceptions import NotAuthorizedError
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.data_entry_flow import FlowResult

from .const import DOMAIN, LOGGER

Expand All @@ -21,7 +19,7 @@
STEP_REAUTH_DATA_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class SchlageConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Schlage."""

VERSION = 1
Expand All @@ -30,7 +28,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle the initial step."""
if user_input is None:
return self._show_user_form({})
Expand All @@ -45,13 +43,15 @@ async def async_step_user(
await self.async_set_unique_id(user_id)
return self.async_create_entry(title=username, data=user_input)

def _show_user_form(self, errors: dict[str, str]) -> FlowResult:
def _show_user_form(self, errors: dict[str, str]) -> ConfigFlowResult:
"""Show the user form."""
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, 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 reauth upon an API authentication error."""
self.reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
Expand All @@ -60,7 +60,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."""
assert self.reauth_entry is not None
if user_input is None:
Expand All @@ -85,7 +85,7 @@ async def async_step_reauth_confirm(
await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")

def _show_reauth_form(self, errors: dict[str, str]) -> FlowResult:
def _show_reauth_form(self, errors: dict[str, str]) -> ConfigFlowResult:
"""Show the reauth form."""
return self.async_show_form(
step_id="reauth_confirm",
Expand Down
28 changes: 17 additions & 11 deletions homeassistant/components/screenlogic/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
from screenlogicpy.requests import login
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import dhcp
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT, CONF_SCAN_INTERVAL
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import format_mac

Expand Down Expand Up @@ -60,7 +64,7 @@ def name_for_mac(mac):
return f"Pentair: {short_mac(mac)}"


class ScreenlogicConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class ScreenlogicConfigFlow(ConfigFlow, domain=DOMAIN):
"""Config flow to setup screen logic devices."""

VERSION = 1
Expand All @@ -73,17 +77,19 @@ def __init__(self) -> None:
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
config_entry: ConfigEntry,
) -> ScreenLogicOptionsFlowHandler:
"""Get the options flow for ScreenLogic."""
return ScreenLogicOptionsFlowHandler(config_entry)

async def async_step_user(self, user_input=None) -> FlowResult:
async def async_step_user(self, user_input=None) -> ConfigFlowResult:
"""Handle the start of the config flow."""
self.discovered_gateways = await async_discover_gateways_by_unique_id(self.hass)
return await self.async_step_gateway_select()

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."""
mac = format_mac(discovery_info.macaddress)
await self.async_set_unique_id(mac)
Expand All @@ -94,7 +100,7 @@ async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowRes
self.context["title_placeholders"] = {"name": discovery_info.hostname}
return await self.async_step_gateway_entry()

async def async_step_gateway_select(self, user_input=None) -> FlowResult:
async def async_step_gateway_select(self, user_input=None) -> ConfigFlowResult:
"""Handle the selection of a discovered ScreenLogic gateway."""
existing = self._async_current_ids()
unconfigured_gateways = {
Expand Down Expand Up @@ -141,7 +147,7 @@ async def async_step_gateway_select(self, user_input=None) -> FlowResult:
description_placeholders={},
)

async def async_step_gateway_entry(self, user_input=None) -> FlowResult:
async def async_step_gateway_entry(self, user_input=None) -> ConfigFlowResult:
"""Handle the manual entry of a ScreenLogic gateway."""
errors: dict[str, str] = {}
ip_address = self.discovered_ip
Expand Down Expand Up @@ -180,14 +186,14 @@ async def async_step_gateway_entry(self, user_input=None) -> FlowResult:
)


class ScreenLogicOptionsFlowHandler(config_entries.OptionsFlow):
class ScreenLogicOptionsFlowHandler(OptionsFlow):
"""Handles the options for the ScreenLogic integration."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
def __init__(self, config_entry: ConfigEntry) -> None:
"""Init the screen logic options flow."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None) -> FlowResult:
async def async_step_init(self, user_input=None) -> ConfigFlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/components/season/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import voluptuous as vol

from homeassistant.config_entries import ConfigFlow
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_TYPE
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.selector import (
SelectSelector,
SelectSelectorConfig,
Expand All @@ -24,7 +23,7 @@ class SeasonConfigFlow(ConfigFlow, domain=DOMAIN):

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
if user_input is not None:
await self.async_set_unique_id(user_input[CONF_TYPE])
Expand Down
Loading

0 comments on commit b0ed8c4

Please sign in to comment.