From b2cad90fee1e622aec01b1da7ad96911b8c68f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Mart=C3=ADn?= Date: Wed, 6 Nov 2024 01:56:51 +0100 Subject: [PATCH] feat(MQTT): Add `enable`, `disable` and `enable_on_boot` (#4408) --- components/mqtt.rst | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/components/mqtt.rst b/components/mqtt.rst index 693026225..9656d41a9 100644 --- a/components/mqtt.rst +++ b/components/mqtt.rst @@ -34,6 +34,7 @@ Configuration variables: ------------------------ - **broker** (**Required**, string): The host of your MQTT broker. +- **enable_on_boot** (*Optional*, boolean): If enabled, MQTT will be enabled on boot. Defaults to ``true``. - **port** (*Optional*, int): The port to connect to. Defaults to 1883. - **username** (*Optional*, string): The username to use for authentication. Empty (the default) means no authentication. @@ -733,6 +734,57 @@ Configuration options: root["something"] = id(my_sensor).state; }); +``mqtt.disable`` Action +----------------------- + +This action turns off the MQTT component on demand. + +.. code-block:: yaml + + on_...: + then: + - mqtt.disable: + +.. note:: + + The configuration option ``enable_on_boot`` can be set to ``false`` if you do not want MQTT to be enabled on boot. + + +``mqtt.enable`` Action +---------------------- + +This action turns on the MQTT component on demand. + +.. code-block:: yaml + + on_...: + then: + - mqtt.enable: + +.. note:: + + The configuration option ``enable_on_boot`` can be set to ``false`` if you do not want MQTT to be enabled on boot. + ``mqtt.enable`` can be useful for custom setups. For example, if the broker name is negotiated dynamically and saved in a global variable. + +.. code-block:: yaml + + mqtt: + id: mqtt_id + broker: "" + enable_on_boot: False + + globals: + - id: broker_address + type: std::string + restore_value: yes + max_restore_data_length: 24 + initial_value: '"192.168.1.2"' + + on_...: + then: + - lambda: !lambda id(mqtt_id).set_broker_address(id(broker_address)); + - mqtt.enable: + .. _mqtt-connected_condition: ``mqtt.connected`` Condition