mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Refactor, support template logic values, add tests.
This commit is contained in:
parent
a955f3db08
commit
9a9dbcfaea
@ -14,6 +14,8 @@ from homeassistant.components.switch import SwitchDevice
|
|||||||
|
|
||||||
from homeassistant.core import EVENT_STATE_CHANGED
|
from homeassistant.core import EVENT_STATE_CHANGED
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
STATE_ON,
|
||||||
|
STATE_OFF,
|
||||||
ATTR_FRIENDLY_NAME,
|
ATTR_FRIENDLY_NAME,
|
||||||
CONF_VALUE_TEMPLATE)
|
CONF_VALUE_TEMPLATE)
|
||||||
|
|
||||||
@ -33,6 +35,8 @@ STATE_ERROR = 'error'
|
|||||||
ON_ACTION = 'turn_on'
|
ON_ACTION = 'turn_on'
|
||||||
OFF_ACTION = 'turn_off'
|
OFF_ACTION = 'turn_off'
|
||||||
|
|
||||||
|
STATE_TRUE = 'True'
|
||||||
|
STATE_FALSE = 'False'
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
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)
|
self.hass.bus.listen(EVENT_STATE_CHANGED, _update_callback)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
""" Returns the name of the device. """
|
""" Returns the name of the device. """
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
""" Returns the state of the device. """
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
""" Tells Home Assistant not to poll this entity. """
|
""" Tells Home Assistant not to poll this entity. """
|
||||||
@ -137,16 +137,24 @@ class SwitchTemplate(SwitchDevice):
|
|||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
_LOGGER.error("TURN OFF not implemented yet")
|
_LOGGER.error("TURN OFF not implemented yet")
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
""" Tells Home Assistant not to poll this entity. """
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
""" True if device is on. """
|
""" True if device is on. """
|
||||||
_LOGGER.error("IS ON CALLED %s", self._state)
|
return self._state == STATE_TRUE or self._state == STATE_ON
|
||||||
return 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):
|
def update(self):
|
||||||
try:
|
try:
|
||||||
|
@ -8,6 +8,10 @@ Tests template switch.
|
|||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
import homeassistant.components.switch as switch
|
import homeassistant.components.switch as switch
|
||||||
|
|
||||||
|
from homeassistant.const import (
|
||||||
|
STATE_ON,
|
||||||
|
STATE_OFF)
|
||||||
|
|
||||||
|
|
||||||
class TestTemplateSwitch:
|
class TestTemplateSwitch:
|
||||||
""" Test the Template switch. """
|
""" Test the Template switch. """
|
||||||
@ -19,7 +23,7 @@ class TestTemplateSwitch:
|
|||||||
""" Stop down stuff we started. """
|
""" Stop down stuff we started. """
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
def test_template_state(self):
|
def test_template_state_text(self):
|
||||||
assert switch.setup(self.hass, {
|
assert switch.setup(self.hass, {
|
||||||
'switch': {
|
'switch': {
|
||||||
'platform': 'template',
|
'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()
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test_template_switch')
|
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()
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test_template_switch')
|
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):
|
def test_template_syntax_error(self):
|
||||||
assert switch.setup(self.hass, {
|
assert switch.setup(self.hass, {
|
||||||
'switch': {
|
'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()
|
self.hass.pool.block_till_done()
|
||||||
state = self.hass.states.get('switch.test_template_switch')
|
state = self.hass.states.get('switch.test_template_switch')
|
||||||
assert state.state == 'error'
|
assert state.state == 'error'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user