Skip to content

Commit

Permalink
Config validation for MQTT switch platform.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaharkes committed Apr 7, 2016
1 parent eb3f812 commit deecec5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
46 changes: 30 additions & 16 deletions homeassistant/components/switch/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,55 @@
"""
import logging

import voluptuous as vol

import homeassistant.components.mqtt as mqtt
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import CONF_VALUE_TEMPLATE
from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import template
from homeassistant.util import convert

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['mqtt']

CONF_STATE_TOPIC = 'state_topic'
CONF_COMMAND_TOPIC = 'command_topic'
CONF_RETAIN = 'retain'
CONF_PAYLOAD_ON = 'payload_on'
CONF_PAYLOAD_OFF = 'payload_off'

DEFAULT_NAME = "MQTT Switch"
DEFAULT_QOS = 0
DEFAULT_PAYLOAD_ON = "ON"
DEFAULT_PAYLOAD_OFF = "OFF"
DEFAULT_OPTIMISTIC = False
DEFAULT_RETAIN = False

DEPENDENCIES = ['mqtt']
PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Required(CONF_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
})


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Add MQTT switch."""
if config.get('command_topic') is None:
_LOGGER.error("Missing required variable: command_topic")
return False

add_devices_callback([MqttSwitch(
hass,
convert(config.get('name'), str, DEFAULT_NAME),
config.get('state_topic'),
config.get('command_topic'),
convert(config.get('qos'), int, DEFAULT_QOS),
convert(config.get('retain'), bool, DEFAULT_RETAIN),
convert(config.get('payload_on'), str, DEFAULT_PAYLOAD_ON),
convert(config.get('payload_off'), str, DEFAULT_PAYLOAD_OFF),
convert(config.get('optimistic'), bool, DEFAULT_OPTIMISTIC),
config[CONF_NAME],
config.get(CONF_STATE_TOPIC),
config[CONF_COMMAND_TOPIC],
config[mqtt.CONF_QOS],
config[CONF_RETAIN],
config[CONF_PAYLOAD_ON],
config[CONF_PAYLOAD_OFF],
config[CONF_OPTIMISTIC],
config.get(CONF_VALUE_TEMPLATE))])


Expand Down
22 changes: 13 additions & 9 deletions tests/components/switch/test_mqtt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The tests for the MQTT switch platform."""
import unittest

from homeassistant.bootstrap import _setup_component
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
import homeassistant.components.switch as switch
from tests.common import (
Expand All @@ -21,16 +22,17 @@ def tearDown(self): # pylint: disable=invalid-name

def test_controlling_state_via_topic(self):
"""Test the controlling state via topic."""
self.assertTrue(switch.setup(self.hass, {
'switch': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'payload_on': 1,
'payload_off': 0
}
}))
})

state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
Expand All @@ -50,16 +52,17 @@ def test_controlling_state_via_topic(self):

def test_sending_mqtt_commands_and_optimistic(self):
"""Test the sending MQTT commands in optimistic mode."""
self.assertTrue(switch.setup(self.hass, {
'switch': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'command_topic': 'command-topic',
'payload_on': 'beer on',
'payload_off': 'beer off',
'qos': '2'
}
}))
})

state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
Expand All @@ -83,8 +86,9 @@ def test_sending_mqtt_commands_and_optimistic(self):

def test_controlling_state_via_topic_and_json_message(self):
"""Test the controlling state via topic and JSON message."""
self.assertTrue(switch.setup(self.hass, {
'switch': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
Expand All @@ -93,7 +97,7 @@ def test_controlling_state_via_topic_and_json_message(self):
'payload_off': 'beer off',
'value_template': '{{ value_json.val }}'
}
}))
})

state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
Expand Down

0 comments on commit deecec5

Please sign in to comment.