core/tests/components/switch/test_command_line.py

201 lines
6.7 KiB
Python
Raw Normal View History

"""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.setup import setup_component
2015-12-28 03:49:55 +00:00
from homeassistant.const import STATE_ON, STATE_OFF
import homeassistant.components.switch as switch
import homeassistant.components.switch.command_line as command_line
2015-12-28 03:49:55 +00:00
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
from tests.components.switch import common
2016-02-14 23:08:23 +00:00
2015-12-28 03:49:55 +00:00
# pylint: disable=invalid-name
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):
2018-08-19 20:29:08 +00:00
"""Set up 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):
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 = {
'command_on': 'echo 1 > {}'.format(path),
'command_off': 'echo 0 > {}'.format(path),
2015-12-28 03:49:55 +00:00
}
assert setup_component(self.hass, switch.DOMAIN, {
2015-12-28 03:49:55 +00:00
'switch': {
'platform': 'command_line',
2015-12-28 03:49:55 +00:00
'switches': {
'test': test_switch
}
}
})
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_OFF == state.state
2015-12-28 03:49:55 +00:00
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_ON == state.state
2015-12-28 03:49:55 +00:00
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_OFF == state.state
2015-12-28 03:49:55 +00:00
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 = {
'command_state': 'cat {}'.format(path),
'command_on': 'echo 1 > {}'.format(path),
'command_off': 'echo 0 > {}'.format(path),
2015-12-28 03:49:55 +00:00
'value_template': '{{ value=="1" }}'
}
assert setup_component(self.hass, switch.DOMAIN, {
2015-12-28 03:49:55 +00:00
'switch': {
'platform': 'command_line',
2015-12-28 03:49:55 +00:00
'switches': {
'test': test_switch
}
}
})
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_OFF == state.state
2015-12-28 03:49:55 +00:00
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_ON == state.state
2015-12-28 03:49:55 +00:00
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_OFF == state.state
2015-12-28 03:49:55 +00:00
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 = {
'command_state': 'cat {}'.format(path),
'command_on': 'echo \'{}\' > {}'.format(oncmd, path),
'command_off': 'echo \'{}\' > {}'.format(offcmd, path),
2015-12-28 03:49:55 +00:00
'value_template': '{{ value_json.status=="ok" }}'
}
assert setup_component(self.hass, switch.DOMAIN, {
2015-12-28 03:49:55 +00:00
'switch': {
'platform': 'command_line',
2015-12-28 03:49:55 +00:00
'switches': {
'test': test_switch
}
}
})
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_OFF == state.state
2015-12-28 03:49:55 +00:00
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_ON == state.state
2015-12-28 03:49:55 +00:00
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_OFF == state.state
2015-12-28 03:49:55 +00:00
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 = {
'command_state': 'cat {}'.format(path),
'command_on': 'echo 1 > {}'.format(path),
'command_off': 'echo 0 > {}'.format(path),
2015-12-28 03:49:55 +00:00
}
assert setup_component(self.hass, switch.DOMAIN, {
2015-12-28 03:49:55 +00:00
'switch': {
'platform': 'command_line',
2015-12-28 03:49:55 +00:00
'switches': {
'test': test_switch
}
}
})
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_OFF == state.state
2015-12-28 03:49:55 +00:00
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_ON == state.state
2015-12-28 03:49:55 +00:00
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
2015-12-28 03:49:55 +00:00
state = self.hass.states.get('switch.test')
assert STATE_ON == state.state
def test_assumed_state_should_be_true_if_command_state_is_none(self):
"""Test with state value."""
# args: hass, device_name, friendly_name, command_on, command_off,
# command_state, value_template
init_args = [
self.hass,
"test_device_name",
"Test friendly name!",
"echo 'on command'",
"echo 'off command'",
None,
None,
]
no_state_device = command_line.CommandSwitch(*init_args)
assert no_state_device.assumed_state
# Set state command
init_args[-2] = 'cat {}'
state_device = command_line.CommandSwitch(*init_args)
assert not state_device.assumed_state
def test_entity_id_set_correctly(self):
2016-12-28 18:04:59 +00:00
"""Test that entity_id is set correctly from object_id."""
init_args = [
self.hass,
"test_device_name",
"Test friendly name!",
"echo 'on command'",
"echo 'off command'",
False,
None,
]
test_switch = command_line.CommandSwitch(*init_args)
assert test_switch.entity_id == 'switch.test_device_name'
assert test_switch.name == 'Test friendly name!'