mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 19:57:07 +00:00
Improve google cast known hosts configuration (#140913)
This commit is contained in:
parent
7c6abe17a2
commit
793e36635b
@ -16,12 +16,21 @@ from homeassistant.config_entries import (
|
|||||||
from homeassistant.const import CONF_UUID
|
from homeassistant.const import CONF_UUID
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
|
||||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||||
|
|
||||||
from .const import CONF_IGNORE_CEC, CONF_KNOWN_HOSTS, DOMAIN
|
from .const import CONF_IGNORE_CEC, CONF_KNOWN_HOSTS, DOMAIN
|
||||||
|
|
||||||
IGNORE_CEC_SCHEMA = vol.Schema(vol.All(cv.ensure_list, [cv.string]))
|
IGNORE_CEC_SCHEMA = vol.Schema(vol.All(cv.ensure_list, [cv.string]))
|
||||||
KNOWN_HOSTS_SCHEMA = vol.Schema(vol.All(cv.ensure_list, [cv.string]))
|
KNOWN_HOSTS_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional(
|
||||||
|
CONF_KNOWN_HOSTS,
|
||||||
|
): SelectSelector(
|
||||||
|
SelectSelectorConfig(custom_value=True, options=[], multiple=True),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
WANTED_UUID_SCHEMA = vol.Schema(vol.All(cv.ensure_list, [cv.string]))
|
WANTED_UUID_SCHEMA = vol.Schema(vol.All(cv.ensure_list, [cv.string]))
|
||||||
|
|
||||||
|
|
||||||
@ -30,12 +39,6 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
"""Initialize flow."""
|
|
||||||
self._ignore_cec = set[str]()
|
|
||||||
self._known_hosts = set[str]()
|
|
||||||
self._wanted_uuid = set[str]()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(
|
def async_get_options_flow(
|
||||||
@ -62,48 +65,31 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Confirm the setup."""
|
"""Confirm the setup."""
|
||||||
errors = {}
|
|
||||||
data = {CONF_KNOWN_HOSTS: self._known_hosts}
|
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
bad_hosts = False
|
known_hosts = _trim_items(user_input.get(CONF_KNOWN_HOSTS, []))
|
||||||
known_hosts = user_input[CONF_KNOWN_HOSTS]
|
return self.async_create_entry(
|
||||||
known_hosts = [x.strip() for x in known_hosts.split(",") if x.strip()]
|
title="Google Cast",
|
||||||
try:
|
data=self._get_data(known_hosts=known_hosts),
|
||||||
known_hosts = KNOWN_HOSTS_SCHEMA(known_hosts)
|
)
|
||||||
except vol.Invalid:
|
|
||||||
errors["base"] = "invalid_known_hosts"
|
|
||||||
bad_hosts = True
|
|
||||||
else:
|
|
||||||
self._known_hosts = known_hosts
|
|
||||||
data = self._get_data()
|
|
||||||
if not bad_hosts:
|
|
||||||
return self.async_create_entry(title="Google Cast", data=data)
|
|
||||||
|
|
||||||
fields = {}
|
return self.async_show_form(step_id="config", data_schema=KNOWN_HOSTS_SCHEMA)
|
||||||
fields[vol.Optional(CONF_KNOWN_HOSTS, default="")] = str
|
|
||||||
|
|
||||||
return self.async_show_form(
|
|
||||||
step_id="config", data_schema=vol.Schema(fields), errors=errors
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_step_confirm(
|
async def async_step_confirm(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Confirm the setup."""
|
"""Confirm the setup."""
|
||||||
|
|
||||||
data = self._get_data()
|
|
||||||
|
|
||||||
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
|
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
|
||||||
return self.async_create_entry(title="Google Cast", data=data)
|
return self.async_create_entry(title="Google Cast", data=self._get_data())
|
||||||
|
|
||||||
return self.async_show_form(step_id="confirm")
|
return self.async_show_form(step_id="confirm")
|
||||||
|
|
||||||
def _get_data(self):
|
def _get_data(
|
||||||
|
self, *, known_hosts: list[str] | None = None
|
||||||
|
) -> dict[str, list[str]]:
|
||||||
return {
|
return {
|
||||||
CONF_IGNORE_CEC: list(self._ignore_cec),
|
CONF_IGNORE_CEC: [],
|
||||||
CONF_KNOWN_HOSTS: list(self._known_hosts),
|
CONF_KNOWN_HOSTS: known_hosts or [],
|
||||||
CONF_UUID: list(self._wanted_uuid),
|
CONF_UUID: [],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -123,31 +109,24 @@ class CastOptionsFlowHandler(OptionsFlow):
|
|||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Manage the Google Cast options."""
|
"""Manage the Google Cast options."""
|
||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
current_config = self.config_entry.data
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
bad_hosts, known_hosts = _string_to_list(
|
known_hosts = _trim_items(user_input.get(CONF_KNOWN_HOSTS, []))
|
||||||
user_input.get(CONF_KNOWN_HOSTS, ""), KNOWN_HOSTS_SCHEMA
|
self.updated_config = dict(self.config_entry.data)
|
||||||
|
self.updated_config[CONF_KNOWN_HOSTS] = known_hosts
|
||||||
|
|
||||||
|
if self.show_advanced_options:
|
||||||
|
return await self.async_step_advanced_options()
|
||||||
|
|
||||||
|
self.hass.config_entries.async_update_entry(
|
||||||
|
self.config_entry, data=self.updated_config
|
||||||
)
|
)
|
||||||
|
return self.async_create_entry(title="", data={})
|
||||||
if not bad_hosts:
|
|
||||||
self.updated_config = dict(current_config)
|
|
||||||
self.updated_config[CONF_KNOWN_HOSTS] = known_hosts
|
|
||||||
|
|
||||||
if self.show_advanced_options:
|
|
||||||
return await self.async_step_advanced_options()
|
|
||||||
|
|
||||||
self.hass.config_entries.async_update_entry(
|
|
||||||
self.config_entry, data=self.updated_config
|
|
||||||
)
|
|
||||||
return self.async_create_entry(title="", data={})
|
|
||||||
|
|
||||||
fields: dict[vol.Marker, type[str]] = {}
|
|
||||||
suggested_value = _list_to_string(current_config.get(CONF_KNOWN_HOSTS))
|
|
||||||
_add_with_suggestion(fields, CONF_KNOWN_HOSTS, suggested_value)
|
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="basic_options",
|
step_id="basic_options",
|
||||||
data_schema=vol.Schema(fields),
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
|
KNOWN_HOSTS_SCHEMA, self.config_entry.data
|
||||||
|
),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
last_step=not self.show_advanced_options,
|
last_step=not self.show_advanced_options,
|
||||||
)
|
)
|
||||||
@ -206,6 +185,10 @@ def _string_to_list(string, schema):
|
|||||||
return invalid, items
|
return invalid, items
|
||||||
|
|
||||||
|
|
||||||
|
def _trim_items(items: list[str]) -> list[str]:
|
||||||
|
return [x.strip() for x in items if x.strip()]
|
||||||
|
|
||||||
|
|
||||||
def _add_with_suggestion(
|
def _add_with_suggestion(
|
||||||
fields: dict[vol.Marker, type[str]], key: str, suggested_value: str
|
fields: dict[vol.Marker, type[str]], key: str, suggested_value: str
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -6,9 +6,11 @@
|
|||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"title": "Google Cast configuration",
|
"title": "Google Cast configuration",
|
||||||
"description": "Known Hosts - A comma-separated list of hostnames or IP-addresses of cast devices, use if mDNS discovery is not working.",
|
|
||||||
"data": {
|
"data": {
|
||||||
"known_hosts": "Known hosts"
|
"known_hosts": "Add known host"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"known_hosts": "Hostnames or IP-addresses of cast devices, use if mDNS discovery is not working"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -20,9 +22,11 @@
|
|||||||
"step": {
|
"step": {
|
||||||
"basic_options": {
|
"basic_options": {
|
||||||
"title": "[%key:component::cast::config::step::config::title%]",
|
"title": "[%key:component::cast::config::step::config::title%]",
|
||||||
"description": "[%key:component::cast::config::step::config::description%]",
|
|
||||||
"data": {
|
"data": {
|
||||||
"known_hosts": "[%key:component::cast::config::step::config::data::known_hosts%]"
|
"known_hosts": "[%key:component::cast::config::step::config::data::known_hosts%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"known_hosts": "[%key:component::cast::config::step::config::data_description::known_hosts%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"advanced_options": {
|
"advanced_options": {
|
||||||
|
@ -87,7 +87,7 @@ async def test_user_setup_options(hass: HomeAssistant) -> None:
|
|||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], {"known_hosts": "192.168.0.1, , 192.168.0.2 "}
|
result["flow_id"], {"known_hosts": ["192.168.0.1", "", " ", "192.168.0.2 "]}
|
||||||
)
|
)
|
||||||
|
|
||||||
users = await hass.auth.async_get_users()
|
users = await hass.auth.async_get_users()
|
||||||
@ -152,13 +152,13 @@ def get_suggested(schema, key):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"parameter_data",
|
("parameter", "initial", "suggested", "user_input", "updated"),
|
||||||
[
|
[
|
||||||
(
|
(
|
||||||
"known_hosts",
|
"known_hosts",
|
||||||
["192.168.0.10", "192.168.0.11"],
|
["192.168.0.10", "192.168.0.11"],
|
||||||
"192.168.0.10,192.168.0.11",
|
["192.168.0.10", "192.168.0.11"],
|
||||||
"192.168.0.1, , 192.168.0.2 ",
|
["192.168.0.1", " ", " 192.168.0.2 "],
|
||||||
["192.168.0.1", "192.168.0.2"],
|
["192.168.0.1", "192.168.0.2"],
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@ -177,11 +177,17 @@ def get_suggested(schema, key):
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_option_flow(hass: HomeAssistant, parameter_data) -> None:
|
async def test_option_flow(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
parameter: str,
|
||||||
|
initial: list[str],
|
||||||
|
suggested: str | list[str],
|
||||||
|
user_input: str | list[str],
|
||||||
|
updated: list[str],
|
||||||
|
) -> None:
|
||||||
"""Test config flow options."""
|
"""Test config flow options."""
|
||||||
basic_parameters = ["known_hosts"]
|
basic_parameters = ["known_hosts"]
|
||||||
advanced_parameters = ["ignore_cec", "uuid"]
|
advanced_parameters = ["ignore_cec", "uuid"]
|
||||||
parameter, initial, suggested, user_input, updated = parameter_data
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"ignore_cec": [],
|
"ignore_cec": [],
|
||||||
@ -213,7 +219,7 @@ async def test_option_flow(hass: HomeAssistant, parameter_data) -> None:
|
|||||||
for other_param in basic_parameters:
|
for other_param in basic_parameters:
|
||||||
if other_param == parameter:
|
if other_param == parameter:
|
||||||
continue
|
continue
|
||||||
assert get_suggested(data_schema, other_param) == ""
|
assert get_suggested(data_schema, other_param) == []
|
||||||
if parameter in basic_parameters:
|
if parameter in basic_parameters:
|
||||||
assert get_suggested(data_schema, parameter) == suggested
|
assert get_suggested(data_schema, parameter) == suggested
|
||||||
|
|
||||||
@ -261,7 +267,7 @@ async def test_option_flow(hass: HomeAssistant, parameter_data) -> None:
|
|||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={"known_hosts": ""},
|
user_input={},
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["data"] == {}
|
assert result["data"] == {}
|
||||||
@ -277,7 +283,7 @@ async def test_known_hosts(hass: HomeAssistant, castbrowser_mock) -> None:
|
|||||||
"cast", context={"source": config_entries.SOURCE_USER}
|
"cast", context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], {"known_hosts": "192.168.0.1, 192.168.0.2"}
|
result["flow_id"], {"known_hosts": ["192.168.0.1", "192.168.0.2"]}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
await hass.async_block_till_done(wait_background_tasks=True)
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
@ -290,7 +296,7 @@ async def test_known_hosts(hass: HomeAssistant, castbrowser_mock) -> None:
|
|||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={"known_hosts": "192.168.0.11, 192.168.0.12"},
|
user_input={"known_hosts": ["192.168.0.11", "192.168.0.12"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.async_block_till_done(wait_background_tasks=True)
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user