Skip to content

Commit 8b6fb05

Browse files
authored
Add subentry support for MQTT siren device (home-assistant#154220)
1 parent 28405e2 commit 8b6fb05

File tree

6 files changed

+157
-9
lines changed

6 files changed

+157
-9
lines changed

homeassistant/components/mqtt/config_flow.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
CONF_ACTION_TOPIC,
151151
CONF_AVAILABILITY_TEMPLATE,
152152
CONF_AVAILABILITY_TOPIC,
153+
CONF_AVAILABLE_TONES,
153154
CONF_BIRTH_MESSAGE,
154155
CONF_BLUE_TEMPLATE,
155156
CONF_BRIGHTNESS_COMMAND_TEMPLATE,
@@ -307,6 +308,8 @@
307308
CONF_STATE_VALUE_TEMPLATE,
308309
CONF_STEP,
309310
CONF_SUGGESTED_DISPLAY_PRECISION,
311+
CONF_SUPPORT_DURATION,
312+
CONF_SUPPORT_VOLUME_SET,
310313
CONF_SUPPORTED_COLOR_MODES,
311314
CONF_SUPPORTED_FEATURES,
312315
CONF_SWING_HORIZONTAL_MODE_COMMAND_TEMPLATE,
@@ -460,6 +463,7 @@
460463
Platform.NUMBER,
461464
Platform.SELECT,
462465
Platform.SENSOR,
466+
Platform.SIREN,
463467
Platform.SWITCH,
464468
]
465469

