* Add event loop to the core * Add block_till_done to HA core object * Fix some tests * Linting core * Fix statemachine tests * Core test fixes * fix block_till_done to wait for loop and queue to empty * fix test_core for passing, and correct start/stop/block_till_done * Fix remote tests * Fix tests: block_till_done * Fix linting * Fix more tests * Fix final linting * Fix remote test * remove unnecessary import * reduce sleep to avoid slowing down the tests excessively * fix remaining tests to wait for non-threadsafe operations * Add async_ doc strings for event loop / coroutine info * Fix command line test to block for the right timeout * Fix py3.4.2 loop var access * Fix SERVICE_CALL_LIMIT being in effect for other tests * Fix lint errors * Fix lint error with proper placement * Fix slave start to not start a timer * Add asyncio compatible listeners. * Increase min Python version to 3.4.2 * Move async backports to util * Add backported async tests * Fix linting * Simplify Python version check * Fix lint * Remove unneeded try/except and queue listener appproriately. * Fix tuple vs. list unorderable error on version compare. * Fix version tests
176 lines
6.2 KiB
Python
176 lines
6.2 KiB
Python
"""the tests for the Command line switch platform."""
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF
|
|
import homeassistant.components.switch as switch
|
|
import homeassistant.components.switch.command_line as command_line
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
class TestCommandSwitch(unittest.TestCase):
|
|
"""Test the command switch."""
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
"""Setup things to be run when tests are started."""
|
|
self.hass = get_test_home_assistant()
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
|
"""Stop everything that was started."""
|
|
self.hass.stop()
|
|
|
|
def test_state_none(self):
|
|
"""Test with none state."""
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
test_switch = {
|
|
'command_on': 'echo 1 > {}'.format(path),
|
|
'command_off': 'echo 0 > {}'.format(path),
|
|
}
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
'switch': {
|
|
'platform': 'command_line',
|
|
'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.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.block_till_done()
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
def test_state_value(self):
|
|
"""Test with state value."""
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
test_switch = {
|
|
'command_state': 'cat {}'.format(path),
|
|
'command_on': 'echo 1 > {}'.format(path),
|
|
'command_off': 'echo 0 > {}'.format(path),
|
|
'value_template': '{{ value=="1" }}'
|
|
}
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
'switch': {
|
|
'platform': 'command_line',
|
|
'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.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.block_till_done()
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
def test_state_json_value(self):
|
|
"""Test with state JSON value."""
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
oncmd = json.dumps({'status': 'ok'})
|
|
offcmd = json.dumps({'status': 'nope'})
|
|
test_switch = {
|
|
'command_state': 'cat {}'.format(path),
|
|
'command_on': 'echo \'{}\' > {}'.format(oncmd, path),
|
|
'command_off': 'echo \'{}\' > {}'.format(offcmd, path),
|
|
'value_template': '{{ value_json.status=="ok" }}'
|
|
}
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
'switch': {
|
|
'platform': 'command_line',
|
|
'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.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.block_till_done()
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
def test_state_code(self):
|
|
"""Test with state code."""
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
test_switch = {
|
|
'command_state': 'cat {}'.format(path),
|
|
'command_on': 'echo 1 > {}'.format(path),
|
|
'command_off': 'echo 0 > {}'.format(path),
|
|
}
|
|
self.assertTrue(switch.setup(self.hass, {
|
|
'switch': {
|
|
'platform': 'command_line',
|
|
'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.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.block_till_done()
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
def test_assumed_state_should_be_true_if_command_state_is_false(self):
|
|
"""Test with state value."""
|
|
self.hass = get_test_home_assistant()
|
|
|
|
# Set state command to false
|
|
statecmd = False
|
|
|
|
no_state_device = command_line.CommandSwitch(self.hass, "Test", "echo",
|
|
"echo", statecmd, None)
|
|
self.assertTrue(no_state_device.assumed_state)
|
|
|
|
# Set state command
|
|
statecmd = 'cat {}'
|
|
|
|
state_device = command_line.CommandSwitch(self.hass, "Test", "echo",
|
|
"echo", statecmd, None)
|
|
self.assertFalse(state_device.assumed_state)
|