diff --git a/homeassistant/components/switch/template.py b/homeassistant/components/switch/template.py index 170df067e57..1747df37d56 100644 --- a/homeassistant/components/switch/template.py +++ b/homeassistant/components/switch/template.py @@ -14,6 +14,8 @@ from homeassistant.components.switch import SwitchDevice from homeassistant.core import EVENT_STATE_CHANGED from homeassistant.const import ( + STATE_ON, + STATE_OFF, ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE) @@ -33,6 +35,8 @@ STATE_ERROR = 'error' ON_ACTION = 'turn_on' OFF_ACTION = 'turn_off' +STATE_TRUE = 'True' +STATE_FALSE = 'False' # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): @@ -116,16 +120,12 @@ class SwitchTemplate(SwitchDevice): self.hass.bus.listen(EVENT_STATE_CHANGED, _update_callback) + @property def name(self): """ Returns the name of the device. """ return self._name - @property - def state(self): - """ Returns the state of the device. """ - return self._state - @property def should_poll(self): """ Tells Home Assistant not to poll this entity. """ @@ -137,16 +137,24 @@ class SwitchTemplate(SwitchDevice): def turn_off(self, **kwargs): _LOGGER.error("TURN OFF not implemented yet") - @property - def should_poll(self): - """ Tells Home Assistant not to poll this entity. """ - return False - @property def is_on(self): """ True if device is on. """ - _LOGGER.error("IS ON CALLED %s", self._state) - return self._state == STATE_ON + return self._state == STATE_TRUE or self._state == STATE_ON + + @property + def is_off(self): + """ True if device is on. """ + return self._state == STATE_FALSE or self._state == STATE_OFF + + @property + def state(self): + """ Returns the state. """ + if self.is_on: + return STATE_ON + if self.is_off: + return STATE_OFF + return self._state def update(self): try: diff --git a/tests/components/switch/test_template.py b/tests/components/switch/test_template.py index 3dc93c41bf2..1f9de0fdd3a 100644 --- a/tests/components/switch/test_template.py +++ b/tests/components/switch/test_template.py @@ -8,6 +8,10 @@ Tests template switch. import homeassistant.core as ha import homeassistant.components.switch as switch +from homeassistant.const import ( + STATE_ON, + STATE_OFF) + class TestTemplateSwitch: """ Test the Template switch. """ @@ -19,7 +23,7 @@ class TestTemplateSwitch: """ Stop down stuff we started. """ self.hass.stop() - def test_template_state(self): + def test_template_state_text(self): assert switch.setup(self.hass, { 'switch': { 'platform': 'template', @@ -41,19 +45,67 @@ class TestTemplateSwitch: }) - state = self.hass.states.set('switch.test_state', 'On') + 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 == 'On' + assert state.state == STATE_ON - state = self.hass.states.set('switch.test_state', 'Off') + 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 == 'Off' + assert state.state == STATE_OFF + def test_template_state_boolean_on(self): + assert switch.setup(self.hass, { + 'switch': { + 'platform': 'template', + 'switches': { + 'test_template_switch': { + 'value_template': + "{{ 1 == 1 }}", + 'turn_on': { + 'service': 'switch.turn_on', + 'entity_id': 'switch.test_state' + }, + 'turn_off': { + 'service': 'switch.turn_off', + 'entity_id': 'switch.test_state' + }, + } + } + } + }) + + state = self.hass.states.get('switch.test_template_switch') + assert state.state == STATE_ON + + def test_template_state_boolean_off(self): + assert switch.setup(self.hass, { + 'switch': { + 'platform': 'template', + 'switches': { + 'test_template_switch': { + 'value_template': + "{{ 1 == 2 }}", + 'turn_on': { + 'service': 'switch.turn_on', + 'entity_id': 'switch.test_state' + }, + 'turn_off': { + 'service': 'switch.turn_off', + 'entity_id': 'switch.test_state' + }, + } + } + } + }) + + state = self.hass.states.get('switch.test_template_switch') + assert state.state == STATE_OFF + def test_template_syntax_error(self): assert switch.setup(self.hass, { 'switch': { @@ -75,7 +127,7 @@ class TestTemplateSwitch: } }) - state = self.hass.states.set('switch.test_state', 'On') + 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 == 'error'