mirror of
https://github.com/home-assistant/core.git
synced 2026-08-03 20:24:55 +02:00
Remove useless input validation from cast options flow (#171171)
This commit is contained in:
@@ -9,7 +9,6 @@ from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFl
|
|||||||
from homeassistant.const import CONF_UUID
|
from homeassistant.const import CONF_UUID
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.data_entry_flow import SectionConfig, section
|
from homeassistant.data_entry_flow import SectionConfig, section
|
||||||
from homeassistant.helpers import config_validation as cv
|
|
||||||
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
|
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
|
||||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||||
|
|
||||||
@@ -19,7 +18,6 @@ if TYPE_CHECKING:
|
|||||||
from . import CastConfigEntry
|
from . import CastConfigEntry
|
||||||
|
|
||||||
CONF_MORE_OPTIONS = "more_options"
|
CONF_MORE_OPTIONS = "more_options"
|
||||||
IGNORE_CEC_SCHEMA = vol.Schema(vol.All(cv.ensure_list, [cv.string]))
|
|
||||||
KNOWN_HOSTS_SCHEMA = vol.Schema(
|
KNOWN_HOSTS_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
@@ -42,7 +40,6 @@ OPTIONS_SCHEMA = KNOWN_HOSTS_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
WANTED_UUID_SCHEMA = vol.Schema(vol.All(cv.ensure_list, [cv.string]))
|
|
||||||
|
|
||||||
|
|
||||||
class FlowHandler(ConfigFlow, domain=DOMAIN):
|
class FlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
@@ -111,62 +108,50 @@ class CastOptionsFlowHandler(OptionsFlow):
|
|||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Manage the Google Cast options."""
|
"""Manage the Google Cast options."""
|
||||||
errors: dict[str, str] = {}
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
bad_cec, ignore_cec = _string_to_list(
|
ignore_cec = _string_to_list(
|
||||||
user_input[CONF_MORE_OPTIONS].get(CONF_IGNORE_CEC, ""),
|
user_input[CONF_MORE_OPTIONS].get(CONF_IGNORE_CEC, "")
|
||||||
IGNORE_CEC_SCHEMA,
|
|
||||||
)
|
)
|
||||||
bad_uuid, wanted_uuid = _string_to_list(
|
known_hosts = _trim_items(user_input.get(CONF_KNOWN_HOSTS, []))
|
||||||
user_input[CONF_MORE_OPTIONS].get(CONF_UUID, ""), WANTED_UUID_SCHEMA
|
wanted_uuid = _string_to_list(
|
||||||
|
user_input[CONF_MORE_OPTIONS].get(CONF_UUID, "")
|
||||||
)
|
)
|
||||||
if not bad_cec and not bad_uuid:
|
updated_config = dict(self.config_entry.data)
|
||||||
known_hosts = _trim_items(user_input.get(CONF_KNOWN_HOSTS, []))
|
updated_config[CONF_IGNORE_CEC] = ignore_cec
|
||||||
updated_config = dict(self.config_entry.data)
|
updated_config[CONF_KNOWN_HOSTS] = known_hosts
|
||||||
updated_config[CONF_IGNORE_CEC] = ignore_cec
|
updated_config[CONF_UUID] = wanted_uuid
|
||||||
updated_config[CONF_KNOWN_HOSTS] = known_hosts
|
|
||||||
updated_config[CONF_UUID] = wanted_uuid
|
|
||||||
|
|
||||||
self.hass.config_entries.async_update_entry(
|
self.hass.config_entries.async_update_entry(
|
||||||
self.config_entry, data=updated_config
|
self.config_entry, data=updated_config
|
||||||
)
|
)
|
||||||
return self.async_create_entry(title="", data={})
|
return self.async_create_entry(title="", data={})
|
||||||
suggested: dict[str, Any] = user_input
|
|
||||||
else:
|
suggested: dict[str, Any] = {CONF_MORE_OPTIONS: {}}
|
||||||
suggested = {CONF_MORE_OPTIONS: {}}
|
if CONF_KNOWN_HOSTS in self.config_entry.data:
|
||||||
if CONF_KNOWN_HOSTS in self.config_entry.data:
|
suggested[CONF_KNOWN_HOSTS] = self.config_entry.data[CONF_KNOWN_HOSTS]
|
||||||
suggested[CONF_KNOWN_HOSTS] = self.config_entry.data[CONF_KNOWN_HOSTS]
|
for key in (CONF_UUID, CONF_IGNORE_CEC):
|
||||||
for key in (CONF_UUID, CONF_IGNORE_CEC):
|
if key not in self.config_entry.data:
|
||||||
if key not in self.config_entry.data:
|
continue
|
||||||
continue
|
suggested[CONF_MORE_OPTIONS][key] = _list_to_string(
|
||||||
suggested[CONF_MORE_OPTIONS][key] = _list_to_string(
|
self.config_entry.data[key]
|
||||||
self.config_entry.data[key]
|
)
|
||||||
)
|
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="init",
|
step_id="init",
|
||||||
data_schema=self.add_suggested_values_to_schema(OPTIONS_SCHEMA, suggested),
|
data_schema=self.add_suggested_values_to_schema(OPTIONS_SCHEMA, suggested),
|
||||||
errors=errors,
|
|
||||||
last_step=True,
|
last_step=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _list_to_string(items):
|
def _list_to_string(items: list[str]) -> str:
|
||||||
comma_separated_string = ""
|
comma_separated_string = ""
|
||||||
if items:
|
if items:
|
||||||
comma_separated_string = ",".join(items)
|
comma_separated_string = ",".join(items)
|
||||||
return comma_separated_string
|
return comma_separated_string
|
||||||
|
|
||||||
|
|
||||||
def _string_to_list(string, schema):
|
def _string_to_list(string: str) -> list[str]:
|
||||||
invalid = False
|
return [x.strip() for x in string.split(",") if x.strip()]
|
||||||
items = [x.strip() for x in string.split(",") if x.strip()]
|
|
||||||
try:
|
|
||||||
items = schema(items)
|
|
||||||
except vol.Invalid:
|
|
||||||
invalid = True
|
|
||||||
|
|
||||||
return invalid, items
|
|
||||||
|
|
||||||
|
|
||||||
def _trim_items(items: list[str]) -> list[str]:
|
def _trim_items(items: list[str]) -> list[str]:
|
||||||
|
|||||||
@@ -24,9 +24,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"options": {
|
"options": {
|
||||||
"error": {
|
|
||||||
"invalid_known_hosts": "[%key:component::cast::config::error::invalid_known_hosts%]"
|
|
||||||
},
|
|
||||||
"step": {
|
"step": {
|
||||||
"init": {
|
"init": {
|
||||||
"data": {
|
"data": {
|
||||||
|
|||||||
Reference in New Issue
Block a user