Config validation for MQTT binary_sensor platform.

This commit is contained in:
Jan Harkes 2016-04-05 00:53:58 -04:00
parent 0bd4e15fcb
commit 287f0f4f68
2 changed files with 41 additions and 28 deletions

View File

@ -6,43 +6,52 @@ https://home-assistant.io/components/binary_sensor.mqtt/
"""
import logging
import voluptuous as vol
import homeassistant.components.mqtt as mqtt
from homeassistant.components.binary_sensor import (BinarySensorDevice,
SENSOR_CLASSES)
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_SENSOR_CLASS = 'sensor_class'
CONF_PAYLOAD_ON = 'payload_on'
CONF_PAYLOAD_OFF = 'payload_off'
DEFAULT_NAME = 'MQTT Binary sensor'
DEFAULT_QOS = 0
DEFAULT_PAYLOAD_ON = 'ON'
DEFAULT_PAYLOAD_OFF = 'OFF'
DEPENDENCIES = ['mqtt']
PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Required(CONF_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_SENSOR_CLASS, default=None):
vol.Any(vol.In(SENSOR_CLASSES), vol.SetTo(None)),
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_VALUE_TEMPLATE): cv.template,
})
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Add MQTT binary sensor."""
if config.get('state_topic') is None:
_LOGGER.error('Missing required variable: state_topic')
return False
sensor_class = config.get('sensor_class')
if sensor_class not in SENSOR_CLASSES:
_LOGGER.warning('Unknown sensor class: %s', sensor_class)
sensor_class = None
add_devices([MqttBinarySensor(
hass,
config.get('name', DEFAULT_NAME),
config.get('state_topic', None),
sensor_class,
config.get('qos', DEFAULT_QOS),
config.get('payload_on', DEFAULT_PAYLOAD_ON),
config.get('payload_off', DEFAULT_PAYLOAD_OFF),
config.get(CONF_VALUE_TEMPLATE))])
config[CONF_NAME],
config[CONF_STATE_TOPIC],
config[CONF_SENSOR_CLASS],
config[mqtt.CONF_QOS],
config[CONF_PAYLOAD_ON],
config[CONF_PAYLOAD_OFF],
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 binary sensor platform."""
import unittest
from homeassistant.bootstrap import _setup_component
import homeassistant.components.binary_sensor as binary_sensor
from tests.common import mock_mqtt_component, fire_mqtt_message
from homeassistant.const import (STATE_OFF, STATE_ON)
@ -22,15 +23,16 @@ class TestSensorMQTT(unittest.TestCase):
def test_setting_sensor_value_via_mqtt_message(self):
"""Test the setting of the value via MQTT."""
self.assertTrue(binary_sensor.setup(self.hass, {
'binary_sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'payload_on': 'ON',
'payload_off': 'OFF',
}
}))
})
state = self.hass.states.get('binary_sensor.test')
self.assertEqual(STATE_OFF, state.state)
@ -47,28 +49,30 @@ class TestSensorMQTT(unittest.TestCase):
def test_valid_sensor_class(self):
"""Test the setting of a valid sensor class."""
self.assertTrue(binary_sensor.setup(self.hass, {
'binary_sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'sensor_class': 'motion',
'state_topic': 'test-topic',
}
}))
})
state = self.hass.states.get('binary_sensor.test')
self.assertEqual('motion', state.attributes.get('sensor_class'))
def test_invalid_sensor_class(self):
"""Test the setting of an invalid sensor class."""
self.assertTrue(binary_sensor.setup(self.hass, {
'binary_sensor': {
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'sensor_class': 'abc123',
'state_topic': 'test-topic',
}
}))
})
state = self.hass.states.get('binary_sensor.test')
self.assertIsNone(state.attributes.get('sensor_class'))