Config validation for MQTT rollershutter platform.

pull/1703/head
Jan Harkes 2016-04-06 21:00:14 -04:00
parent deecec5e4e
commit fca08b095a
2 changed files with 50 additions and 29 deletions

View File

@ -6,38 +6,53 @@ https://home-assistant.io/components/rollershutter.mqtt/
"""
import logging
import voluptuous as vol
import homeassistant.components.mqtt as mqtt
from homeassistant.components.rollershutter import RollershutterDevice
from homeassistant.const import CONF_VALUE_TEMPLATE
from homeassistant.const import CONF_NAME, CONF_VALUE_TEMPLATE
from homeassistant.helpers import template
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['mqtt']
CONF_STATE_TOPIC = 'state_topic'
CONF_COMMAND_TOPIC = 'command_topic'
CONF_PAYLOAD_UP = 'payload_up'
CONF_PAYLOAD_DOWN = 'payload_down'
CONF_PAYLOAD_STOP = 'payload_stop'
DEFAULT_NAME = "MQTT Rollershutter"
DEFAULT_QOS = 0
DEFAULT_PAYLOAD_UP = "UP"
DEFAULT_PAYLOAD_DOWN = "DOWN"
DEFAULT_PAYLOAD_STOP = "STOP"
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_PAYLOAD_UP, default=DEFAULT_PAYLOAD_UP): cv.string,
vol.Optional(CONF_PAYLOAD_DOWN, default=DEFAULT_PAYLOAD_DOWN): cv.string,
vol.Optional(CONF_PAYLOAD_STOP, default=DEFAULT_PAYLOAD_STOP): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
})
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Add MQTT Rollershutter."""
if config.get('command_topic') is None:
_LOGGER.error("Missing required variable: command_topic")
return False
add_devices_callback([MqttRollershutter(
hass,
config.get('name', DEFAULT_NAME),
config.get('state_topic'),
config.get('command_topic'),
config.get('qos', DEFAULT_QOS),
config.get('payload_up', DEFAULT_PAYLOAD_UP),
config.get('payload_down', DEFAULT_PAYLOAD_DOWN),
config.get('payload_stop', DEFAULT_PAYLOAD_STOP),
config.get(CONF_VALUE_TEMPLATE))])
config[CONF_NAME],
config.get(CONF_STATE_TOPIC),
config[CONF_COMMAND_TOPIC],
config[mqtt.CONF_QOS],
config[CONF_PAYLOAD_UP],
config[CONF_PAYLOAD_DOWN],
config[CONF_PAYLOAD_STOP],
config.get(CONF_VALUE_TEMPLATE)
)])
# pylint: disable=too-many-arguments, too-many-instance-attributes

View File

@ -1,6 +1,7 @@
"""The tests for the MQTT roller shutter platform."""
import unittest
from homeassistant.bootstrap import _setup_component
from homeassistant.const import STATE_OPEN, STATE_CLOSED, STATE_UNKNOWN
import homeassistant.components.rollershutter as rollershutter
from tests.common import mock_mqtt_component, fire_mqtt_message
@ -22,8 +23,9 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_controlling_state_via_topic(self):
"""Test the controlling state via topic."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -33,7 +35,7 @@ class TestRollershutterMQTT(unittest.TestCase):
'payload_down': 'DOWN',
'payload_stop': 'STOP'
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -58,15 +60,16 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_send_move_up_command(self):
"""Test the sending of move_up."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 2
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -81,15 +84,16 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_send_move_down_command(self):
"""Test the sending of move_down."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 2
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -104,15 +108,16 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_send_stop_command(self):
"""Test the sending of stop."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 2
}
}))
})
state = self.hass.states.get('rollershutter.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -127,8 +132,9 @@ class TestRollershutterMQTT(unittest.TestCase):
def test_state_attributes_current_position(self):
"""Test the current position."""
self.assertTrue(rollershutter.setup(self.hass, {
'rollershutter': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, rollershutter.DOMAIN, {
rollershutter.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
@ -137,7 +143,7 @@ class TestRollershutterMQTT(unittest.TestCase):
'payload_down': 'DOWN',
'payload_stop': 'STOP'
}
}))
})
state_attributes_dict = self.hass.states.get(
'rollershutter.test').attributes