Add actions.

pull/1112/head
pavoni 2016-02-03 14:29:25 +00:00
parent 9a9dbcfaea
commit 5521096c02
2 changed files with 76 additions and 2 deletions

View File

@ -19,7 +19,10 @@ from homeassistant.const import (
ATTR_FRIENDLY_NAME,
CONF_VALUE_TEMPLATE)
from homeassistant.helpers.service import call_from_config
from homeassistant.util import template, slugify
from homeassistant.exceptions import TemplateError
from homeassistant.components.switch import DOMAIN
@ -132,10 +135,10 @@ class SwitchTemplate(SwitchDevice):
return False
def turn_on(self, **kwargs):
_LOGGER.error("TURN ON not implemented yet")
call_from_config(self.hass, self._on_action, True)
def turn_off(self, **kwargs):
_LOGGER.error("TURN OFF not implemented yet")
call_from_config(self.hass, self._off_action, True)
@property
def is_on(self):

View File

@ -6,6 +6,7 @@ Tests template switch.
"""
import homeassistant.core as ha
import homeassistant.components as core
import homeassistant.components.switch as switch
from homeassistant.const import (
@ -19,6 +20,14 @@ class TestTemplateSwitch:
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.calls = []
def record_call(service):
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def teardown_method(self, method):
""" Stop down stuff we started. """
self.hass.stop()
@ -238,3 +247,65 @@ class TestTemplateSwitch:
}
})
assert self.hass.states.all() == []
def test_on_action(self):
assert switch.setup(self.hass, {
'switch': {
'platform': 'template',
'switches': {
'test_template_switch': {
'value_template':
"{{ states.switch.test_state.state }}",
'turn_on': {
'service': 'test.automation'
},
'turn_off': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
},
}
}
}
})
self.hass.states.set('switch.test_state', STATE_OFF)
self.hass.pool.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_OFF
core.switch.turn_on(self.hass, 'switch.test_template_switch')
self.hass.pool.block_till_done()
assert 1 == len(self.calls)
def test_off_action(self):
assert switch.setup(self.hass, {
'switch': {
'platform': 'template',
'switches': {
'test_template_switch': {
'value_template':
"{{ states.switch.test_state.state }}",
'turn_on': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'turn_off': {
'service': 'test.automation'
},
}
}
}
})
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 == STATE_ON
core.switch.turn_off(self.hass, 'switch.test_template_switch')
self.hass.pool.block_till_done()
assert 1 == len(self.calls)