diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index 78d2305c4e2..bb884d6392f 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -221,6 +221,8 @@ from .const import ( CONF_SPEED_RANGE_MIN, CONF_STATE_CLOSED, CONF_STATE_CLOSING, + CONF_STATE_OFF, + CONF_STATE_ON, CONF_STATE_OPEN, CONF_STATE_OPENING, CONF_STATE_STOPPED, @@ -1330,6 +1332,24 @@ PLATFORM_MQTT_FIELDS: dict[str, dict[str, PlatformField]] = { validator=validate(cv.template), error="invalid_template", ), + CONF_PAYLOAD_OFF: PlatformField( + selector=TEXT_SELECTOR, + required=False, + default=DEFAULT_PAYLOAD_OFF, + ), + CONF_PAYLOAD_ON: PlatformField( + selector=TEXT_SELECTOR, + required=False, + default=DEFAULT_PAYLOAD_ON, + ), + CONF_STATE_OFF: PlatformField( + selector=TEXT_SELECTOR, + required=False, + ), + CONF_STATE_ON: PlatformField( + selector=TEXT_SELECTOR, + required=False, + ), CONF_RETAIN: PlatformField(selector=BOOLEAN_SELECTOR, required=False), CONF_OPTIMISTIC: PlatformField(selector=BOOLEAN_SELECTOR, required=False), }, diff --git a/homeassistant/components/mqtt/const.py b/homeassistant/components/mqtt/const.py index 7c0ac1f2a3f..c60aa674b1b 100644 --- a/homeassistant/components/mqtt/const.py +++ b/homeassistant/components/mqtt/const.py @@ -159,6 +159,8 @@ CONF_SPEED_RANGE_MAX = "speed_range_max" CONF_SPEED_RANGE_MIN = "speed_range_min" CONF_STATE_CLOSED = "state_closed" CONF_STATE_CLOSING = "state_closing" +CONF_STATE_OFF = "state_off" +CONF_STATE_ON = "state_on" CONF_STATE_OPEN = "state_open" CONF_STATE_OPENING = "state_opening" CONF_STATE_STOPPED = "state_stopped" diff --git a/homeassistant/components/mqtt/strings.json b/homeassistant/components/mqtt/strings.json index 5e4c2612592..281c5a34a45 100644 --- a/homeassistant/components/mqtt/strings.json +++ b/homeassistant/components/mqtt/strings.json @@ -270,6 +270,8 @@ "qos": "QoS", "red_template": "Red template", "retain": "Retain", + "state_off": "State \"off\"", + "state_on": "State \"on\"", "state_template": "State template", "state_topic": "State topic", "state_value_template": "State value template", @@ -295,6 +297,8 @@ "qos": "The QoS value a {platform} entity should use.", "red_template": "[Template](https://www.home-assistant.io/docs/configuration/templating/#using-value-templates-with-mqtt) to extract red color from the state payload value. Expected result of the template is an integer from 0-255 range.", "retain": "Select if values published by the {platform} entity should be retained at the MQTT broker.", + "state_off": "The incoming payload that represents the \"off\" state. Use only when the value that represents \"off\" state in the state topic is different from value that should be sent to the command topic to turn the device off.", + "state_on": "The incoming payload that represents the \"on\" state. Use only when the value that represents \"on\" state in the state topic is different from value that should be sent to the command topic to turn the device on.", "state_template": "[Template](https://www.home-assistant.io/docs/configuration/templating/#using-value-templates-with-mqtt) to extract state from the state payload value.", "state_topic": "The MQTT topic subscribed to receive {platform} state values. [Learn more.]({url}#state_topic)", "supported_color_modes": "A list of color modes supported by the light. Possible color modes are On/Off, Brightness, Color temperature, HS, XY, RGB, RGBW, RGBWW, White. Note that if On/Off or Brightness are used, that must be the only value in the list. [Learn more.]({url}#supported_color_modes)", diff --git a/homeassistant/components/mqtt/switch.py b/homeassistant/components/mqtt/switch.py index f6996fc77ce..fa33751f37d 100644 --- a/homeassistant/components/mqtt/switch.py +++ b/homeassistant/components/mqtt/switch.py @@ -31,7 +31,11 @@ from .config import MQTT_RW_SCHEMA from .const import ( CONF_COMMAND_TEMPLATE, CONF_COMMAND_TOPIC, + CONF_STATE_OFF, + CONF_STATE_ON, CONF_STATE_TOPIC, + DEFAULT_PAYLOAD_OFF, + DEFAULT_PAYLOAD_ON, PAYLOAD_NONE, ) from .entity import MqttEntity, async_setup_entity_entry_helper @@ -46,10 +50,6 @@ from .schemas import MQTT_ENTITY_COMMON_SCHEMA PARALLEL_UPDATES = 0 DEFAULT_NAME = "MQTT Switch" -DEFAULT_PAYLOAD_ON = "ON" -DEFAULT_PAYLOAD_OFF = "OFF" -CONF_STATE_ON = "state_on" -CONF_STATE_OFF = "state_off" PLATFORM_SCHEMA_MODERN = MQTT_RW_SCHEMA.extend( { diff --git a/tests/components/mqtt/common.py b/tests/components/mqtt/common.py index ab5ffe28518..b985a8caffe 100644 --- a/tests/components/mqtt/common.py +++ b/tests/components/mqtt/common.py @@ -234,6 +234,8 @@ MOCK_SUBENTRY_SWITCH_COMPONENT = { "state_topic": "test-topic", "command_template": "{{ value }}", "value_template": "{{ value_json.value }}", + "payload_off": "OFF", + "payload_on": "ON", "entity_picture": "https://example.com/3faf1318016c46c5aea26707eeb6f12e", "optimistic": True, },