1 """Config flow for roon integration."""
7 from roonapi
import RoonApi, RoonDiscovery
8 import voluptuous
as vol
25 _LOGGER = logging.getLogger(__name__)
27 DATA_SCHEMA = vol.Schema(
29 vol.Required(
"host"): cv.string,
30 vol.Required(
"port", default=9330): cv.port,
38 """Interact with roon during config flow."""
40 def __init__(self, hass: HomeAssistant) ->
None:
41 """Initialise the RoonHub."""
44 async
def discover(self) -> list[tuple[str, int]]:
45 """Try and discover roon servers."""
47 def get_discovered_servers(discovery: RoonDiscovery) -> list[tuple[str, int]]:
48 servers = discovery.all()
52 discovery = RoonDiscovery(
None)
53 servers = await self.
_hass_hass.async_add_executor_job(
54 get_discovered_servers, discovery
56 _LOGGER.debug(
"Servers = %s", servers)
60 """Authenticate with one or more roon servers."""
72 RoonApi(ROON_APPINFO,
None, server[0], server[1], blocking_init=
False)
76 apis = [RoonApi(ROON_APPINFO,
None, host, port, blocking_init=
False)]
78 while secs <= TIMEOUT:
81 auth_api = [api
for api
in apis
if api.token
is not None]
83 secs += AUTHENTICATE_TIMEOUT
85 core_id = auth_api[0].core_id
86 core_name = auth_api[0].core_name
87 token = auth_api[0].token
90 await asyncio.sleep(AUTHENTICATE_TIMEOUT)
92 await self.
_hass_hass.async_add_executor_job(stop_apis, apis)
94 return (token, core_id, core_name)
97 async
def discover(hass: HomeAssistant) -> list[tuple[str, int]]:
98 """Connect and authenticate home assistant."""
101 return await hub.discover()
105 """Connect and authenticate home assistant."""
108 (token, core_id, core_name) = await hub.authenticate(host, port, servers)
115 CONF_ROON_ID: core_id,
116 CONF_ROON_NAME: core_name,
122 """Handle a config flow for roon."""
127 """Initialize the Roon flow."""
130 self.
_servers_servers: list[tuple[str, int]] = []
133 self, user_input: dict[str, Any] |
None =
None
134 ) -> ConfigFlowResult:
135 """Get roon core details via discovery."""
146 self, user_input: dict[str, Any] |
None =
None
147 ) -> ConfigFlowResult:
148 """Get host and port details from the user."""
149 errors: dict[str, str] = {}
151 if user_input
is not None:
152 self.
_host_host = user_input[
"host"]
153 self.
_port_port = user_input[
"port"]
157 step_id=
"fallback", data_schema=DATA_SCHEMA, errors=errors
161 self, user_input: dict[str, Any] |
None =
None
162 ) -> ConfigFlowResult:
163 """Handle linking and authenticating with the roon server."""
165 if user_input
is not None:
175 errors[
"base"] =
"invalid_auth"
177 _LOGGER.exception(
"Unexpected exception")
178 errors[
"base"] =
"unknown"
186 """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_link(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_fallback(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
None __init__(self, HomeAssistant hass)
list[tuple[str, int]] discover(self)
def authenticate(self, host, port, servers)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
def authenticate(HomeAssistant hass, host, port, servers)
list[tuple[str, int]] discover(HomeAssistant hass)