Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate integrations u-z to generic flowhandler #111866

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/ukraine_alarm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from uasiren.client import Client
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_NAME, CONF_REGION
from homeassistant.helpers.aiohttp_client import async_get_clientsession

Expand All @@ -16,7 +16,7 @@
_LOGGER = logging.getLogger(__name__)


class UkraineAlarmConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class UkraineAlarmConfigFlow(ConfigFlow, domain=DOMAIN):
"""Config flow for Ukraine Alarm."""

VERSION = 1
Expand Down
44 changes: 26 additions & 18 deletions homeassistant/components/unifi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
from aiounifi.interfaces.sites import Sites
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import ssdp
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
Expand All @@ -27,7 +32,6 @@
CONF_VERIFY_SSL,
)
from homeassistant.core import HomeAssistant, 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 @@ -61,7 +65,7 @@
}


class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
class UnifiFlowHandler(ConfigFlow, domain=UNIFI_DOMAIN):
"""Handle a UniFi Network config flow."""

VERSION = 1
Expand All @@ -71,20 +75,20 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
config_entry: ConfigEntry,
) -> UnifiOptionsFlowHandler:
"""Get the options flow for this handler."""
return UnifiOptionsFlowHandler(config_entry)

def __init__(self) -> None:
"""Initialize the UniFi Network flow."""
self.config: dict[str, Any] = {}
self.reauth_config_entry: config_entries.ConfigEntry | None = None
self.reauth_config_entry: ConfigEntry | None = None
self.reauth_schema: dict[vol.Marker, Any] = {}

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 Expand Up @@ -144,7 +148,7 @@ async def async_step_user(

async def async_step_site(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Select site to control."""
if user_input is not None:
unique_id = user_input[CONF_SITE_ID]
Expand Down Expand Up @@ -181,7 +185,9 @@ async def async_step_site(
data_schema=vol.Schema({vol.Required(CONF_SITE_ID): vol.In(site_names)}),
)

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Trigger a reauthentication flow."""
config_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
Expand All @@ -206,7 +212,9 @@ async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:

return await self.async_step_user()

async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle a discovered UniFi device."""
parsed_url = urlparse(discovery_info.ssdp_location)
model_description = discovery_info.upnp[ssdp.ATTR_UPNP_MODEL_DESCRIPTION]
Expand Down Expand Up @@ -235,19 +243,19 @@ async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowRes
return await self.async_step_user()


class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
class UnifiOptionsFlowHandler(OptionsFlow):
"""Handle Unifi Network options."""

hub: UnifiHub

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize UniFi Network options flow."""
self.config_entry = config_entry
self.options = dict(config_entry.options)

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Manage the UniFi Network options."""
if self.config_entry.entry_id not in self.hass.data[UNIFI_DOMAIN]:
return self.async_abort(reason="integration_not_setup")
Expand All @@ -261,7 +269,7 @@ async def async_step_init(

async def async_step_simple_options(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""For users without advanced settings enabled."""
if user_input is not None:
self.options.update(user_input)
Expand Down Expand Up @@ -296,7 +304,7 @@ async def async_step_simple_options(

async def async_step_configure_entity_sources(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Select sources for entities."""
if user_input is not None:
self.options.update(user_input)
Expand Down Expand Up @@ -329,7 +337,7 @@ async def async_step_configure_entity_sources(

async def async_step_device_tracker(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Manage the device tracker options."""
if user_input is not None:
self.options.update(user_input)
Expand Down Expand Up @@ -390,7 +398,7 @@ async def async_step_device_tracker(

async def async_step_client_control(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Manage configuration of network access controlled clients."""
if user_input is not None:
self.options.update(user_input)
Expand Down Expand Up @@ -429,7 +437,7 @@ async def async_step_client_control(

async def async_step_statistics_sensors(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Manage the statistics sensors options."""
if user_input is not None:
self.options.update(user_input)
Expand All @@ -452,7 +460,7 @@ async def async_step_statistics_sensors(
last_step=True,
)

async def _update_options(self) -> FlowResult:
async def _update_options(self) -> ConfigFlowResult:
"""Update config entry options."""
return self.async_create_entry(title="", data=self.options)

Expand Down
56 changes: 34 additions & 22 deletions homeassistant/components/unifiprotect/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
from unifi_discovery import async_console_is_alive
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import dhcp, ssdp
from homeassistant.config_entries import (
SOURCE_IGNORE,
ConfigEntry,
ConfigEntryState,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import (
CONF_HOST,
CONF_ID,
Expand All @@ -24,7 +31,6 @@
CONF_VERIFY_SSL,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import (
async_create_clientsession,
async_get_clientsession,
Expand Down Expand Up @@ -54,8 +60,8 @@
_LOGGER = logging.getLogger(__name__)

ENTRY_FAILURE_STATES = (
config_entries.ConfigEntryState.SETUP_ERROR,
config_entries.ConfigEntryState.SETUP_RETRY,
ConfigEntryState.SETUP_ERROR,
ConfigEntryState.SETUP_RETRY,
)


Expand All @@ -72,7 +78,7 @@ def _host_is_direct_connect(host: str) -> bool:

async def _async_console_is_offline(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,
entry: ConfigEntry,
) -> bool:
"""Check if a console is offline.

Expand All @@ -89,28 +95,32 @@ async def _async_console_is_offline(
)


class ProtectFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class ProtectFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle a UniFi Protect config flow."""

VERSION = 2

def __init__(self) -> None:
"""Init the config flow."""
super().__init__()
self.entry: config_entries.ConfigEntry | None = None
self.entry: ConfigEntry | None = None
self._discovered_device: dict[str, str] = {}

async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle discovery via dhcp."""
_LOGGER.debug("Starting discovery via: %s", discovery_info)
return await self._async_discovery_handoff()

async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle a discovered UniFi device."""
_LOGGER.debug("Starting discovery via: %s", discovery_info)
return await self._async_discovery_handoff()

async def _async_discovery_handoff(self) -> FlowResult:
async def _async_discovery_handoff(self) -> ConfigFlowResult:
"""Ensure discovery is active."""
# Discovery requires an additional check so we use
# SSDP and DHCP to tell us to start it so it only
Expand All @@ -120,15 +130,15 @@ async def _async_discovery_handoff(self) -> FlowResult:

async def async_step_integration_discovery(
self, discovery_info: DiscoveryInfoType
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle integration discovery."""
self._discovered_device = discovery_info
mac = _async_unifi_mac_from_hass(discovery_info["hw_addr"])
await self.async_set_unique_id(mac)
source_ip = discovery_info["source_ip"]
direct_connect_domain = discovery_info["direct_connect_domain"]
for entry in self._async_current_entries():
if entry.source == config_entries.SOURCE_IGNORE:
if entry.source == SOURCE_IGNORE:
if entry.unique_id == mac:
return self.async_abort(reason="already_configured")
continue
Expand Down Expand Up @@ -164,7 +174,7 @@ async def async_step_integration_discovery(

async def async_step_discovery_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Confirm discovery."""
errors: dict[str, str] = {}
discovery_info = self._discovered_device
Expand Down Expand Up @@ -212,13 +222,13 @@ async def async_step_discovery_confirm(
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
config_entry: ConfigEntry,
) -> OptionsFlow:
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)

@callback
def _async_create_entry(self, title: str, data: dict[str, Any]) -> FlowResult:
def _async_create_entry(self, title: str, data: dict[str, Any]) -> ConfigFlowResult:
return self.async_create_entry(
title=title,
data={**data, CONF_ID: title},
Expand Down Expand Up @@ -279,15 +289,17 @@ async def _async_get_nvr_data(

return nvr_data, 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:
"""Perform reauth upon an API authentication error."""

self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
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."""
errors: dict[str, str] = {}
assert self.entry is not None
Expand Down Expand Up @@ -321,7 +333,7 @@ async def async_step_reauth_confirm(

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

errors: dict[str, str] = {}
Expand Down Expand Up @@ -362,16 +374,16 @@ async def async_step_user(
)


class OptionsFlowHandler(config_entries.OptionsFlow):
class OptionsFlowHandler(OptionsFlow):
"""Handle options."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
Expand Down
9 changes: 5 additions & 4 deletions homeassistant/components/upb/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import upb_lib
import voluptuous as vol

from homeassistant import config_entries, exceptions
from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_ADDRESS, CONF_FILE_PATH, CONF_HOST, CONF_PROTOCOL
from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN

Expand Down Expand Up @@ -70,7 +71,7 @@ def _make_url_from_data(data):
return f"{protocol}{address}"


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class UPBConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for UPB PIM."""

VERSION = 1
Expand Down Expand Up @@ -128,9 +129,9 @@ def _url_already_configured(self, url):
return urlparse(url).hostname in existing_hosts


class CannotConnect(exceptions.HomeAssistantError):
class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""


class InvalidUpbFile(exceptions.HomeAssistantError):
class InvalidUpbFile(HomeAssistantError):
"""Error to indicate there is invalid or missing UPB config file."""
Loading
Loading