core/tests/components/command_line/test_switch.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
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
import homeassistant.components.command_line.switch 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!'