From 53d6f4948e671073219ae51b318ffdc8a14b2d5e Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 15 Jul 2020 20:16:03 +0200 Subject: [PATCH] Adapt MQTT config flow to default birth and will (#37875) --- homeassistant/components/mqtt/__init__.py | 22 ++---- homeassistant/components/mqtt/config_flow.py | 74 +++++++++++--------- homeassistant/components/mqtt/const.py | 20 ++++++ 3 files changed, 66 insertions(+), 50 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 0a35c85d023..b88c536d6a3 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -56,9 +56,14 @@ from .const import ( CONF_RETAIN, CONF_STATE_TOPIC, CONF_WILL_MESSAGE, + DEFAULT_BIRTH, DEFAULT_DISCOVERY, + DEFAULT_PAYLOAD_AVAILABLE, + DEFAULT_PAYLOAD_NOT_AVAILABLE, + DEFAULT_PREFIX, DEFAULT_QOS, DEFAULT_RETAIN, + DEFAULT_WILL, MQTT_CONNECTED, MQTT_DISCONNECTED, PROTOCOL_311, @@ -110,10 +115,7 @@ PROTOCOL_31 = "3.1" DEFAULT_PORT = 1883 DEFAULT_KEEPALIVE = 60 DEFAULT_PROTOCOL = PROTOCOL_311 -DEFAULT_PREFIX = "homeassistant" DEFAULT_TLS_PROTOCOL = "auto" -DEFAULT_PAYLOAD_AVAILABLE = "online" -DEFAULT_PAYLOAD_NOT_AVAILABLE = "offline" ATTR_PAYLOAD_TEMPLATE = "payload_template" @@ -139,20 +141,6 @@ CLIENT_KEY_AUTH_MSG = ( "the MQTT broker configuration" ) -DEFAULT_BIRTH = { - ATTR_TOPIC: DEFAULT_PREFIX + "/status", - CONF_PAYLOAD: DEFAULT_PAYLOAD_AVAILABLE, - ATTR_QOS: DEFAULT_QOS, - ATTR_RETAIN: DEFAULT_RETAIN, -} - -DEFAULT_WILL = { - ATTR_TOPIC: DEFAULT_PREFIX + "/status", - CONF_PAYLOAD: DEFAULT_PAYLOAD_NOT_AVAILABLE, - ATTR_QOS: DEFAULT_QOS, - ATTR_RETAIN: DEFAULT_RETAIN, -} - MQTT_WILL_BIRTH_SCHEMA = vol.Schema( { vol.Inclusive(ATTR_TOPIC, "topic_payload"): valid_publish_topic, diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index 2f4feaed5e9..4a5847366f7 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -9,6 +9,7 @@ from homeassistant import config_entries from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, + CONF_PAYLOAD, CONF_PORT, CONF_PROTOCOL, CONF_USERNAME, @@ -23,9 +24,9 @@ from .const import ( CONF_BROKER, CONF_DISCOVERY, CONF_WILL_MESSAGE, + DEFAULT_BIRTH, DEFAULT_DISCOVERY, - DEFAULT_QOS, - DEFAULT_RETAIN, + DEFAULT_WILL, ) from .util import MQTT_WILL_BIRTH_SCHEMA @@ -220,6 +221,8 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow): except vol.Invalid: errors["base"] = "bad_birth" bad_birth = True + if not user_input["birth_enable"]: + options_config[CONF_BIRTH_MESSAGE] = {} if "will_topic" in user_input: will_message = { @@ -234,6 +237,8 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow): except vol.Invalid: errors["base"] = "bad_will" bad_will = True + if not user_input["will_enable"]: + options_config[CONF_WILL_MESSAGE] = {} options_config[CONF_DISCOVERY] = user_input[CONF_DISCOVERY] @@ -246,29 +251,8 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow): ) return self.async_create_entry(title="", data=None) - birth_topic = None - birth_payload = None - birth_qos = DEFAULT_QOS - birth_retain = DEFAULT_RETAIN - if CONF_BIRTH_MESSAGE in current_config: - birth_topic = current_config[CONF_BIRTH_MESSAGE][ATTR_TOPIC] - birth_payload = current_config[CONF_BIRTH_MESSAGE][ATTR_PAYLOAD] - birth_qos = current_config[CONF_BIRTH_MESSAGE].get(ATTR_QOS, DEFAULT_QOS) - birth_retain = current_config[CONF_BIRTH_MESSAGE].get( - ATTR_RETAIN, DEFAULT_RETAIN - ) - - will_topic = None - will_payload = None - will_qos = DEFAULT_QOS - will_retain = DEFAULT_RETAIN - if CONF_WILL_MESSAGE in current_config: - will_topic = current_config[CONF_WILL_MESSAGE][ATTR_TOPIC] - will_payload = current_config[CONF_WILL_MESSAGE][ATTR_PAYLOAD] - will_qos = current_config[CONF_WILL_MESSAGE].get(ATTR_QOS, DEFAULT_QOS) - will_retain = current_config[CONF_WILL_MESSAGE].get( - ATTR_RETAIN, DEFAULT_RETAIN - ) + birth = {**DEFAULT_BIRTH, **current_config.get(CONF_BIRTH_MESSAGE, {})} + will = {**DEFAULT_WILL, **current_config.get(CONF_WILL_MESSAGE, {})} fields = OrderedDict() fields[ @@ -277,24 +261,48 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow): default=current_config.get(CONF_DISCOVERY, DEFAULT_DISCOVERY), ) ] = bool + + # Birth message is disabled if CONF_BIRTH_MESSAGE = {} fields[ - vol.Optional("birth_topic", description={"suggested_value": birth_topic}) + vol.Optional( + "birth_enable", + default=CONF_BIRTH_MESSAGE not in current_config + or current_config[CONF_BIRTH_MESSAGE] != {}, + ) + ] = bool + fields[ + vol.Optional( + "birth_topic", description={"suggested_value": birth[ATTR_TOPIC]} + ) ] = str fields[ vol.Optional( - "birth_payload", description={"suggested_value": birth_payload} + "birth_payload", description={"suggested_value": birth[CONF_PAYLOAD]} ) ] = str - fields[vol.Optional("birth_qos", default=birth_qos)] = vol.In([0, 1, 2]) - fields[vol.Optional("birth_retain", default=birth_retain)] = bool + fields[vol.Optional("birth_qos", default=birth[ATTR_QOS])] = vol.In([0, 1, 2]) + fields[vol.Optional("birth_retain", default=birth[ATTR_RETAIN])] = bool + + # Will message is disabled if CONF_WILL_MESSAGE = {} fields[ - vol.Optional("will_topic", description={"suggested_value": will_topic}) + vol.Optional( + "will_enable", + default=CONF_WILL_MESSAGE not in current_config + or current_config[CONF_WILL_MESSAGE] != {}, + ) + ] = bool + fields[ + vol.Optional( + "will_topic", description={"suggested_value": will[ATTR_TOPIC]} + ) ] = str fields[ - vol.Optional("will_payload", description={"suggested_value": will_payload}) + vol.Optional( + "will_payload", description={"suggested_value": will[CONF_PAYLOAD]} + ) ] = str - fields[vol.Optional("will_qos", default=will_qos)] = vol.In([0, 1, 2]) - fields[vol.Optional("will_retain", default=will_retain)] = bool + fields[vol.Optional("will_qos", default=will[ATTR_QOS])] = vol.In([0, 1, 2]) + fields[vol.Optional("will_retain", default=will[ATTR_RETAIN])] = bool return self.async_show_form( step_id="options", data_schema=vol.Schema(fields), errors=errors, diff --git a/homeassistant/components/mqtt/const.py b/homeassistant/components/mqtt/const.py index 62d2643bc91..7ea6d9d348b 100644 --- a/homeassistant/components/mqtt/const.py +++ b/homeassistant/components/mqtt/const.py @@ -1,4 +1,6 @@ """Constants used by multiple MQTT modules.""" +from homeassistant.const import CONF_PAYLOAD + ATTR_DISCOVERY_HASH = "discovery_hash" ATTR_DISCOVERY_PAYLOAD = "discovery_payload" ATTR_DISCOVERY_TOPIC = "discovery_topic" @@ -15,10 +17,28 @@ CONF_RETAIN = ATTR_RETAIN CONF_STATE_TOPIC = "state_topic" CONF_WILL_MESSAGE = "will_message" +DEFAULT_PREFIX = "homeassistant" +DEFAULT_BIRTH_WILL_TOPIC = DEFAULT_PREFIX + "/status" DEFAULT_DISCOVERY = False DEFAULT_QOS = 0 +DEFAULT_PAYLOAD_AVAILABLE = "online" +DEFAULT_PAYLOAD_NOT_AVAILABLE = "offline" DEFAULT_RETAIN = False +DEFAULT_BIRTH = { + ATTR_TOPIC: DEFAULT_BIRTH_WILL_TOPIC, + CONF_PAYLOAD: DEFAULT_PAYLOAD_AVAILABLE, + ATTR_QOS: DEFAULT_QOS, + ATTR_RETAIN: DEFAULT_RETAIN, +} + +DEFAULT_WILL = { + ATTR_TOPIC: DEFAULT_BIRTH_WILL_TOPIC, + CONF_PAYLOAD: DEFAULT_PAYLOAD_NOT_AVAILABLE, + ATTR_QOS: DEFAULT_QOS, + ATTR_RETAIN: DEFAULT_RETAIN, +} + MQTT_CONNECTED = "mqtt_connected" MQTT_DISCONNECTED = "mqtt_disconnected"