2024-07-10 21:02:33 +00:00
|
|
|
"""Config flow to configure russound_rio component."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
2024-09-26 12:38:36 +00:00
|
|
|
from aiorussound import RussoundClient, RussoundTcpConnectionHandler
|
2024-07-10 21:02:33 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2024-12-18 07:40:27 +00:00
|
|
|
from homeassistant.config_entries import (
|
|
|
|
SOURCE_RECONFIGURE,
|
|
|
|
ConfigFlow,
|
|
|
|
ConfigFlowResult,
|
|
|
|
)
|
2024-12-30 21:46:08 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
2024-07-10 21:02:33 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2025-01-15 14:49:01 +00:00
|
|
|
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
2024-07-10 21:02:33 +00:00
|
|
|
|
2024-12-27 10:01:10 +00:00
|
|
|
from .const import DOMAIN, RUSSOUND_RIO_EXCEPTIONS
|
2024-07-10 21:02:33 +00:00
|
|
|
|
|
|
|
DATA_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=9621): cv.port,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class FlowHandler(ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Russound RIO configuration flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2024-12-30 21:46:08 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Initialize the config flow."""
|
|
|
|
self.data: dict[str, Any] = {}
|
|
|
|
|
|
|
|
async def async_step_zeroconf(
|
2025-01-15 14:49:01 +00:00
|
|
|
self, discovery_info: ZeroconfServiceInfo
|
2024-12-30 21:46:08 +00:00
|
|
|
) -> ConfigFlowResult:
|
|
|
|
"""Handle zeroconf discovery."""
|
|
|
|
self.data[CONF_HOST] = host = discovery_info.host
|
|
|
|
self.data[CONF_PORT] = port = discovery_info.port or 9621
|
|
|
|
|
|
|
|
client = RussoundClient(RussoundTcpConnectionHandler(host, port))
|
|
|
|
try:
|
|
|
|
await client.connect()
|
|
|
|
controller = client.controllers[1]
|
|
|
|
await client.disconnect()
|
|
|
|
except RUSSOUND_RIO_EXCEPTIONS:
|
|
|
|
return self.async_abort(reason="cannot_connect")
|
|
|
|
|
|
|
|
await self.async_set_unique_id(controller.mac_address)
|
|
|
|
self._abort_if_unique_id_configured(updates={CONF_HOST: host})
|
|
|
|
|
|
|
|
self.data[CONF_NAME] = controller.controller_type
|
|
|
|
|
|
|
|
self.context["title_placeholders"] = {
|
|
|
|
"name": self.data[CONF_NAME],
|
|
|
|
}
|
|
|
|
return await self.async_step_discovery_confirm()
|
|
|
|
|
|
|
|
async def async_step_discovery_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> ConfigFlowResult:
|
|
|
|
"""Confirm discovery."""
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=self.data[CONF_NAME],
|
|
|
|
data={CONF_HOST: self.data[CONF_HOST], CONF_PORT: self.data[CONF_PORT]},
|
|
|
|
)
|
|
|
|
|
|
|
|
self._set_confirm_only()
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="discovery_confirm",
|
|
|
|
description_placeholders={
|
|
|
|
"name": self.data[CONF_NAME],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-07-10 21:02:33 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> ConfigFlowResult:
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
errors: dict[str, str] = {}
|
|
|
|
if user_input is not None:
|
|
|
|
host = user_input[CONF_HOST]
|
|
|
|
port = user_input[CONF_PORT]
|
|
|
|
|
2024-09-26 12:38:36 +00:00
|
|
|
client = RussoundClient(RussoundTcpConnectionHandler(host, port))
|
2024-07-10 21:02:33 +00:00
|
|
|
try:
|
2024-12-27 10:01:10 +00:00
|
|
|
await client.connect()
|
|
|
|
controller = client.controllers[1]
|
|
|
|
await client.disconnect()
|
2024-07-10 21:02:33 +00:00
|
|
|
except RUSSOUND_RIO_EXCEPTIONS:
|
|
|
|
_LOGGER.exception("Could not connect to Russound RIO")
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
else:
|
2024-12-30 21:46:08 +00:00
|
|
|
await self.async_set_unique_id(
|
|
|
|
controller.mac_address, raise_on_progress=False
|
|
|
|
)
|
2024-12-18 07:40:27 +00:00
|
|
|
if self.source == SOURCE_RECONFIGURE:
|
|
|
|
self._abort_if_unique_id_mismatch(reason="wrong_device")
|
|
|
|
return self.async_update_reload_and_abort(
|
|
|
|
self._get_reconfigure_entry(),
|
|
|
|
data_updates=user_input,
|
|
|
|
)
|
2024-07-10 21:02:33 +00:00
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
data = {CONF_HOST: host, CONF_PORT: port}
|
2024-09-26 12:38:36 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title=controller.controller_type, data=data
|
|
|
|
)
|
2024-07-10 21:02:33 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
|
|
|
)
|
|
|
|
|
2024-12-18 07:40:27 +00:00
|
|
|
async def async_step_reconfigure(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> ConfigFlowResult:
|
|
|
|
"""Handle reconfiguration of the integration."""
|
|
|
|
if not user_input:
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="reconfigure",
|
|
|
|
data_schema=DATA_SCHEMA,
|
|
|
|
)
|
|
|
|
return await self.async_step_user(user_input)
|