2016-03-09 09:25:50 +00:00
|
|
|
"""the tests for the Command line switch platform."""
|
2015-12-28 03:49:55 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF
|
|
|
|
import homeassistant.components.switch as switch
|
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2015-12-28 03:49:55 +00:00
|
|
|
|
|
|
|
class TestCommandSwitch(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the command switch."""
|
2015-12-28 03:49:55 +00:00
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-12-28 03:49:55 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_state_none(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with none state."""
|
2015-12-28 03:49:55 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
|
|
test_switch = {
|
|
|
|
'oncmd': 'echo 1 > {}'.format(path),
|
|
|
|
'offcmd': 'echo 0 > {}'.format(path),
|
|
|
|
}
|
|
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.turn_on(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.turn_off(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
def test_state_value(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with state value."""
|
2015-12-28 03:49:55 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
|
|
test_switch = {
|
|
|
|
'statecmd': 'cat {}'.format(path),
|
|
|
|
'oncmd': 'echo 1 > {}'.format(path),
|
|
|
|
'offcmd': 'echo 0 > {}'.format(path),
|
|
|
|
'value_template': '{{ value=="1" }}'
|
|
|
|
}
|
|
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.turn_on(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
2016-02-13 13:19:11 +00:00
|
|
|
|
2015-12-28 03:49:55 +00:00
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.turn_off(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
def test_state_json_value(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with state JSON value."""
|
2015-12-28 03:49:55 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
|
|
oncmd = json.dumps({'status': 'ok'})
|
|
|
|
offcmd = json.dumps({'status': 'nope'})
|
|
|
|
test_switch = {
|
|
|
|
'statecmd': 'cat {}'.format(path),
|
|
|
|
'oncmd': 'echo \'{}\' > {}'.format(oncmd, path),
|
|
|
|
'offcmd': 'echo \'{}\' > {}'.format(offcmd, path),
|
|
|
|
'value_template': '{{ value_json.status=="ok" }}'
|
|
|
|
}
|
|
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.turn_on(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.turn_off(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
def test_state_code(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with state code."""
|
2015-12-28 03:49:55 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
|
|
test_switch = {
|
|
|
|
'statecmd': 'cat {}'.format(path),
|
|
|
|
'oncmd': 'echo 1 > {}'.format(path),
|
|
|
|
'offcmd': 'echo 0 > {}'.format(path),
|
|
|
|
}
|
|
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.turn_on(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.turn_off(self.hass, 'switch.test')
|
|
|
|
self.hass.pool.block_till_done()
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|