Config validation for MQTT rollershutter platform.

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

View File

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