From ec5da05804dd66c2199bebb5f3a023b9656a0c63 Mon Sep 17 00:00:00 2001 From: emontnemery Date: Fri, 25 Jan 2019 14:43:56 +0800 Subject: [PATCH] Add character encoding to MQTT automation. (#20292) --- homeassistant/components/automation/mqtt.py | 8 +++- tests/components/automation/test_mqtt.py | 42 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/automation/mqtt.py b/homeassistant/components/automation/mqtt.py index 67c538154e5..1fa0d540610 100644 --- a/homeassistant/components/automation/mqtt.py +++ b/homeassistant/components/automation/mqtt.py @@ -15,19 +15,23 @@ import homeassistant.helpers.config_validation as cv DEPENDENCIES = ['mqtt'] +CONF_ENCODING = 'encoding' CONF_TOPIC = 'topic' +DEFAULT_ENCODING = 'utf-8' TRIGGER_SCHEMA = vol.Schema({ vol.Required(CONF_PLATFORM): mqtt.DOMAIN, vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic, vol.Optional(CONF_PAYLOAD): cv.string, + vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string, }) async def async_trigger(hass, config, action, automation_info): """Listen for state changes based on configuration.""" - topic = config.get(CONF_TOPIC) + topic = config[CONF_TOPIC] payload = config.get(CONF_PAYLOAD) + encoding = config[CONF_ENCODING] or None @callback 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( - hass, topic, mqtt_automation_listener) + hass, topic, mqtt_automation_listener, encoding=encoding) return remove diff --git a/tests/components/automation/test_mqtt.py b/tests/components/automation/test_mqtt.py index 196fdaa9a6f..7d2fe5fa439 100644 --- a/tests/components/automation/test_mqtt.py +++ b/tests/components/automation/test_mqtt.py @@ -1,5 +1,6 @@ """The tests for the MQTT automation.""" import pytest +from unittest import mock from homeassistant.setup import async_setup_component 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') await hass.async_block_till_done() 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)