@@ -1163,6 +1167,7 @@ def validate_sensor_platform_config(
11631167
Platform.NOTIFY.value: None,
11641168
Platform.NUMBER.value: validate_number_platform_config,
11651169
Platform.SELECT: None,
1170+
Platform.SIREN: None,
11661171
Platform.SENSOR.value: validate_sensor_platform_config,
11671172
Platform.SWITCH.value: None,
11681173
}
@@ -1419,6 +1424,7 @@ class PlatformField:
14191424
default=None,
14201425
),
14211426
},
1427+
Platform.SIREN: {},
14221428
Platform.SWITCH.value: {
14231429
CONF_DEVICE_CLASS: PlatformField(
14241430
selector=SWITCH_DEVICE_CLASS_SELECTOR, required=False
@@ -3181,6 +3187,71 @@ class PlatformField:
31813187
section="advanced_settings",
31823188
),
31833189
},
3190+
Platform.SIREN: {
3191+
CONF_COMMAND_TOPIC: PlatformField(
3192+
selector=TEXT_SELECTOR,
3193+
required=True,
3194+
validator=valid_publish_topic,
3195+
error="invalid_publish_topic",
3196+
),
3197+
CONF_COMMAND_TEMPLATE: PlatformField(
3198+
selector=TEMPLATE_SELECTOR,
3199+
required=False,
3200+
validator=validate(cv.template),
3201+
error="invalid_template",
3202+
),
3203+
CONF_STATE_TOPIC: PlatformField(
3204+
selector=TEXT_SELECTOR,
3205+
required=False,
3206+
validator=valid_subscribe_topic,
3207+
error="invalid_subscribe_topic",
3208+
),
3209+
CONF_VALUE_TEMPLATE: PlatformField(
3210+
selector=TEMPLATE_SELECTOR,
3211+
required=False,
3212+
validator=validate(cv.template),
3213+
error="invalid_template",
3214+
),
3215+
CONF_PAYLOAD_OFF: PlatformField(
3216+
selector=TEXT_SELECTOR,
3217+
required=False,
3218+
default=DEFAULT_PAYLOAD_OFF,
3219+
),
3220+
CONF_PAYLOAD_ON: PlatformField(
3221+
selector=TEXT_SELECTOR,
3222+
required=False,
3223+
default=DEFAULT_PAYLOAD_ON,
3224+
),
3225+
CONF_STATE_OFF: PlatformField(
3226+
selector=TEXT_SELECTOR,
3227+
required=False,
3228+
),
3229+
CONF_STATE_ON: PlatformField(
3230+
selector=TEXT_SELECTOR,
3231+
required=False,
3232+
),
3233+
CONF_AVAILABLE_TONES: PlatformField(
3234+
selector=OPTIONS_SELECTOR,
3235+
required=False,
3236+
),
3237+
CONF_SUPPORT_DURATION: PlatformField(
3238+
selector=BOOLEAN_SELECTOR,
3239+
required=False,
3240+
),
3241+
CONF_SUPPORT_VOLUME_SET: PlatformField(
3242+
selector=BOOLEAN_SELECTOR,
3243+
required=False,
3244+
),
3245+
CONF_RETAIN: PlatformField(selector=BOOLEAN_SELECTOR, required=False),
3246+
CONF_OPTIMISTIC: PlatformField(selector=BOOLEAN_SELECTOR, required=False),
3247+
CONF_COMMAND_OFF_TEMPLATE: PlatformField(
3248+
selector=TEMPLATE_SELECTOR,
3249+
required=False,
3250+
validator=validate(cv.template),
3251+
error="invalid_template",
3252+
section="siren_advanced_settings",
3253+
),
3254+
},
31843255
Platform.SWITCH.value: {
31853256
CONF_COMMAND_TOPIC: PlatformField(
31863257
selector=TEXT_SELECTOR,

homeassistant/components/mqtt/const.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
CONF_AVAILABILITY_MODE = "availability_mode"
3131
CONF_AVAILABILITY_TEMPLATE = "availability_template"
3232
CONF_AVAILABILITY_TOPIC = "availability_topic"
33+
CONF_AVAILABLE_TONES = "available_tones"
3334
CONF_BROKER = "broker"
3435
CONF_BIRTH_MESSAGE = "birth_message"
3536
CONF_CODE_ARM_REQUIRED = "code_arm_required"
@@ -200,6 +201,8 @@
200201
CONF_STATE_UNLOCKING = "state_unlocking"
201202
CONF_STEP = "step"
202203
CONF_SUGGESTED_DISPLAY_PRECISION = "suggested_display_precision"
204+
CONF_SUPPORT_DURATION = "support_duration"
205+
CONF_SUPPORT_VOLUME_SET = "support_volume_set"
203206
CONF_SUPPORTED_COLOR_MODES = "supported_color_modes"
204207
CONF_SWING_HORIZONTAL_MODE_COMMAND_TEMPLATE = "swing_horizontal_mode_command_template"
205208
CONF_SWING_HORIZONTAL_MODE_COMMAND_TOPIC = "swing_horizontal_mode_command_topic"

homeassistant/components/mqtt/siren.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@
3939
from . import subscription
4040
from .config import MQTT_RW_SCHEMA
4141
from .const import (
42+
CONF_AVAILABLE_TONES,
43+
CONF_COMMAND_OFF_TEMPLATE,
4244
CONF_COMMAND_TEMPLATE,
4345
CONF_COMMAND_TOPIC,
46+
CONF_STATE_OFF,
47+
CONF_STATE_ON,
4448
CONF_STATE_TOPIC,
4549
CONF_STATE_VALUE_TEMPLATE,
50+
CONF_SUPPORT_DURATION,
51+
CONF_SUPPORT_VOLUME_SET,
52+
DEFAULT_PAYLOAD_OFF,
53+
DEFAULT_PAYLOAD_ON,
4654
PAYLOAD_EMPTY_JSON,
4755
PAYLOAD_NONE,
4856
)
@@ -58,18 +66,9 @@
5866
PARALLEL_UPDATES = 0
5967

6068
DEFAULT_NAME = "MQTT Siren"
61-
DEFAULT_PAYLOAD_ON = "ON"
62-
DEFAULT_PAYLOAD_OFF = "OFF"
6369

6470
ENTITY_ID_FORMAT = siren.DOMAIN + ".{}"
6571

66-
CONF_AVAILABLE_TONES = "available_tones"
67-
CONF_COMMAND_OFF_TEMPLATE = "command_off_template"
68-
CONF_STATE_ON = "state_on"
69-
CONF_STATE_OFF = "state_off"
70-
CONF_SUPPORT_DURATION = "support_duration"
71-
CONF_SUPPORT_VOLUME_SET = "support_volume_set"
72-
7372
STATE = "state"
7473

7574
PLATFORM_SCHEMA_MODERN = MQTT_RW_SCHEMA.extend(

homeassistant/components/mqtt/strings.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
"title": "Configure MQTT device \"{mqtt_device}\"",
319319
"description": "Please configure MQTT specific details for {platform} entity \"{entity}\":",
320320
"data": {
321+
"available_tones": "Available Tones",
321322
"blue_template": "Blue template",
322323
"brightness_template": "Brightness template",
323324
"code": "Alarm code",
@@ -360,12 +361,15 @@
360361
"state_topic": "State topic",
361362
"state_value_template": "State value template",
362363
"step": "Step",
364+
"support_duration": "Duration support",
365+
"support_volume_set": "Set volume support",
363366
"supported_color_modes": "Supported color modes",
364367
"url_template": "URL template",
365368
"url_topic": "URL topic",
366369
"value_template": "Value template"
367370
},
368371
"data_description": {
372+
"available_tones": "The siren supports tones. The `tone` variable will become available for use in the \"Command template\" setting. [Learn more.]({url}#available_tones)",
369373
"blue_template": "[Template](https://www.home-assistant.io/docs/configuration/templating/#using-value-templates-with-mqtt) to extract blue color from the state payload value. Expected result of the template is an integer from 0-255 range.",
370374
"brightness_template": "[Template](https://www.home-assistant.io/docs/configuration/templating/#using-value-templates-with-mqtt) to extract brightness from the state payload value. Expected result of the template is an integer from 0-255 range.",
371375
"code": "Specifies a code to enable or disable the alarm in the frontend. Note that this blocks sending MQTT message commands to the remote device if the code validation fails. [Learn more.]({url}#code)",
@@ -407,6 +411,8 @@
407411
"state_template": "[Template](https://www.home-assistant.io/docs/configuration/templating/#using-value-templates-with-mqtt) to extract state from the state payload value.",
408412
"state_topic": "The MQTT topic subscribed to receive {platform} state values. [Learn more.]({url}#state_topic)",
409413
"step": "Step value. Smallest value 0.001.",
414+
"support_duration": "The siren supports setting a duration in second. The `duration` variable will become available for use in the \"Command template\" setting. [Learn more.]({url}#support_duration)",
415+
"support_volume_set": "The siren supports setting a volume. The `tone` variable will become available for use in the \"Command template\" setting. [Learn more.]({url}#support_volume_set)",
410416
"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)",
411417
"url_template": "[Template](https://www.home-assistant.io/docs/configuration/templating/#using-value-templates-with-mqtt) to extract an URL from the received URL topic payload value. [Learn more.]({url}#url_template)",
412418
"url_topic": "The MQTT topic subscribed to receive messages containing the image URL. [Learn more.]({url}#url_topic)",
@@ -910,6 +916,15 @@
910916
"target_humidity_state_topic": "The MQTT topic to subscribe for changes of the target humidity. [Learn more.]({url}#humidity_state_topic)"
911917
}
912918
},
919+
"siren_advanced_settings": {
920+
"name": "Advanced siren settings",
921+
"data": {
922+
"command_off_template": "Command \"off\" template"
923+
},
924+
"data_description": {
925+
"command_off_template": "The [template]({templating_url}#using-command-templates-with-mqtt) for \"off\" state changes. By default the \"[Command template]({url}#command_template)\" will be used. [Learn more.]({url}#command_off_template)"
926+
}
927+
},
913928
"target_temperature_settings": {
914929
"name": "Target temperature settings",
915930
"data": {
@@ -1338,6 +1353,7 @@
13381353
"number": "[%key:component::number::title%]",
13391354
"select": "[%key:component::select::title%]",
13401355
"sensor": "[%key:component::sensor::title%]",
1356+
"siren": "[%key:component::siren::title%]",
13411357
"switch": "[%key:component::switch::title%]"
13421358
}
13431359
},

tests/components/mqtt/common.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,25 @@
565565
"entity_picture": "https://example.com/e9261f6feed443e7b7d5f3fbe2a47412",
566566
},
567567
}
568+
MOCK_SUBENTRY_SIREN_COMPONENT = {
569+
"3faf1318023c46c5aea26707eeb6f12e": {
570+
"platform": "siren",
571+
"name": "Siren",
572+
"entity_category": None,
573+
"command_topic": "test-topic",
574+
"state_topic": "test-topic",
575+
"command_template": "{{ value }}",
576+
"command_off_template": "{{ value }}",
577+
"value_template": "{{ value_json.value }}",
578+
"payload_off": "OFF",
579+
"payload_on": "ON",
580+
"available_tones": ["Happy hour", "Cooling alarm"],
581+
"support_volume_set": True,
582+
"support_duration": True,
583+
"entity_picture": "https://example.com/3faf1318023c46c5aea26707eeb6f12e",
584+
"optimistic": True,
585+
},
586+
}
568587
MOCK_SUBENTRY_SWITCH_COMPONENT = {
569588
"3faf1318016c46c5aea26707eeb6f12e": {
570589
"platform": "switch",
@@ -698,6 +717,10 @@
698717
"device": MOCK_SUBENTRY_DEVICE_DATA | {"mqtt_settings": {"qos": 0}},
699718
"components": MOCK_SUBENTRY_SENSOR_COMPONENT_LAST_RESET,
700719
}
720+
MOCK_SIREN_SUBENTRY_DATA = {
721+
"device": MOCK_SUBENTRY_DEVICE_DATA | {"mqtt_settings": {"qos": 0}},
722+
"components": MOCK_SUBENTRY_SIREN_COMPONENT,
723+
}
701724
MOCK_SWITCH_SUBENTRY_DATA = {
702725
"device": MOCK_SUBENTRY_DEVICE_DATA | {"mqtt_settings": {"qos": 0}},
703726
"components": MOCK_SUBENTRY_SWITCH_COMPONENT,

tests/components/mqtt/test_config_flow.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
MOCK_SENSOR_SUBENTRY_DATA,
6161
MOCK_SENSOR_SUBENTRY_DATA_LAST_RESET_TEMPLATE,
6262
MOCK_SENSOR_SUBENTRY_DATA_STATE_CLASS,
63+
MOCK_SIREN_SUBENTRY_DATA,
6364
MOCK_SWITCH_SUBENTRY_DATA,
6465
)
6566

@@ -3655,6 +3656,41 @@ async def test_migrate_of_incompatible_config_entry(
36553656
"Milk notifier Energy",
36563657
id="sensor_total",
36573658
),
3659+
pytest.param(
3660+
MOCK_SIREN_SUBENTRY_DATA,
3661+
{"name": "Milk notifier", "mqtt_settings": {"qos": 0}},
3662+
{"name": "Siren"},
3663+
{},
3664+
(),
3665+
{
3666+
"command_topic": "test-topic",
3667+
"command_template": "{{ value }}",
3668+
"state_topic": "test-topic",
3669+
"value_template": "{{ value_json.value }}",
3670+
"optimistic": True,
3671+
"available_tones": ["Happy hour", "Cooling alarm"],
3672+
"support_duration": True,
3673+
"support_volume_set": True,
3674+
"siren_advanced_settings": {
3675+
"command_off_template": "{{ value }}",
3676+
},
3677+
},
3678+
(
3679+
(
3680+
{"command_topic": "test-topic#invalid"},
3681+
{"command_topic": "invalid_publish_topic"},
3682+
),
3683+
(
3684+
{
3685+
"command_topic": "test-topic",
3686+
"state_topic": "test-topic#invalid",
3687+
},
3688+
{"state_topic": "invalid_subscribe_topic"},
3689+
),
3690+
),
3691+
"Milk notifier Siren",
3692+
id="siren",
3693+
),
36583694
pytest.param(
36593695
MOCK_SWITCH_SUBENTRY_DATA,
36603696
{"name": "Milk notifier", "mqtt_settings": {"qos": 0}},

0 commit comments

Comments
 (0)