Add payload ON and OFF options to MQTT switch subentry component (#144627)

* Add payload ON and OFF options to MQTT switch component

* Add `state_on` and `state_off` options
This commit is contained in:
Jan Bouwhuis 2025-05-26 11:12:42 +02:00 committed by GitHub
parent d975135a7c
commit 301d308d5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 32 additions and 4 deletions

View File

@ -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),
},

View File

@ -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"

View File

@ -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)",

View File

@ -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(
{

View File

@ -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,
},