Skip to content

Commit

Permalink
Add voluptuous to template switch (#2940)
Browse files Browse the repository at this point in the history
* Add voluptuous to template switch / revise tests.
  • Loading branch information
pavoni authored and kellerza committed Aug 22, 2016
1 parent e5969f0 commit eac67fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
47 changes: 19 additions & 28 deletions homeassistant/components/switch/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
https://home-assistant.io/components/switch.template/
"""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv

from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice
from homeassistant.components.switch import (
ENTITY_ID_FORMAT, SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (
ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, STATE_OFF, STATE_ON,
ATTR_ENTITY_ID, MATCH_ALL)
Expand All @@ -15,7 +18,6 @@
from homeassistant.helpers.script import Script
from homeassistant.helpers import template
from homeassistant.helpers.event import track_state_change
from homeassistant.util import slugify

CONF_SWITCHES = 'switches'

Expand All @@ -25,40 +27,29 @@
_LOGGER = logging.getLogger(__name__)
_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false']

SWITCH_SCHEMA = vol.Schema({
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
vol.Required(ON_ACTION): cv.SCRIPT_SCHEMA,
vol.Required(OFF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids
})

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SWITCHES): vol.Schema({cv.slug: SWITCH_SCHEMA}),
})


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Template switch."""
switches = []
if config.get(CONF_SWITCHES) is None:
_LOGGER.error("Missing configuration data for switch platform")
return False

for device, device_config in config[CONF_SWITCHES].items():

if device != slugify(device):
_LOGGER.error("Found invalid key for switch.template: %s. "
"Use %s instead", device, slugify(device))
continue

if not isinstance(device_config, dict):
_LOGGER.error("Missing configuration data for switch %s", device)
continue

friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
state_template = device_config.get(CONF_VALUE_TEMPLATE)
on_action = device_config.get(ON_ACTION)
off_action = device_config.get(OFF_ACTION)
if state_template is None:
_LOGGER.error(
"Missing %s for switch %s", CONF_VALUE_TEMPLATE, device)
continue

if on_action is None or off_action is None:
_LOGGER.error(
"Missing action for switch %s", device)
continue

state_template = device_config[CONF_VALUE_TEMPLATE]
on_action = device_config[ON_ACTION]
off_action = device_config[OFF_ACTION]
entity_ids = device_config.get(ATTR_ENTITY_ID, MATCH_ALL)

switches.append(
Expand Down
39 changes: 18 additions & 21 deletions tests/components/switch/test_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""The tests for the Template switch platform."""
import homeassistant.bootstrap as bootstrap
import homeassistant.components as core
import homeassistant.components.switch as switch

from homeassistant.const import (
STATE_ON,
Expand All @@ -18,6 +18,7 @@ def setup_method(self, method):
self.calls = []

def record_call(service):
"""Track function calls.."""
self.calls.append(service)

self.hass.services.register('test', 'automation', record_call)
Expand All @@ -28,7 +29,7 @@ def teardown_method(self, method):

def test_template_state_text(self):
""""Test the state text of a template."""
assert switch.setup(self.hass, {
assert bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand Down Expand Up @@ -62,7 +63,7 @@ def test_template_state_text(self):

def test_template_state_boolean_on(self):
"""Test the setting of the state with boolean on."""
assert switch.setup(self.hass, {
assert bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -87,7 +88,7 @@ def test_template_state_boolean_on(self):

def test_template_state_boolean_off(self):
"""Test the setting of the state with off."""
assert switch.setup(self.hass, {
assert bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -112,7 +113,7 @@ def test_template_state_boolean_off(self):

def test_template_syntax_error(self):
"""Test templating syntax error."""
assert switch.setup(self.hass, {
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -131,15 +132,11 @@ def test_template_syntax_error(self):
}
}
})

state = self.hass.states.set('switch.test_state', STATE_ON)
self.hass.pool.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == 'unavailable'
assert self.hass.states.all() == []

def test_invalid_name_does_not_create(self):
"""Test invalid name."""
assert switch.setup(self.hass, {
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -161,8 +158,8 @@ def test_invalid_name_does_not_create(self):
assert self.hass.states.all() == []

def test_invalid_switch_does_not_create(self):
"""Test invalid name."""
assert switch.setup(self.hass, {
"""Test invalid switch."""
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -174,7 +171,7 @@ def test_invalid_switch_does_not_create(self):

def test_no_switches_does_not_create(self):
"""Test if there are no switches no creation."""
assert switch.setup(self.hass, {
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template'
}
Expand All @@ -183,7 +180,7 @@ def test_no_switches_does_not_create(self):

def test_missing_template_does_not_create(self):
"""Test missing template."""
assert switch.setup(self.hass, {
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -206,7 +203,7 @@ def test_missing_template_does_not_create(self):

def test_missing_on_does_not_create(self):
"""Test missing on."""
assert switch.setup(self.hass, {
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -229,7 +226,7 @@ def test_missing_on_does_not_create(self):

def test_missing_off_does_not_create(self):
"""Test missing off."""
assert switch.setup(self.hass, {
assert not bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -252,7 +249,7 @@ def test_missing_off_does_not_create(self):

def test_on_action(self):
"""Test on action."""
assert switch.setup(self.hass, {
assert bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand All @@ -279,11 +276,11 @@ def test_on_action(self):
core.switch.turn_on(self.hass, 'switch.test_template_switch')
self.hass.pool.block_till_done()

assert 1 == len(self.calls)
assert len(self.calls) == 1

def test_off_action(self):
"""Test off action."""
assert switch.setup(self.hass, {
assert bootstrap.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
Expand Down Expand Up @@ -311,4 +308,4 @@ def test_off_action(self):
core.switch.turn_off(self.hass, 'switch.test_template_switch')
self.hass.pool.block_till_done()

assert 1 == len(self.calls)
assert len(self.calls) == 1

0 comments on commit eac67fd

Please sign in to comment.