mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Add character encoding to MQTT automation. (#20292)
This commit is contained in:
parent
d84cd01cbf
commit
ec5da05804
@ -15,19 +15,23 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
|
|
||||||
DEPENDENCIES = ['mqtt']
|
DEPENDENCIES = ['mqtt']
|
||||||
|
|
||||||
|
CONF_ENCODING = 'encoding'
|
||||||
CONF_TOPIC = 'topic'
|
CONF_TOPIC = 'topic'
|
||||||
|
DEFAULT_ENCODING = 'utf-8'
|
||||||
|
|
||||||
TRIGGER_SCHEMA = vol.Schema({
|
TRIGGER_SCHEMA = vol.Schema({
|
||||||
vol.Required(CONF_PLATFORM): mqtt.DOMAIN,
|
vol.Required(CONF_PLATFORM): mqtt.DOMAIN,
|
||||||
vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic,
|
vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic,
|
||||||
vol.Optional(CONF_PAYLOAD): cv.string,
|
vol.Optional(CONF_PAYLOAD): cv.string,
|
||||||
|
vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
async def async_trigger(hass, config, action, automation_info):
|
async def async_trigger(hass, config, action, automation_info):
|
||||||
"""Listen for state changes based on configuration."""
|
"""Listen for state changes based on configuration."""
|
||||||
topic = config.get(CONF_TOPIC)
|
topic = config[CONF_TOPIC]
|
||||||
payload = config.get(CONF_PAYLOAD)
|
payload = config.get(CONF_PAYLOAD)
|
||||||
|
encoding = config[CONF_ENCODING] or None
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def mqtt_automation_listener(msg_topic, msg_payload, qos):
|
def mqtt_automation_listener(msg_topic, msg_payload, qos):
|
||||||
@ -50,5 +54,5 @@ async def async_trigger(hass, config, action, automation_info):
|
|||||||
})
|
})
|
||||||
|
|
||||||
remove = await mqtt.async_subscribe(
|
remove = await mqtt.async_subscribe(
|
||||||
hass, topic, mqtt_automation_listener)
|
hass, topic, mqtt_automation_listener, encoding=encoding)
|
||||||
return remove
|
return remove
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""The tests for the MQTT automation."""
|
"""The tests for the MQTT automation."""
|
||||||
import pytest
|
import pytest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.components.automation as automation
|
import homeassistant.components.automation as automation
|
||||||
@ -92,3 +93,44 @@ async def test_if_not_fires_on_topic_but_no_payload_match(hass, calls):
|
|||||||
async_fire_mqtt_message(hass, 'test-topic', 'no-hello')
|
async_fire_mqtt_message(hass, 'test-topic', 'no-hello')
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert 0 == len(calls)
|
assert 0 == len(calls)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_encoding_default(hass, calls):
|
||||||
|
"""Test default encoding."""
|
||||||
|
mock_mqtt = await async_mock_mqtt_component(hass)
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'topic': 'test-topic'
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
mock_mqtt.async_subscribe.assert_called_once_with(
|
||||||
|
'test-topic', mock.ANY, 0, 'utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
async def test_encoding_custom(hass, calls):
|
||||||
|
"""Test default encoding."""
|
||||||
|
mock_mqtt = await async_mock_mqtt_component(hass)
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
'trigger': {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'topic': 'test-topic',
|
||||||
|
'encoding': ''
|
||||||
|
},
|
||||||
|
'action': {
|
||||||
|
'service': 'test.automation'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
mock_mqtt.async_subscribe.assert_called_once_with(
|
||||||
|
'test-topic', mock.ANY, 0, None)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user