mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add voluptuous to binary template sensor (#2938)
* Add voluptuous to binary template sensor / update failing test. * Update tests. * Quick fixes to remove duplicate variables
This commit is contained in:
parent
61ef2683c5
commit
4795122463
@ -5,55 +5,47 @@ For more details about this platform, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/binary_sensor.template/
|
https://home-assistant.io/components/binary_sensor.template/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
import voluptuous as vol
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (BinarySensorDevice,
|
from homeassistant.components.binary_sensor import (BinarySensorDevice,
|
||||||
ENTITY_ID_FORMAT,
|
ENTITY_ID_FORMAT,
|
||||||
SENSOR_CLASSES)
|
PLATFORM_SCHEMA,
|
||||||
from homeassistant.const import (ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE,
|
SENSOR_CLASSES_SCHEMA)
|
||||||
ATTR_ENTITY_ID, MATCH_ALL)
|
|
||||||
|
from homeassistant.const import (ATTR_FRIENDLY_NAME, ATTR_ENTITY_ID, MATCH_ALL,
|
||||||
|
CONF_VALUE_TEMPLATE, CONF_SENSOR_CLASS)
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.helpers.entity import generate_entity_id
|
from homeassistant.helpers.entity import generate_entity_id
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import track_state_change
|
||||||
from homeassistant.util import slugify
|
|
||||||
|
|
||||||
CONF_SENSORS = 'sensors'
|
CONF_SENSORS = 'sensors'
|
||||||
|
|
||||||
|
SENSOR_SCHEMA = vol.Schema({
|
||||||
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
|
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
|
||||||
|
vol.Optional(ATTR_ENTITY_ID, default=MATCH_ALL): cv.entity_ids,
|
||||||
|
vol.Optional(CONF_SENSOR_CLASS, default=None): SENSOR_CLASSES_SCHEMA
|
||||||
|
})
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_SENSORS): vol.Schema({cv.slug: SENSOR_SCHEMA}),
|
||||||
|
})
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup template binary sensors."""
|
"""Setup template binary sensors."""
|
||||||
sensors = []
|
sensors = []
|
||||||
if config.get(CONF_SENSORS) is None:
|
|
||||||
_LOGGER.error('Missing configuration data for binary_sensor platform')
|
|
||||||
return False
|
|
||||||
|
|
||||||
for device, device_config in config[CONF_SENSORS].items():
|
for device, device_config in config[CONF_SENSORS].items():
|
||||||
|
|
||||||
if device != slugify(device):
|
value_template = device_config[CONF_VALUE_TEMPLATE]
|
||||||
_LOGGER.error('Found invalid key for binary_sensor.template: %s. '
|
entity_ids = device_config[ATTR_ENTITY_ID]
|
||||||
'Use %s instead', device, slugify(device))
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not isinstance(device_config, dict):
|
|
||||||
_LOGGER.error('Missing configuration data for binary_sensor %s',
|
|
||||||
device)
|
|
||||||
continue
|
|
||||||
|
|
||||||
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
||||||
sensor_class = device_config.get('sensor_class')
|
sensor_class = device_config.get(CONF_SENSOR_CLASS)
|
||||||
value_template = device_config.get(CONF_VALUE_TEMPLATE)
|
|
||||||
|
|
||||||
if sensor_class not in SENSOR_CLASSES:
|
|
||||||
_LOGGER.error('Sensor class is not valid')
|
|
||||||
continue
|
|
||||||
|
|
||||||
if value_template is None:
|
|
||||||
_LOGGER.error(
|
|
||||||
'Missing %s for sensor %s', CONF_VALUE_TEMPLATE, device)
|
|
||||||
continue
|
|
||||||
|
|
||||||
entity_ids = device_config.get(ATTR_ENTITY_ID, MATCH_ALL)
|
|
||||||
|
|
||||||
sensors.append(
|
sensors.append(
|
||||||
BinarySensorTemplate(
|
BinarySensorTemplate(
|
||||||
|
@ -3,6 +3,7 @@ import unittest
|
|||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
|
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
|
||||||
|
import homeassistant.bootstrap as bootstrap
|
||||||
from homeassistant.components.binary_sensor import template
|
from homeassistant.components.binary_sensor import template
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
|
|
||||||
@ -21,6 +22,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||||||
'friendly_name': 'virtual thingy',
|
'friendly_name': 'virtual thingy',
|
||||||
'value_template': '{{ foo }}',
|
'value_template': '{{ foo }}',
|
||||||
'sensor_class': 'motion',
|
'sensor_class': 'motion',
|
||||||
|
'entity_id': 'test'
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,28 +31,38 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||||||
result = template.setup_platform(hass, config, add_devices)
|
result = template.setup_platform(hass, config, add_devices)
|
||||||
self.assertTrue(result)
|
self.assertTrue(result)
|
||||||
mock_template.assert_called_once_with(hass, 'test', 'virtual thingy',
|
mock_template.assert_called_once_with(hass, 'test', 'virtual thingy',
|
||||||
'motion', '{{ foo }}', MATCH_ALL)
|
'motion', '{{ foo }}', 'test')
|
||||||
add_devices.assert_called_once_with([mock_template.return_value])
|
add_devices.assert_called_once_with([mock_template.return_value])
|
||||||
|
|
||||||
def test_setup_no_sensors(self):
|
def test_setup_no_sensors(self):
|
||||||
""""Test setup with no sensors."""
|
""""Test setup with no sensors."""
|
||||||
config = {}
|
hass = mock.MagicMock()
|
||||||
result = template.setup_platform(None, config, None)
|
result = bootstrap.setup_component(hass, 'sensor', {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template'
|
||||||
|
}
|
||||||
|
})
|
||||||
self.assertFalse(result)
|
self.assertFalse(result)
|
||||||
|
|
||||||
def test_setup_invalid_device(self):
|
def test_setup_invalid_device(self):
|
||||||
""""Test the setup with invalid devices."""
|
""""Test the setup with invalid devices."""
|
||||||
config = {
|
hass = mock.MagicMock()
|
||||||
|
result = bootstrap.setup_component(hass, 'sensor', {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template',
|
||||||
'sensors': {
|
'sensors': {
|
||||||
'foo bar': {},
|
'foo bar': {},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
result = template.setup_platform(None, config, None)
|
})
|
||||||
self.assertFalse(result)
|
self.assertFalse(result)
|
||||||
|
|
||||||
def test_setup_invalid_sensor_class(self):
|
def test_setup_invalid_sensor_class(self):
|
||||||
""""Test setup with invalid sensor class."""
|
""""Test setup with invalid sensor class."""
|
||||||
config = {
|
hass = mock.MagicMock()
|
||||||
|
result = bootstrap.setup_component(hass, 'sensor', {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template',
|
||||||
'sensors': {
|
'sensors': {
|
||||||
'test': {
|
'test': {
|
||||||
'value_template': '{{ foo }}',
|
'value_template': '{{ foo }}',
|
||||||
@ -58,19 +70,22 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
result = template.setup_platform(None, config, None)
|
})
|
||||||
self.assertFalse(result)
|
self.assertFalse(result)
|
||||||
|
|
||||||
def test_setup_invalid_missing_template(self):
|
def test_setup_invalid_missing_template(self):
|
||||||
""""Test setup with invalid and missing template."""
|
""""Test setup with invalid and missing template."""
|
||||||
config = {
|
hass = mock.MagicMock()
|
||||||
|
result = bootstrap.setup_component(hass, 'sensor', {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template',
|
||||||
'sensors': {
|
'sensors': {
|
||||||
'test': {
|
'test': {
|
||||||
'sensor_class': 'motion',
|
'sensor_class': 'motion',
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
}
|
||||||
result = template.setup_platform(None, config, None)
|
}
|
||||||
|
})
|
||||||
self.assertFalse(result)
|
self.assertFalse(result)
|
||||||
|
|
||||||
def test_attributes(self):
|
def test_attributes(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user