Config validation for MQTT switch platform.
This commit is contained in:
parent
eb3f812e38
commit
deecec5e4e
2 changed files with 43 additions and 25 deletions
|
@ -6,41 +6,55 @@ https://home-assistant.io/components/switch.mqtt/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.components.mqtt as mqtt
|
import homeassistant.components.mqtt as mqtt
|
||||||
from homeassistant.components.switch import SwitchDevice
|
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.helpers import template
|
||||||
from homeassistant.util import convert
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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_NAME = "MQTT Switch"
|
||||||
DEFAULT_QOS = 0
|
|
||||||
DEFAULT_PAYLOAD_ON = "ON"
|
DEFAULT_PAYLOAD_ON = "ON"
|
||||||
DEFAULT_PAYLOAD_OFF = "OFF"
|
DEFAULT_PAYLOAD_OFF = "OFF"
|
||||||
DEFAULT_OPTIMISTIC = False
|
DEFAULT_OPTIMISTIC = False
|
||||||
DEFAULT_RETAIN = 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
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
"""Add MQTT switch."""
|
"""Add MQTT switch."""
|
||||||
if config.get('command_topic') is None:
|
|
||||||
_LOGGER.error("Missing required variable: command_topic")
|
|
||||||
return False
|
|
||||||
|
|
||||||
add_devices_callback([MqttSwitch(
|
add_devices_callback([MqttSwitch(
|
||||||
hass,
|
hass,
|
||||||
convert(config.get('name'), str, DEFAULT_NAME),
|
config[CONF_NAME],
|
||||||
config.get('state_topic'),
|
config.get(CONF_STATE_TOPIC),
|
||||||
config.get('command_topic'),
|
config[CONF_COMMAND_TOPIC],
|
||||||
convert(config.get('qos'), int, DEFAULT_QOS),
|
config[mqtt.CONF_QOS],
|
||||||
convert(config.get('retain'), bool, DEFAULT_RETAIN),
|
config[CONF_RETAIN],
|
||||||
convert(config.get('payload_on'), str, DEFAULT_PAYLOAD_ON),
|
config[CONF_PAYLOAD_ON],
|
||||||
convert(config.get('payload_off'), str, DEFAULT_PAYLOAD_OFF),
|
config[CONF_PAYLOAD_OFF],
|
||||||
convert(config.get('optimistic'), bool, DEFAULT_OPTIMISTIC),
|
config[CONF_OPTIMISTIC],
|
||||||
config.get(CONF_VALUE_TEMPLATE))])
|
config.get(CONF_VALUE_TEMPLATE))])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""The tests for the MQTT switch platform."""
|
"""The tests for the MQTT switch platform."""
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
from homeassistant.bootstrap import _setup_component
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
|
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
|
||||||
import homeassistant.components.switch as switch
|
import homeassistant.components.switch as switch
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
|
@ -21,8 +22,9 @@ class TestSensorMQTT(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(switch.setup(self.hass, {
|
self.hass.config.components = ['mqtt']
|
||||||
'switch': {
|
assert _setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
'state_topic': 'state-topic',
|
'state_topic': 'state-topic',
|
||||||
|
@ -30,7 +32,7 @@ class TestSensorMQTT(unittest.TestCase):
|
||||||
'payload_on': 1,
|
'payload_on': 1,
|
||||||
'payload_off': 0
|
'payload_off': 0
|
||||||
}
|
}
|
||||||
}))
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = self.hass.states.get('switch.test')
|
||||||
self.assertEqual(STATE_OFF, state.state)
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
@ -50,8 +52,9 @@ class TestSensorMQTT(unittest.TestCase):
|
||||||
|
|
||||||
def test_sending_mqtt_commands_and_optimistic(self):
|
def test_sending_mqtt_commands_and_optimistic(self):
|
||||||
"""Test the sending MQTT commands in optimistic mode."""
|
"""Test the sending MQTT commands in optimistic mode."""
|
||||||
self.assertTrue(switch.setup(self.hass, {
|
self.hass.config.components = ['mqtt']
|
||||||
'switch': {
|
assert _setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
'command_topic': 'command-topic',
|
'command_topic': 'command-topic',
|
||||||
|
@ -59,7 +62,7 @@ class TestSensorMQTT(unittest.TestCase):
|
||||||
'payload_off': 'beer off',
|
'payload_off': 'beer off',
|
||||||
'qos': '2'
|
'qos': '2'
|
||||||
}
|
}
|
||||||
}))
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = self.hass.states.get('switch.test')
|
||||||
self.assertEqual(STATE_OFF, state.state)
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
@ -83,8 +86,9 @@ class TestSensorMQTT(unittest.TestCase):
|
||||||
|
|
||||||
def test_controlling_state_via_topic_and_json_message(self):
|
def test_controlling_state_via_topic_and_json_message(self):
|
||||||
"""Test the controlling state via topic and JSON message."""
|
"""Test the controlling state via topic and JSON message."""
|
||||||
self.assertTrue(switch.setup(self.hass, {
|
self.hass.config.components = ['mqtt']
|
||||||
'switch': {
|
assert _setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
'state_topic': 'state-topic',
|
'state_topic': 'state-topic',
|
||||||
|
@ -93,7 +97,7 @@ class TestSensorMQTT(unittest.TestCase):
|
||||||
'payload_off': 'beer off',
|
'payload_off': 'beer off',
|
||||||
'value_template': '{{ value_json.val }}'
|
'value_template': '{{ value_json.val }}'
|
||||||
}
|
}
|
||||||
}))
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = self.hass.states.get('switch.test')
|
||||||
self.assertEqual(STATE_OFF, state.state)
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue