2021-05-03 10:52:22 +00:00
|
|
|
"""Config flow for AVM FRITZ!SmartHome."""
|
2021-05-15 05:54:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
2020-04-20 13:00:07 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from pyfritzhome import Fritzhome, LoginError
|
2020-04-29 05:17:35 +00:00
|
|
|
from requests.exceptions import HTTPError
|
2020-04-20 13:00:07 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-11-29 16:10:07 +00:00
|
|
|
from homeassistant.components import ssdp
|
2021-05-15 05:54:11 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
2020-04-20 13:00:07 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
2021-05-15 05:54:11 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
from .const import DEFAULT_HOST, DEFAULT_USERNAME, DOMAIN
|
|
|
|
|
|
|
|
DATA_SCHEMA_USER = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST, default=DEFAULT_HOST): str,
|
|
|
|
vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
DATA_SCHEMA_CONFIRM = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-10-08 16:12:04 +00:00
|
|
|
RESULT_INVALID_AUTH = "invalid_auth"
|
|
|
|
RESULT_NO_DEVICES_FOUND = "no_devices_found"
|
2020-04-29 05:17:35 +00:00
|
|
|
RESULT_NOT_SUPPORTED = "not_supported"
|
2020-04-20 13:00:07 +00:00
|
|
|
RESULT_SUCCESS = "success"
|
|
|
|
|
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
class FritzboxConfigFlow(ConfigFlow, domain=DOMAIN):
|
2021-05-03 10:52:22 +00:00
|
|
|
"""Handle a AVM FRITZ!SmartHome config flow."""
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
def __init__(self) -> None:
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Initialize flow."""
|
2021-05-15 05:54:11 +00:00
|
|
|
self._entry: ConfigEntry | None = None
|
|
|
|
self._host: str | None = None
|
|
|
|
self._name: str | None = None
|
|
|
|
self._password: str | None = None
|
|
|
|
self._username: str | None = None
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
def _get_entry(self, name: str) -> FlowResult:
|
2020-04-20 13:00:07 +00:00
|
|
|
return self.async_create_entry(
|
2021-05-15 05:54:11 +00:00
|
|
|
title=name,
|
2020-04-20 13:00:07 +00:00
|
|
|
data={
|
|
|
|
CONF_HOST: self._host,
|
|
|
|
CONF_PASSWORD: self._password,
|
|
|
|
CONF_USERNAME: self._username,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
async def _update_entry(self) -> None:
|
|
|
|
assert self._entry is not None
|
2021-01-28 23:08:59 +00:00
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
self._entry,
|
|
|
|
data={
|
|
|
|
CONF_HOST: self._host,
|
|
|
|
CONF_PASSWORD: self._password,
|
|
|
|
CONF_USERNAME: self._username,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await self.hass.config_entries.async_reload(self._entry.entry_id)
|
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
def _try_connect(self) -> str:
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Try to connect and check auth."""
|
|
|
|
fritzbox = Fritzhome(
|
|
|
|
host=self._host, user=self._username, password=self._password
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
fritzbox.login()
|
2020-04-29 05:17:35 +00:00
|
|
|
fritzbox.get_device_elements()
|
2020-04-20 13:00:07 +00:00
|
|
|
fritzbox.logout()
|
|
|
|
return RESULT_SUCCESS
|
|
|
|
except LoginError:
|
2020-10-08 16:12:04 +00:00
|
|
|
return RESULT_INVALID_AUTH
|
2020-04-29 05:17:35 +00:00
|
|
|
except HTTPError:
|
|
|
|
return RESULT_NOT_SUPPORTED
|
|
|
|
except OSError:
|
2020-10-08 16:12:04 +00:00
|
|
|
return RESULT_NO_DEVICES_FOUND
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
2021-05-11 20:00:12 +00:00
|
|
|
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
self._host = user_input[CONF_HOST]
|
2021-05-15 05:54:11 +00:00
|
|
|
self._name = str(user_input[CONF_HOST])
|
2020-04-20 13:00:07 +00:00
|
|
|
self._password = user_input[CONF_PASSWORD]
|
|
|
|
self._username = user_input[CONF_USERNAME]
|
|
|
|
|
|
|
|
result = await self.hass.async_add_executor_job(self._try_connect)
|
|
|
|
|
|
|
|
if result == RESULT_SUCCESS:
|
2021-05-15 05:54:11 +00:00
|
|
|
return self._get_entry(self._name)
|
2020-10-08 16:12:04 +00:00
|
|
|
if result != RESULT_INVALID_AUTH:
|
2020-04-20 13:00:07 +00:00
|
|
|
return self.async_abort(reason=result)
|
|
|
|
errors["base"] = result
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=DATA_SCHEMA_USER, errors=errors
|
|
|
|
)
|
|
|
|
|
2021-11-29 16:10:07 +00:00
|
|
|
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Handle a flow initialized by discovery."""
|
2021-12-01 10:07:10 +00:00
|
|
|
host = urlparse(discovery_info.ssdp_location).hostname
|
2021-05-15 05:54:11 +00:00
|
|
|
assert isinstance(host, str)
|
2020-04-20 13:00:07 +00:00
|
|
|
self.context[CONF_HOST] = host
|
|
|
|
|
2021-12-01 10:07:10 +00:00
|
|
|
if uuid := discovery_info.upnp.get(ssdp.ATTR_UPNP_UDN):
|
2020-04-27 18:18:32 +00:00
|
|
|
if uuid.startswith("uuid:"):
|
|
|
|
uuid = uuid[5:]
|
|
|
|
await self.async_set_unique_id(uuid)
|
|
|
|
self._abort_if_unique_id_configured({CONF_HOST: host})
|
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
for progress in self._async_in_progress():
|
|
|
|
if progress.get("context", {}).get(CONF_HOST) == host:
|
|
|
|
return self.async_abort(reason="already_in_progress")
|
|
|
|
|
2020-04-27 18:18:32 +00:00
|
|
|
# update old and user-configured config entries
|
2021-05-12 10:47:06 +00:00
|
|
|
for entry in self._async_current_entries():
|
2020-04-26 06:57:44 +00:00
|
|
|
if entry.data[CONF_HOST] == host:
|
2020-04-27 18:18:32 +00:00
|
|
|
if uuid and not entry.unique_id:
|
|
|
|
self.hass.config_entries.async_update_entry(entry, unique_id=uuid)
|
2020-04-20 13:00:07 +00:00
|
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
|
|
|
|
self._host = host
|
2021-12-01 10:07:10 +00:00
|
|
|
self._name = str(discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME) or host)
|
2020-04-20 13:00:07 +00:00
|
|
|
|
|
|
|
self.context["title_placeholders"] = {"name": self._name}
|
|
|
|
return await self.async_step_confirm()
|
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
async def async_step_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2020-04-20 13:00:07 +00:00
|
|
|
"""Handle user-confirmation of discovered node."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
self._password = user_input[CONF_PASSWORD]
|
|
|
|
self._username = user_input[CONF_USERNAME]
|
|
|
|
result = await self.hass.async_add_executor_job(self._try_connect)
|
|
|
|
|
|
|
|
if result == RESULT_SUCCESS:
|
2021-05-15 05:54:11 +00:00
|
|
|
assert self._name is not None
|
|
|
|
return self._get_entry(self._name)
|
2020-10-08 16:12:04 +00:00
|
|
|
if result != RESULT_INVALID_AUTH:
|
2020-04-20 13:00:07 +00:00
|
|
|
return self.async_abort(reason=result)
|
|
|
|
errors["base"] = result
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="confirm",
|
|
|
|
data_schema=DATA_SCHEMA_CONFIRM,
|
|
|
|
description_placeholders={"name": self._name},
|
|
|
|
errors=errors,
|
|
|
|
)
|
2021-01-28 23:08:59 +00:00
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
async def async_step_reauth(self, data: dict[str, str]) -> FlowResult:
|
2021-01-28 23:08:59 +00:00
|
|
|
"""Trigger a reauthentication flow."""
|
2021-05-15 05:54:11 +00:00
|
|
|
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
|
|
|
|
assert entry is not None
|
|
|
|
self._entry = entry
|
2021-04-10 05:41:29 +00:00
|
|
|
self._host = data[CONF_HOST]
|
2021-05-15 05:54:11 +00:00
|
|
|
self._name = str(data[CONF_HOST])
|
2021-04-10 05:41:29 +00:00
|
|
|
self._username = data[CONF_USERNAME]
|
2021-01-28 23:08:59 +00:00
|
|
|
|
|
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
|
2021-05-15 05:54:11 +00:00
|
|
|
async def async_step_reauth_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2021-01-28 23:08:59 +00:00
|
|
|
"""Handle reauthorization flow."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
self._password = user_input[CONF_PASSWORD]
|
|
|
|
self._username = user_input[CONF_USERNAME]
|
|
|
|
|
|
|
|
result = await self.hass.async_add_executor_job(self._try_connect)
|
|
|
|
|
|
|
|
if result == RESULT_SUCCESS:
|
|
|
|
await self._update_entry()
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
|
|
|
if result != RESULT_INVALID_AUTH:
|
|
|
|
return self.async_abort(reason=result)
|
|
|
|
errors["base"] = result
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="reauth_confirm",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME, default=self._username): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
description_placeholders={"name": self._name},
|
|
|
|
errors=errors,
|
|
|
|
)
|