Only expose MQTT advanced settings in advanced mode (#91996)
This commit is contained in:
parent
0594fefb0c
commit
aab7dffdb9
2 changed files with 22 additions and 13 deletions
|
@ -12,9 +12,9 @@ from cryptography.hazmat.primitives.serialization import load_pem_private_key
|
||||||
from cryptography.x509 import load_pem_x509_certificate
|
from cryptography.x509 import load_pem_x509_certificate
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
|
||||||
from homeassistant.components.file_upload import process_uploaded_file
|
from homeassistant.components.file_upload import process_uploaded_file
|
||||||
from homeassistant.components.hassio import HassioServiceInfo
|
from homeassistant.components.hassio import HassioServiceInfo
|
||||||
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_CLIENT_ID,
|
CONF_CLIENT_ID,
|
||||||
CONF_DISCOVERY,
|
CONF_DISCOVERY,
|
||||||
|
@ -25,7 +25,7 @@ from homeassistant.const import (
|
||||||
CONF_PROTOCOL,
|
CONF_PROTOCOL,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.json import json_dumps
|
from homeassistant.helpers.json import json_dumps
|
||||||
|
@ -154,7 +154,7 @@ CERT_UPLOAD_SELECTOR = FileSelector(
|
||||||
KEY_UPLOAD_SELECTOR = FileSelector(FileSelectorConfig(accept=".key,application/pkcs8"))
|
KEY_UPLOAD_SELECTOR = FileSelector(FileSelectorConfig(accept=".key,application/pkcs8"))
|
||||||
|
|
||||||
|
|
||||||
class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
class FlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow."""
|
"""Handle a config flow."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
@ -164,7 +164,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(
|
def async_get_options_flow(
|
||||||
config_entry: config_entries.ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
) -> MQTTOptionsFlowHandler:
|
) -> MQTTOptionsFlowHandler:
|
||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return MQTTOptionsFlowHandler(config_entry)
|
return MQTTOptionsFlowHandler(config_entry)
|
||||||
|
@ -186,7 +186,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
fields: OrderedDict[Any, Any] = OrderedDict()
|
fields: OrderedDict[Any, Any] = OrderedDict()
|
||||||
validated_user_input: dict[str, Any] = {}
|
validated_user_input: dict[str, Any] = {}
|
||||||
if await async_get_broker_settings(
|
if await async_get_broker_settings(
|
||||||
self.hass,
|
self,
|
||||||
fields,
|
fields,
|
||||||
None,
|
None,
|
||||||
user_input,
|
user_input,
|
||||||
|
@ -255,10 +255,10 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
class MQTTOptionsFlowHandler(OptionsFlow):
|
||||||
"""Handle MQTT options."""
|
"""Handle MQTT options."""
|
||||||
|
|
||||||
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
def __init__(self, config_entry: ConfigEntry) -> None:
|
||||||
"""Initialize MQTT options flow."""
|
"""Initialize MQTT options flow."""
|
||||||
self.config_entry = config_entry
|
self.config_entry = config_entry
|
||||||
self.broker_config: dict[str, str | int] = {}
|
self.broker_config: dict[str, str | int] = {}
|
||||||
|
@ -276,7 +276,7 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
||||||
fields: OrderedDict[Any, Any] = OrderedDict()
|
fields: OrderedDict[Any, Any] = OrderedDict()
|
||||||
validated_user_input: dict[str, Any] = {}
|
validated_user_input: dict[str, Any] = {}
|
||||||
if await async_get_broker_settings(
|
if await async_get_broker_settings(
|
||||||
self.hass,
|
self,
|
||||||
fields,
|
fields,
|
||||||
self.config_entry.data,
|
self.config_entry.data,
|
||||||
user_input,
|
user_input,
|
||||||
|
@ -448,7 +448,7 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
||||||
|
|
||||||
|
|
||||||
async def async_get_broker_settings(
|
async def async_get_broker_settings(
|
||||||
hass: HomeAssistant,
|
flow: ConfigFlow | OptionsFlow,
|
||||||
fields: OrderedDict[Any, Any],
|
fields: OrderedDict[Any, Any],
|
||||||
entry_config: MappingProxyType[str, Any] | None,
|
entry_config: MappingProxyType[str, Any] | None,
|
||||||
user_input: dict[str, Any] | None,
|
user_input: dict[str, Any] | None,
|
||||||
|
@ -461,6 +461,7 @@ async def async_get_broker_settings(
|
||||||
or when the advanced_broker_options checkbox was selected.
|
or when the advanced_broker_options checkbox was selected.
|
||||||
Returns True when settings are collected successfully.
|
Returns True when settings are collected successfully.
|
||||||
"""
|
"""
|
||||||
|
hass = flow.hass
|
||||||
advanced_broker_options: bool = False
|
advanced_broker_options: bool = False
|
||||||
user_input_basic: dict[str, Any] = {}
|
user_input_basic: dict[str, Any] = {}
|
||||||
current_config: dict[str, Any] = (
|
current_config: dict[str, Any] = (
|
||||||
|
@ -639,9 +640,12 @@ async def async_get_broker_settings(
|
||||||
description={"suggested_value": current_pass},
|
description={"suggested_value": current_pass},
|
||||||
)
|
)
|
||||||
] = PASSWORD_SELECTOR
|
] = PASSWORD_SELECTOR
|
||||||
# show advanced options checkbox if requested
|
# show advanced options checkbox if requested and
|
||||||
|
# advanced options are enabled
|
||||||
# or when the defaults of advanced options are overridden
|
# or when the defaults of advanced options are overridden
|
||||||
if not advanced_broker_options:
|
if not advanced_broker_options:
|
||||||
|
if not flow.show_advanced_options:
|
||||||
|
return False
|
||||||
fields[
|
fields[
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
ADVANCED_OPTIONS,
|
ADVANCED_OPTIONS,
|
||||||
|
|
|
@ -208,7 +208,8 @@ async def test_user_v5_connection_works(
|
||||||
mock_try_connection.return_value = True
|
mock_try_connection.return_value = True
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
"mqtt", context={"source": config_entries.SOURCE_USER}
|
"mqtt",
|
||||||
|
context={"source": config_entries.SOURCE_USER, "show_advanced_options": True},
|
||||||
)
|
)
|
||||||
assert result["type"] == "form"
|
assert result["type"] == "form"
|
||||||
|
|
||||||
|
@ -1015,7 +1016,9 @@ async def test_skipping_advanced_options(
|
||||||
|
|
||||||
mqtt_mock.async_connect.reset_mock()
|
mqtt_mock.async_connect.reset_mock()
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(
|
||||||
|
config_entry.entry_id, context={"show_advanced_options": True}
|
||||||
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
assert result["step_id"] == "broker"
|
assert result["step_id"] == "broker"
|
||||||
|
|
||||||
|
@ -1268,7 +1271,9 @@ async def test_setup_with_advanced_settings(
|
||||||
|
|
||||||
mock_try_connection.return_value = True
|
mock_try_connection.return_value = True
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(
|
||||||
|
config_entry.entry_id, context={"show_advanced_options": True}
|
||||||
|
)
|
||||||
assert result["type"] == "form"
|
assert result["type"] == "form"
|
||||||
assert result["step_id"] == "broker"
|
assert result["step_id"] == "broker"
|
||||||
assert result["data_schema"].schema["advanced_options"]
|
assert result["data_schema"].schema["advanced_options"]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue