Add support for MQTT device triggers (#31679)

* Add support for MQTT device triggers

* Fix test, tweaks

* Improve test coverage

* Address review comments, improve tests

* Tidy up exception handling

* Fix abbreviations

* Rewrite to handle update of attached triggers

* Update abbreviation test

* Refactor according to review comments

* Refactor according to review comments

* Improve trigger removal

* Further refactoring
This commit is contained in:
Erik Montnemery
2020-02-18 22:51:10 +01:00
committed by GitHub
parent f6540e3002
commit 60ae85564e
10 changed files with 1170 additions and 34 deletions

View File

@@ -11,8 +11,10 @@ import homeassistant.helpers.config_validation as cv
# mypy: allow-untyped-defs
CONF_ENCODING = "encoding"
CONF_QOS = "qos"
CONF_TOPIC = "topic"
DEFAULT_ENCODING = "utf-8"
DEFAULT_QOS = 0
TRIGGER_SCHEMA = vol.Schema(
{
@@ -20,6 +22,9 @@ TRIGGER_SCHEMA = vol.Schema(
vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_PAYLOAD): cv.string,
vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string,
vol.Optional(CONF_QOS, default=DEFAULT_QOS): vol.All(
vol.Coerce(int), vol.In([0, 1, 2])
),
}
)
@@ -29,6 +34,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
topic = config[CONF_TOPIC]
payload = config.get(CONF_PAYLOAD)
encoding = config[CONF_ENCODING] or None
qos = config[CONF_QOS]
@callback
def mqtt_automation_listener(mqttmsg):
@@ -49,6 +55,6 @@ async def async_attach_trigger(hass, config, action, automation_info):
hass.async_run_job(action, {"trigger": data})
remove = await mqtt.async_subscribe(
hass, topic, mqtt_automation_listener, encoding=encoding
hass, topic, mqtt_automation_listener, encoding=encoding, qos=qos
)
return remove