From 2622cf2e5352c52085bd68a4cdb39ab9086d5d10 Mon Sep 17 00:00:00 2001 From: pavoni Date: Fri, 5 Feb 2016 11:18:50 +0000 Subject: [PATCH] Use available, remove state, improve true,false tests. --- homeassistant/components/switch/template.py | 26 ++++++++++----------- tests/components/switch/test_template.py | 2 +- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/switch/template.py b/homeassistant/components/switch/template.py index 12a53ccbcc2..589768db9bd 100644 --- a/homeassistant/components/switch/template.py +++ b/homeassistant/components/switch/template.py @@ -35,9 +35,6 @@ 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): @@ -139,26 +136,27 @@ class SwitchTemplate(SwitchDevice): @property def is_on(self): """ True if device is on. """ - return self._state == STATE_TRUE or self._state == STATE_ON + return self._value.lower() == 'true' or self._value == STATE_ON @property def is_off(self): """ True if device is off. """ - return self._state == STATE_FALSE or self._state == STATE_OFF + return self._value.lower() == 'false' or self._value == 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 available(self): + """Return True if entity is available.""" + return self.is_on or self.is_off def update(self): """ Updates the state from the template. """ try: - self._state = template.render(self.hass, self._template) + self._value = template.render(self.hass, self._template) + if not self.available: + _LOGGER.error( + "`%s` is not a switch state, setting %s to unavailable", + self._value, self.entity_id) + except TemplateError as ex: - self._state = STATE_ERROR + self._value = STATE_ERROR _LOGGER.error(ex) diff --git a/tests/components/switch/test_template.py b/tests/components/switch/test_template.py index 108f9a24f85..aeffe9ff194 100644 --- a/tests/components/switch/test_template.py +++ b/tests/components/switch/test_template.py @@ -139,7 +139,7 @@ class TestTemplateSwitch: 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' + assert state.state == 'unavailable' def test_invalid_name_does_not_create(self): assert switch.setup(self.hass, {