2019-06-02 05:13:14 +00:00
|
|
|
"""Config flow to configure the AdGuard Home integration."""
|
2021-03-06 22:19:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2019-06-02 05:13:14 +00:00
|
|
|
from adguardhome import AdGuardHome, AdGuardHomeConnectionError
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigFlow
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
2021-04-29 11:40:51 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2019-06-02 05:13:14 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2021-03-30 04:02:56 +00:00
|
|
|
from .const import DOMAIN
|
2021-03-06 22:19:03 +00:00
|
|
|
|
2019-06-02 05:13:14 +00:00
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
class AdGuardHomeFlowHandler(ConfigFlow, domain=DOMAIN):
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Handle a AdGuard Home config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
_hassio_discovery = None
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
async def _show_setup_form(
|
|
|
|
self, errors: dict[str, str] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Show the setup form to the user."""
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="user",
|
2019-06-02 05:13:14 +00:00
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): str,
|
|
|
|
vol.Required(CONF_PORT, default=3000): vol.Coerce(int),
|
|
|
|
vol.Optional(CONF_USERNAME): str,
|
|
|
|
vol.Optional(CONF_PASSWORD): str,
|
|
|
|
vol.Required(CONF_SSL, default=True): bool,
|
|
|
|
vol.Required(CONF_VERIFY_SSL, default=True): bool,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors or {},
|
|
|
|
)
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
async def _show_hassio_form(
|
|
|
|
self, errors: dict[str, str] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Show the Hass.io confirmation form to the user."""
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="hassio_confirm",
|
|
|
|
description_placeholders={"addon": self._hassio_discovery["addon"]},
|
2019-06-02 05:13:14 +00:00
|
|
|
data_schema=vol.Schema({}),
|
|
|
|
errors=errors or {},
|
|
|
|
)
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Handle a flow initiated by the user."""
|
|
|
|
if user_input is None:
|
|
|
|
return await self._show_setup_form(user_input)
|
|
|
|
|
2021-05-11 20:00:12 +00:00
|
|
|
self._async_abort_entries_match(
|
|
|
|
{CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]}
|
|
|
|
)
|
2021-04-15 19:32:52 +00:00
|
|
|
|
2019-06-02 05:13:14 +00:00
|
|
|
errors = {}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
session = async_get_clientsession(self.hass, user_input[CONF_VERIFY_SSL])
|
2019-06-02 05:13:14 +00:00
|
|
|
|
|
|
|
adguard = AdGuardHome(
|
|
|
|
user_input[CONF_HOST],
|
|
|
|
port=user_input[CONF_PORT],
|
|
|
|
username=user_input.get(CONF_USERNAME),
|
|
|
|
password=user_input.get(CONF_PASSWORD),
|
|
|
|
tls=user_input[CONF_SSL],
|
|
|
|
verify_ssl=user_input[CONF_VERIFY_SSL],
|
|
|
|
session=session,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2020-07-28 15:51:35 +00:00
|
|
|
await adguard.version()
|
2019-06-02 05:13:14 +00:00
|
|
|
except AdGuardHomeConnectionError:
|
2020-10-01 10:56:55 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
2019-06-02 05:13:14 +00:00
|
|
|
return await self._show_setup_form(errors)
|
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input[CONF_HOST],
|
|
|
|
data={
|
|
|
|
CONF_HOST: user_input[CONF_HOST],
|
|
|
|
CONF_PASSWORD: user_input.get(CONF_PASSWORD),
|
|
|
|
CONF_PORT: user_input[CONF_PORT],
|
|
|
|
CONF_SSL: user_input[CONF_SSL],
|
|
|
|
CONF_USERNAME: user_input.get(CONF_USERNAME),
|
|
|
|
CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-04-29 11:40:51 +00:00
|
|
|
async def async_step_hassio(self, discovery_info: dict[str, Any]) -> FlowResult:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Prepare configuration for a Hass.io AdGuard Home add-on.
|
|
|
|
|
|
|
|
This flow is triggered by the discovery component.
|
|
|
|
"""
|
2021-04-16 07:11:19 +00:00
|
|
|
await self._async_handle_discovery_without_unique_id()
|
2019-06-02 05:13:14 +00:00
|
|
|
|
2021-04-16 07:11:19 +00:00
|
|
|
self._hassio_discovery = discovery_info
|
|
|
|
return await self.async_step_hassio_confirm()
|
2019-06-02 05:13:14 +00:00
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
async def async_step_hassio_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2021-04-15 21:34:49 +00:00
|
|
|
"""Confirm Supervisor discovery."""
|
2019-06-02 05:13:14 +00:00
|
|
|
if user_input is None:
|
|
|
|
return await self._show_hassio_form()
|
|
|
|
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
session = async_get_clientsession(self.hass, False)
|
|
|
|
|
|
|
|
adguard = AdGuardHome(
|
|
|
|
self._hassio_discovery[CONF_HOST],
|
|
|
|
port=self._hassio_discovery[CONF_PORT],
|
|
|
|
tls=False,
|
|
|
|
session=session,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2020-07-28 15:51:35 +00:00
|
|
|
await adguard.version()
|
2019-06-02 05:13:14 +00:00
|
|
|
except AdGuardHomeConnectionError:
|
2020-10-01 10:56:55 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
2019-06-02 05:13:14 +00:00
|
|
|
return await self._show_hassio_form(errors)
|
|
|
|
|
|
|
|
return self.async_create_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
title=self._hassio_discovery["addon"],
|
2019-06-02 05:13:14 +00:00
|
|
|
data={
|
|
|
|
CONF_HOST: self._hassio_discovery[CONF_HOST],
|
|
|
|
CONF_PORT: self._hassio_discovery[CONF_PORT],
|
|
|
|
CONF_PASSWORD: None,
|
|
|
|
CONF_SSL: False,
|
|
|
|
CONF_USERNAME: None,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|