2020-07-19 20:48:08 +00:00
|
|
|
"""Config flow for Control4 integration."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2022-06-13 11:17:59 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-07-19 20:48:08 +00:00
|
|
|
import logging
|
2024-08-29 15:24:04 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2020-07-19 20:48:08 +00:00
|
|
|
|
|
|
|
from aiohttp.client_exceptions import ClientError
|
2024-08-28 05:54:28 +00:00
|
|
|
from pyControl4.account import C4Account
|
|
|
|
from pyControl4.director import C4Director
|
|
|
|
from pyControl4.error_handling import NotFound, Unauthorized
|
2020-07-19 20:48:08 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2024-08-21 20:42:58 +00:00
|
|
|
from homeassistant.config_entries import (
|
|
|
|
ConfigEntry,
|
|
|
|
ConfigFlow,
|
|
|
|
ConfigFlowResult,
|
|
|
|
OptionsFlow,
|
|
|
|
)
|
2020-07-19 20:48:08 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2024-08-29 15:24:04 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2024-02-29 19:07:14 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2020-07-19 20:48:08 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
|
|
|
from homeassistant.helpers.device_registry import format_mac
|
|
|
|
|
2021-03-30 04:02:56 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_CONTROLLER_UNIQUE_ID,
|
|
|
|
DEFAULT_SCAN_INTERVAL,
|
|
|
|
DOMAIN,
|
|
|
|
MIN_SCAN_INTERVAL,
|
|
|
|
)
|
2020-07-19 20:48:08 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DATA_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): str,
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Control4Validator:
|
|
|
|
"""Validates that config details can be used to authenticate and communicate with Control4."""
|
|
|
|
|
2024-08-29 15:24:04 +00:00
|
|
|
def __init__(
|
|
|
|
self, host: str, username: str, password: str, hass: HomeAssistant
|
|
|
|
) -> None:
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Initialize."""
|
|
|
|
self.host = host
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
self.controller_unique_id = None
|
|
|
|
self.director_bearer_token = None
|
|
|
|
self.hass = hass
|
|
|
|
|
|
|
|
async def authenticate(self) -> bool:
|
|
|
|
"""Test if we can authenticate with the Control4 account API."""
|
|
|
|
try:
|
|
|
|
account_session = aiohttp_client.async_get_clientsession(self.hass)
|
|
|
|
account = C4Account(self.username, self.password, account_session)
|
|
|
|
# Authenticate with Control4 account
|
|
|
|
await account.getAccountBearerToken()
|
|
|
|
|
|
|
|
# Get controller name
|
|
|
|
account_controllers = await account.getAccountControllers()
|
|
|
|
self.controller_unique_id = account_controllers["controllerCommonName"]
|
|
|
|
|
|
|
|
# Get bearer token to communicate with controller locally
|
|
|
|
self.director_bearer_token = (
|
|
|
|
await account.getDirectorBearerToken(self.controller_unique_id)
|
|
|
|
)["token"]
|
|
|
|
except (Unauthorized, NotFound):
|
|
|
|
return False
|
2024-03-30 09:37:59 +00:00
|
|
|
return True
|
2020-07-19 20:48:08 +00:00
|
|
|
|
|
|
|
async def connect_to_director(self) -> bool:
|
|
|
|
"""Test if we can connect to the local Control4 Director."""
|
|
|
|
try:
|
|
|
|
director_session = aiohttp_client.async_get_clientsession(
|
|
|
|
self.hass, verify_ssl=False
|
|
|
|
)
|
|
|
|
director = C4Director(
|
|
|
|
self.host, self.director_bearer_token, director_session
|
|
|
|
)
|
|
|
|
await director.getAllItemInfo()
|
2024-02-05 17:45:16 +00:00
|
|
|
except (Unauthorized, ClientError, TimeoutError):
|
2020-07-19 20:48:08 +00:00
|
|
|
_LOGGER.error("Failed to connect to the Control4 controller")
|
|
|
|
return False
|
2024-03-30 09:37:59 +00:00
|
|
|
return True
|
2020-07-19 20:48:08 +00:00
|
|
|
|
|
|
|
|
2024-02-29 19:07:14 +00:00
|
|
|
class Control4ConfigFlow(ConfigFlow, domain=DOMAIN):
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Handle a config flow for Control4."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2024-08-21 20:42:58 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> ConfigFlowResult:
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Handle the initial step."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
hub = Control4Validator(
|
2021-10-28 20:15:30 +00:00
|
|
|
user_input[CONF_HOST],
|
|
|
|
user_input[CONF_USERNAME],
|
|
|
|
user_input[CONF_PASSWORD],
|
2020-07-19 20:48:08 +00:00
|
|
|
self.hass,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
if not await hub.authenticate():
|
2024-08-12 07:16:33 +00:00
|
|
|
raise InvalidAuth # noqa: TRY301
|
2020-07-19 20:48:08 +00:00
|
|
|
if not await hub.connect_to_director():
|
2024-08-12 07:16:33 +00:00
|
|
|
raise CannotConnect # noqa: TRY301
|
2020-07-19 20:48:08 +00:00
|
|
|
except InvalidAuth:
|
|
|
|
errors["base"] = "invalid_auth"
|
|
|
|
except CannotConnect:
|
|
|
|
errors["base"] = "cannot_connect"
|
2024-05-07 12:00:27 +00:00
|
|
|
except Exception:
|
2020-07-19 20:48:08 +00:00
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
|
|
|
if not errors:
|
|
|
|
controller_unique_id = hub.controller_unique_id
|
2024-08-29 15:24:04 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
assert hub.controller_unique_id
|
2020-07-19 20:48:08 +00:00
|
|
|
mac = (controller_unique_id.split("_", 3))[2]
|
|
|
|
formatted_mac = format_mac(mac)
|
|
|
|
await self.async_set_unique_id(formatted_mac)
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=controller_unique_id,
|
|
|
|
data={
|
2021-10-28 20:15:30 +00:00
|
|
|
CONF_HOST: user_input[CONF_HOST],
|
|
|
|
CONF_USERNAME: user_input[CONF_USERNAME],
|
|
|
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
2020-07-19 20:48:08 +00:00
|
|
|
CONF_CONTROLLER_UNIQUE_ID: controller_unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
2021-11-11 15:28:46 +00:00
|
|
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
2020-07-19 20:48:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
2022-06-13 11:17:59 +00:00
|
|
|
def async_get_options_flow(
|
2024-02-29 19:07:14 +00:00
|
|
|
config_entry: ConfigEntry,
|
2022-06-13 11:17:59 +00:00
|
|
|
) -> OptionsFlowHandler:
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Get the options flow for this handler."""
|
2024-11-02 17:15:41 +00:00
|
|
|
return OptionsFlowHandler()
|
2020-07-19 20:48:08 +00:00
|
|
|
|
|
|
|
|
2024-02-29 19:07:14 +00:00
|
|
|
class OptionsFlowHandler(OptionsFlow):
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Handle a option flow for Control4."""
|
|
|
|
|
2024-08-29 15:24:04 +00:00
|
|
|
async def async_step_init(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> ConfigFlowResult:
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Handle options flow."""
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
|
|
|
|
data_schema = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
|
|
|
),
|
|
|
|
): vol.All(cv.positive_int, vol.Clamp(min=MIN_SCAN_INTERVAL)),
|
|
|
|
}
|
|
|
|
)
|
2021-11-11 15:28:46 +00:00
|
|
|
return self.async_show_form(step_id="init", data_schema=data_schema)
|
2020-07-19 20:48:08 +00:00
|
|
|
|
|
|
|
|
2024-02-29 19:07:14 +00:00
|
|
|
class CannotConnect(HomeAssistantError):
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Error to indicate we cannot connect."""
|
|
|
|
|
|
|
|
|
2024-02-29 19:07:14 +00:00
|
|
|
class InvalidAuth(HomeAssistantError):
|
2020-07-19 20:48:08 +00:00
|
|
|
"""Error to indicate there is invalid auth."""
|