From 3aa4727b189305d845b216024a14780ab9ce6295 Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Sat, 9 Apr 2016 12:03:41 -0400 Subject: [PATCH] Fix for MQTT config validation on the protocol field. (#1765) --- homeassistant/components/mqtt/__init__.py | 2 +- tests/components/mqtt/test_init.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index ae932f26ff6..510aedb00e2 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -90,7 +90,7 @@ CONFIG_SCHEMA = vol.Schema({ vol.Optional(CONF_PASSWORD): cv.string, vol.Optional(CONF_CERTIFICATE): cv.isfile, vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTOCOL): - [PROTOCOL_31, PROTOCOL_311], + vol.All(cv.string, vol.In([PROTOCOL_31, PROTOCOL_311])), vol.Optional(CONF_EMBEDDED): _HBMQTT_CONFIG_SCHEMA, }), }, extra=vol.ALLOW_EXTRA) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index c83f8380913..4c5f14bf0f1 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -58,6 +58,17 @@ class TestMQTT(unittest.TestCase): } }) + def test_setup_protocol_validation(self): + """Test for setup failure if connection to broker is missing.""" + with mock.patch('paho.mqtt.client.Client'): + self.hass.config.components = [] + assert _setup_component(self.hass, mqtt.DOMAIN, { + mqtt.DOMAIN: { + mqtt.CONF_BROKER: 'test-broker', + mqtt.CONF_PROTOCOL: 3.1, + } + }) + def test_publish_calls_service(self): """Test the publishing of call to services.""" self.hass.bus.listen_once(EVENT_CALL_SERVICE, self.record_calls)