2018-09-14 09:57:31 +00:00
|
|
|
"""Config flow for MQTT."""
|
2022-06-13 11:35:50 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-09-14 09:57:31 +00:00
|
|
|
from collections import OrderedDict
|
2022-10-07 08:12:19 +00:00
|
|
|
from collections.abc import Callable
|
2018-09-14 09:57:31 +00:00
|
|
|
import queue
|
2022-10-07 08:12:19 +00:00
|
|
|
from types import MappingProxyType
|
2022-06-30 19:11:45 +00:00
|
|
|
from typing import Any
|
2018-09-14 09:57:31 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2021-12-03 13:05:56 +00:00
|
|
|
from homeassistant.components.hassio import HassioServiceInfo
|
2018-10-01 12:11:21 +00:00
|
|
|
from homeassistant.const import (
|
2021-02-11 12:38:33 +00:00
|
|
|
CONF_DISCOVERY,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
2020-07-15 18:16:03 +00:00
|
|
|
CONF_PAYLOAD,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2022-10-07 08:12:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-12-03 13:05:56 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2022-10-11 08:51:35 +00:00
|
|
|
from homeassistant.helpers.selector import (
|
|
|
|
BooleanSelector,
|
|
|
|
NumberSelector,
|
|
|
|
NumberSelectorConfig,
|
|
|
|
NumberSelectorMode,
|
|
|
|
TextSelector,
|
|
|
|
TextSelectorConfig,
|
|
|
|
TextSelectorType,
|
|
|
|
)
|
2022-09-28 12:13:44 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2018-09-14 09:57:31 +00:00
|
|
|
|
2022-05-31 07:32:44 +00:00
|
|
|
from .client import MqttClientSetup
|
2020-06-23 00:49:01 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_PAYLOAD,
|
|
|
|
ATTR_QOS,
|
|
|
|
ATTR_RETAIN,
|
|
|
|
ATTR_TOPIC,
|
|
|
|
CONF_BIRTH_MESSAGE,
|
|
|
|
CONF_BROKER,
|
|
|
|
CONF_WILL_MESSAGE,
|
2020-07-15 18:16:03 +00:00
|
|
|
DEFAULT_BIRTH,
|
2020-06-23 00:49:01 +00:00
|
|
|
DEFAULT_DISCOVERY,
|
2022-10-07 08:12:19 +00:00
|
|
|
DEFAULT_PORT,
|
2020-07-15 18:16:03 +00:00
|
|
|
DEFAULT_WILL,
|
2021-03-20 23:34:46 +00:00
|
|
|
DOMAIN,
|
2020-06-23 00:49:01 +00:00
|
|
|
)
|
2022-09-20 17:40:06 +00:00
|
|
|
from .util import MQTT_WILL_BIRTH_SCHEMA, get_mqtt_data
|
2020-06-23 00:49:01 +00:00
|
|
|
|
2022-02-23 11:29:32 +00:00
|
|
|
MQTT_TIMEOUT = 5
|
|
|
|
|
2022-10-11 08:51:35 +00:00
|
|
|
BOOLEAN_SELECTOR = BooleanSelector()
|
|
|
|
TEXT_SELECTOR = TextSelector(TextSelectorConfig(type=TextSelectorType.TEXT))
|
|
|
|
PUBLISH_TOPIC_SELECTOR = TextSelector(TextSelectorConfig(type=TextSelectorType.TEXT))
|
|
|
|
PORT_SELECTOR = vol.All(
|
|
|
|
NumberSelector(NumberSelectorConfig(mode=NumberSelectorMode.BOX, min=1, max=65535)),
|
|
|
|
vol.Coerce(int),
|
|
|
|
)
|
|
|
|
PASSWORD_SELECTOR = TextSelector(TextSelectorConfig(type=TextSelectorType.PASSWORD))
|
|
|
|
QOS_SELECTOR = vol.All(
|
|
|
|
NumberSelector(NumberSelectorConfig(mode=NumberSelectorMode.BOX, min=0, max=2)),
|
|
|
|
vol.Coerce(int),
|
|
|
|
)
|
|
|
|
|
2018-09-14 09:57:31 +00:00
|
|
|
|
2021-04-30 21:28:25 +00:00
|
|
|
class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
2018-09-14 09:57:31 +00:00
|
|
|
"""Handle a config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2018-10-01 12:11:21 +00:00
|
|
|
_hassio_discovery = None
|
|
|
|
|
2020-06-23 00:49:01 +00:00
|
|
|
@staticmethod
|
2022-06-13 11:35:50 +00:00
|
|
|
@callback
|
|
|
|
def async_get_options_flow(
|
|
|
|
config_entry: config_entries.ConfigEntry,
|
|
|
|
) -> MQTTOptionsFlowHandler:
|
2020-06-23 00:49:01 +00:00
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return MQTTOptionsFlowHandler(config_entry)
|
|
|
|
|
2022-10-09 12:41:30 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2018-09-14 09:57:31 +00:00
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
if self._async_current_entries():
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2018-09-14 09:57:31 +00:00
|
|
|
|
|
|
|
return await self.async_step_broker()
|
|
|
|
|
2022-06-30 19:11:45 +00:00
|
|
|
async def async_step_broker(
|
2022-10-09 12:41:30 +00:00
|
|
|
self, user_input: dict[str, Any] | None = None
|
2022-06-30 19:11:45 +00:00
|
|
|
) -> FlowResult:
|
2018-09-18 12:59:39 +00:00
|
|
|
"""Confirm the setup."""
|
2022-10-07 08:12:19 +00:00
|
|
|
yaml_config: ConfigType = get_mqtt_data(self.hass, True).config or {}
|
|
|
|
errors: dict[str, str] = {}
|
|
|
|
fields: OrderedDict[Any, Any] = OrderedDict()
|
2022-10-09 12:41:30 +00:00
|
|
|
validated_user_input: dict[str, Any] = {}
|
2022-10-07 08:12:19 +00:00
|
|
|
if await async_get_broker_settings(
|
|
|
|
self.hass,
|
|
|
|
fields,
|
|
|
|
yaml_config,
|
|
|
|
None,
|
|
|
|
user_input,
|
|
|
|
validated_user_input,
|
|
|
|
errors,
|
|
|
|
):
|
2022-10-09 12:41:30 +00:00
|
|
|
test_config: dict[str, Any] = yaml_config.copy()
|
2022-10-07 08:12:19 +00:00
|
|
|
test_config.update(validated_user_input)
|
2018-09-14 09:57:31 +00:00
|
|
|
can_connect = await self.hass.async_add_executor_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
try_connection,
|
2022-10-07 08:12:19 +00:00
|
|
|
test_config,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-14 09:57:31 +00:00
|
|
|
|
|
|
|
if can_connect:
|
2022-10-07 08:12:19 +00:00
|
|
|
validated_user_input[CONF_DISCOVERY] = DEFAULT_DISCOVERY
|
2018-09-14 09:57:31 +00:00
|
|
|
return self.async_create_entry(
|
2022-10-07 08:12:19 +00:00
|
|
|
title=validated_user_input[CONF_BROKER],
|
|
|
|
data=validated_user_input,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-14 09:57:31 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
2018-09-14 09:57:31 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="broker", data_schema=vol.Schema(fields), errors=errors
|
|
|
|
)
|
2018-09-14 09:57:31 +00:00
|
|
|
|
2021-12-03 13:05:56 +00:00
|
|
|
async def async_step_hassio(self, discovery_info: HassioServiceInfo) -> FlowResult:
|
2018-10-01 12:11:21 +00:00
|
|
|
"""Receive a Hass.io discovery."""
|
2021-08-26 09:14:42 +00:00
|
|
|
await self._async_handle_discovery_without_unique_id()
|
2018-10-01 12:11:21 +00:00
|
|
|
|
2021-12-03 13:05:56 +00:00
|
|
|
self._hassio_discovery = discovery_info.config
|
2018-10-01 12:11:21 +00:00
|
|
|
|
|
|
|
return await self.async_step_hassio_confirm()
|
|
|
|
|
2022-06-30 19:11:45 +00:00
|
|
|
async def async_step_hassio_confirm(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2018-10-01 12:11:21 +00:00
|
|
|
"""Confirm a Hass.io discovery."""
|
2022-10-07 08:12:19 +00:00
|
|
|
errors: dict[str, str] = {}
|
2022-06-30 19:11:45 +00:00
|
|
|
assert self._hassio_discovery
|
2018-10-01 12:11:21 +00:00
|
|
|
|
|
|
|
if user_input is not None:
|
2022-10-09 12:41:30 +00:00
|
|
|
data: dict[str, Any] = self._hassio_discovery.copy()
|
2022-10-07 08:12:19 +00:00
|
|
|
data[CONF_BROKER] = data.pop(CONF_HOST)
|
2018-10-01 12:11:21 +00:00
|
|
|
can_connect = await self.hass.async_add_executor_job(
|
|
|
|
try_connection,
|
2022-10-07 08:12:19 +00:00
|
|
|
data,
|
2018-10-01 12:11:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if can_connect:
|
|
|
|
return self.async_create_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
title=data["addon"],
|
|
|
|
data={
|
2022-10-07 08:12:19 +00:00
|
|
|
CONF_BROKER: data[CONF_BROKER],
|
2018-10-01 12:11:21 +00:00
|
|
|
CONF_PORT: data[CONF_PORT],
|
|
|
|
CONF_USERNAME: data.get(CONF_USERNAME),
|
|
|
|
CONF_PASSWORD: data.get(CONF_PASSWORD),
|
2021-05-06 04:25:28 +00:00
|
|
|
CONF_DISCOVERY: DEFAULT_DISCOVERY,
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2018-09-14 09:57:31 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
2018-10-01 12:11:21 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="hassio_confirm",
|
|
|
|
description_placeholders={"addon": self._hassio_discovery["addon"]},
|
2018-10-01 12:11:21 +00:00
|
|
|
errors=errors,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-23 00:49:01 +00:00
|
|
|
class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
"""Handle MQTT options."""
|
|
|
|
|
2022-06-13 11:35:50 +00:00
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
2020-06-23 00:49:01 +00:00
|
|
|
"""Initialize MQTT options flow."""
|
|
|
|
self.config_entry = config_entry
|
2022-06-13 11:35:50 +00:00
|
|
|
self.broker_config: dict[str, str | int] = {}
|
2020-06-23 00:49:01 +00:00
|
|
|
self.options = dict(config_entry.options)
|
|
|
|
|
2022-06-30 19:11:45 +00:00
|
|
|
async def async_step_init(self, user_input: None = None) -> FlowResult:
|
2020-06-23 00:49:01 +00:00
|
|
|
"""Manage the MQTT options."""
|
|
|
|
return await self.async_step_broker()
|
|
|
|
|
2022-06-30 19:11:45 +00:00
|
|
|
async def async_step_broker(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2021-04-23 18:02:12 +00:00
|
|
|
"""Manage the MQTT broker configuration."""
|
2022-10-07 08:12:19 +00:00
|
|
|
errors: dict[str, str] = {}
|
|
|
|
yaml_config: ConfigType = get_mqtt_data(self.hass, True).config or {}
|
|
|
|
fields: OrderedDict[Any, Any] = OrderedDict()
|
2022-10-09 12:41:30 +00:00
|
|
|
validated_user_input: dict[str, Any] = {}
|
2022-10-07 08:12:19 +00:00
|
|
|
if await async_get_broker_settings(
|
|
|
|
self.hass,
|
|
|
|
fields,
|
|
|
|
yaml_config,
|
|
|
|
self.config_entry.data,
|
|
|
|
user_input,
|
|
|
|
validated_user_input,
|
|
|
|
errors,
|
|
|
|
):
|
2022-10-09 12:41:30 +00:00
|
|
|
test_config: dict[str, Any] = yaml_config.copy()
|
2022-10-07 08:12:19 +00:00
|
|
|
test_config.update(validated_user_input)
|
2020-06-23 00:49:01 +00:00
|
|
|
can_connect = await self.hass.async_add_executor_job(
|
|
|
|
try_connection,
|
2022-10-07 08:12:19 +00:00
|
|
|
test_config,
|
2020-06-23 00:49:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if can_connect:
|
2022-10-07 08:12:19 +00:00
|
|
|
self.broker_config.update(validated_user_input)
|
2020-06-23 00:49:01 +00:00
|
|
|
return await self.async_step_options()
|
|
|
|
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
|
|
|
|
return self.async_show_form(
|
2020-08-27 11:56:20 +00:00
|
|
|
step_id="broker",
|
|
|
|
data_schema=vol.Schema(fields),
|
|
|
|
errors=errors,
|
2021-04-23 18:02:12 +00:00
|
|
|
last_step=False,
|
2020-06-23 00:49:01 +00:00
|
|
|
)
|
|
|
|
|
2022-06-30 19:11:45 +00:00
|
|
|
async def async_step_options(
|
2022-10-09 12:41:30 +00:00
|
|
|
self, user_input: dict[str, Any] | None = None
|
2022-06-30 19:11:45 +00:00
|
|
|
) -> FlowResult:
|
2020-06-23 00:49:01 +00:00
|
|
|
"""Manage the MQTT options."""
|
|
|
|
errors = {}
|
|
|
|
current_config = self.config_entry.data
|
2022-10-07 08:12:19 +00:00
|
|
|
yaml_config = get_mqtt_data(self.hass, True).config or {}
|
2022-10-09 12:41:30 +00:00
|
|
|
options_config: dict[str, Any] = {}
|
2022-10-07 08:12:19 +00:00
|
|
|
bad_input: bool = False
|
|
|
|
|
|
|
|
def _birth_will(birt_or_will: str) -> dict:
|
|
|
|
"""Return the user input for birth or will."""
|
|
|
|
assert user_input
|
|
|
|
return {
|
|
|
|
ATTR_TOPIC: user_input[f"{birt_or_will}_topic"],
|
|
|
|
ATTR_PAYLOAD: user_input.get(f"{birt_or_will}_payload", ""),
|
|
|
|
ATTR_QOS: user_input[f"{birt_or_will}_qos"],
|
|
|
|
ATTR_RETAIN: user_input[f"{birt_or_will}_retain"],
|
|
|
|
}
|
|
|
|
|
|
|
|
def _validate(
|
2022-10-09 12:41:30 +00:00
|
|
|
field: str, values: dict[str, Any], error_code: str, schema: Callable
|
2022-10-07 08:12:19 +00:00
|
|
|
):
|
|
|
|
"""Validate the user input."""
|
|
|
|
nonlocal bad_input
|
|
|
|
try:
|
|
|
|
option_values = schema(values)
|
|
|
|
options_config[field] = option_values
|
|
|
|
except vol.Invalid:
|
|
|
|
errors["base"] = error_code
|
|
|
|
bad_input = True
|
2020-06-23 00:49:01 +00:00
|
|
|
|
2022-10-07 08:12:19 +00:00
|
|
|
if user_input is not None:
|
|
|
|
# validate input
|
|
|
|
options_config[CONF_DISCOVERY] = user_input[CONF_DISCOVERY]
|
2020-06-23 00:49:01 +00:00
|
|
|
if "birth_topic" in user_input:
|
2022-10-07 08:12:19 +00:00
|
|
|
_validate(
|
|
|
|
CONF_BIRTH_MESSAGE,
|
|
|
|
_birth_will("birth"),
|
|
|
|
"bad_birth",
|
|
|
|
MQTT_WILL_BIRTH_SCHEMA,
|
|
|
|
)
|
2020-07-15 18:16:03 +00:00
|
|
|
if not user_input["birth_enable"]:
|
|
|
|
options_config[CONF_BIRTH_MESSAGE] = {}
|
2020-06-23 00:49:01 +00:00
|
|
|
|
|
|
|
if "will_topic" in user_input:
|
2022-10-07 08:12:19 +00:00
|
|
|
_validate(
|
|
|
|
CONF_WILL_MESSAGE,
|
|
|
|
_birth_will("will"),
|
|
|
|
"bad_will",
|
|
|
|
MQTT_WILL_BIRTH_SCHEMA,
|
|
|
|
)
|
2020-07-15 18:16:03 +00:00
|
|
|
if not user_input["will_enable"]:
|
|
|
|
options_config[CONF_WILL_MESSAGE] = {}
|
2020-06-23 00:49:01 +00:00
|
|
|
|
2022-10-07 08:12:19 +00:00
|
|
|
if not bad_input:
|
2020-06-23 00:49:01 +00:00
|
|
|
updated_config = {}
|
|
|
|
updated_config.update(self.broker_config)
|
|
|
|
updated_config.update(options_config)
|
|
|
|
self.hass.config_entries.async_update_entry(
|
2022-09-27 06:05:28 +00:00
|
|
|
self.config_entry,
|
|
|
|
data=updated_config,
|
|
|
|
title=str(self.broker_config[CONF_BROKER]),
|
2020-06-23 00:49:01 +00:00
|
|
|
)
|
2022-06-30 19:11:45 +00:00
|
|
|
return self.async_create_entry(title="", data={})
|
2020-06-23 00:49:01 +00:00
|
|
|
|
2020-09-19 20:10:01 +00:00
|
|
|
birth = {
|
|
|
|
**DEFAULT_BIRTH,
|
|
|
|
**current_config.get(
|
|
|
|
CONF_BIRTH_MESSAGE, yaml_config.get(CONF_BIRTH_MESSAGE, {})
|
|
|
|
),
|
|
|
|
}
|
|
|
|
will = {
|
|
|
|
**DEFAULT_WILL,
|
|
|
|
**current_config.get(
|
|
|
|
CONF_WILL_MESSAGE, yaml_config.get(CONF_WILL_MESSAGE, {})
|
|
|
|
),
|
|
|
|
}
|
|
|
|
discovery = current_config.get(
|
|
|
|
CONF_DISCOVERY, yaml_config.get(CONF_DISCOVERY, DEFAULT_DISCOVERY)
|
|
|
|
)
|
2020-06-23 00:49:01 +00:00
|
|
|
|
2022-10-07 08:12:19 +00:00
|
|
|
# build form
|
2022-06-30 19:11:45 +00:00
|
|
|
fields: OrderedDict[vol.Marker, Any] = OrderedDict()
|
2022-10-11 08:51:35 +00:00
|
|
|
fields[vol.Optional(CONF_DISCOVERY, default=discovery)] = BOOLEAN_SELECTOR
|
2020-07-15 18:16:03 +00:00
|
|
|
|
|
|
|
# Birth message is disabled if CONF_BIRTH_MESSAGE = {}
|
|
|
|
fields[
|
|
|
|
vol.Optional(
|
|
|
|
"birth_enable",
|
|
|
|
default=CONF_BIRTH_MESSAGE not in current_config
|
|
|
|
or current_config[CONF_BIRTH_MESSAGE] != {},
|
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = BOOLEAN_SELECTOR
|
2020-06-23 00:49:01 +00:00
|
|
|
fields[
|
2020-07-15 18:16:03 +00:00
|
|
|
vol.Optional(
|
|
|
|
"birth_topic", description={"suggested_value": birth[ATTR_TOPIC]}
|
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = PUBLISH_TOPIC_SELECTOR
|
2020-06-23 00:49:01 +00:00
|
|
|
fields[
|
|
|
|
vol.Optional(
|
2020-07-15 18:16:03 +00:00
|
|
|
"birth_payload", description={"suggested_value": birth[CONF_PAYLOAD]}
|
2020-06-23 00:49:01 +00:00
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = TEXT_SELECTOR
|
|
|
|
fields[vol.Optional("birth_qos", default=birth[ATTR_QOS])] = QOS_SELECTOR
|
|
|
|
fields[
|
|
|
|
vol.Optional("birth_retain", default=birth[ATTR_RETAIN])
|
|
|
|
] = BOOLEAN_SELECTOR
|
2020-07-15 18:16:03 +00:00
|
|
|
|
|
|
|
# Will message is disabled if CONF_WILL_MESSAGE = {}
|
|
|
|
fields[
|
|
|
|
vol.Optional(
|
|
|
|
"will_enable",
|
|
|
|
default=CONF_WILL_MESSAGE not in current_config
|
|
|
|
or current_config[CONF_WILL_MESSAGE] != {},
|
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = BOOLEAN_SELECTOR
|
2020-06-23 00:49:01 +00:00
|
|
|
fields[
|
2020-07-15 18:16:03 +00:00
|
|
|
vol.Optional(
|
|
|
|
"will_topic", description={"suggested_value": will[ATTR_TOPIC]}
|
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = PUBLISH_TOPIC_SELECTOR
|
2020-06-23 00:49:01 +00:00
|
|
|
fields[
|
2020-07-15 18:16:03 +00:00
|
|
|
vol.Optional(
|
|
|
|
"will_payload", description={"suggested_value": will[CONF_PAYLOAD]}
|
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = TEXT_SELECTOR
|
|
|
|
fields[vol.Optional("will_qos", default=will[ATTR_QOS])] = QOS_SELECTOR
|
|
|
|
fields[
|
|
|
|
vol.Optional("will_retain", default=will[ATTR_RETAIN])
|
|
|
|
] = BOOLEAN_SELECTOR
|
2020-06-23 00:49:01 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2020-08-27 11:56:20 +00:00
|
|
|
step_id="options",
|
|
|
|
data_schema=vol.Schema(fields),
|
|
|
|
errors=errors,
|
2021-04-23 18:02:12 +00:00
|
|
|
last_step=True,
|
2020-06-23 00:49:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-07 08:12:19 +00:00
|
|
|
async def async_get_broker_settings(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
fields: OrderedDict[Any, Any],
|
2022-09-28 12:13:44 +00:00
|
|
|
yaml_config: ConfigType,
|
2022-10-07 08:12:19 +00:00
|
|
|
entry_config: MappingProxyType[str, Any] | None,
|
2022-10-09 12:41:30 +00:00
|
|
|
user_input: dict[str, Any] | None,
|
|
|
|
validated_user_input: dict[str, Any],
|
2022-10-07 08:12:19 +00:00
|
|
|
errors: dict[str, str],
|
|
|
|
) -> bool:
|
|
|
|
"""Build the config flow schema to collect the broker settings.
|
|
|
|
|
|
|
|
Returns True when settings are collected successfully.
|
|
|
|
"""
|
2022-10-09 12:41:30 +00:00
|
|
|
user_input_basic: dict[str, Any] = {}
|
|
|
|
current_config = entry_config.copy() if entry_config is not None else {}
|
2022-10-07 08:12:19 +00:00
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
validated_user_input.update(user_input)
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Update the current settings the the new posted data to fill the defaults
|
|
|
|
current_config.update(user_input_basic)
|
|
|
|
|
|
|
|
# Get default settings (if any)
|
|
|
|
current_broker = current_config.get(CONF_BROKER, yaml_config.get(CONF_BROKER))
|
|
|
|
current_port = current_config.get(
|
|
|
|
CONF_PORT, yaml_config.get(CONF_PORT, DEFAULT_PORT)
|
|
|
|
)
|
|
|
|
current_user = current_config.get(CONF_USERNAME, yaml_config.get(CONF_USERNAME))
|
|
|
|
current_pass = current_config.get(CONF_PASSWORD, yaml_config.get(CONF_PASSWORD))
|
|
|
|
|
|
|
|
# Build form
|
2022-10-11 08:51:35 +00:00
|
|
|
fields[vol.Required(CONF_BROKER, default=current_broker)] = TEXT_SELECTOR
|
|
|
|
fields[vol.Required(CONF_PORT, default=current_port)] = PORT_SELECTOR
|
2022-10-07 08:12:19 +00:00
|
|
|
fields[
|
|
|
|
vol.Optional(
|
|
|
|
CONF_USERNAME,
|
|
|
|
description={"suggested_value": current_user},
|
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = TEXT_SELECTOR
|
2022-10-07 08:12:19 +00:00
|
|
|
fields[
|
|
|
|
vol.Optional(
|
|
|
|
CONF_PASSWORD,
|
|
|
|
description={"suggested_value": current_pass},
|
|
|
|
)
|
2022-10-11 08:51:35 +00:00
|
|
|
] = PASSWORD_SELECTOR
|
2022-10-07 08:12:19 +00:00
|
|
|
|
|
|
|
# Show form
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def try_connection(
|
2022-10-09 12:41:30 +00:00
|
|
|
user_input: dict[str, Any],
|
2022-09-17 19:43:42 +00:00
|
|
|
) -> bool:
|
2018-09-14 09:57:31 +00:00
|
|
|
"""Test if we can connect to an MQTT broker."""
|
2022-03-10 09:32:49 +00:00
|
|
|
# We don't import on the top because some integrations
|
|
|
|
# should be able to optionally rely on MQTT.
|
|
|
|
import paho.mqtt.client as mqtt # pylint: disable=import-outside-toplevel
|
|
|
|
|
2022-10-07 08:12:19 +00:00
|
|
|
client = MqttClientSetup(user_input).client
|
2018-09-14 09:57:31 +00:00
|
|
|
|
2022-09-17 19:43:42 +00:00
|
|
|
result: queue.Queue[bool] = queue.Queue(maxsize=1)
|
2018-09-14 09:57:31 +00:00
|
|
|
|
|
|
|
def on_connect(client_, userdata, flags, result_code):
|
|
|
|
"""Handle connection result."""
|
2022-03-10 09:32:49 +00:00
|
|
|
result.put(result_code == mqtt.CONNACK_ACCEPTED)
|
2018-09-14 09:57:31 +00:00
|
|
|
|
|
|
|
client.on_connect = on_connect
|
|
|
|
|
2022-10-07 08:12:19 +00:00
|
|
|
client.connect_async(user_input[CONF_BROKER], user_input[CONF_PORT])
|
2018-09-14 09:57:31 +00:00
|
|
|
client.loop_start()
|
|
|
|
|
|
|
|
try:
|
2022-02-23 11:29:32 +00:00
|
|
|
return result.get(timeout=MQTT_TIMEOUT)
|
2018-09-14 09:57:31 +00:00
|
|
|
except queue.Empty:
|
|
|
|
return False
|
|
|
|
finally:
|
|
|
|
client.disconnect()
|
|
|
|
client.loop_stop()
|