This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

@@ -1,7 +1,7 @@
"""The tests for the Command line Binary sensor platform."""
import unittest
from homeassistant.const import (STATE_ON, STATE_OFF)
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.components.command_line import binary_sensor as command_line
from homeassistant.helpers import template
@@ -21,12 +21,13 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
def test_setup(self):
"""Test sensor setup."""
config = {'name': 'Test',
'command': 'echo 1',
'payload_on': '1',
'payload_off': '0',
'command_timeout': 15
}
config = {
"name": "Test",
"command": "echo 1",
"payload_on": "1",
"payload_off": "0",
"command_timeout": 15,
}
devices = []
@@ -40,24 +41,31 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
assert 1 == len(devices)
entity = devices[0]
entity.update()
assert 'Test' == entity.name
assert "Test" == entity.name
assert STATE_ON == entity.state
def test_template(self):
"""Test setting the state with a template."""
data = command_line.CommandSensorData(self.hass, 'echo 10', 15)
data = command_line.CommandSensorData(self.hass, "echo 10", 15)
entity = command_line.CommandBinarySensor(
self.hass, data, 'test', None, '1.0', '0',
template.Template('{{ value | multiply(0.1) }}', self.hass))
self.hass,
data,
"test",
None,
"1.0",
"0",
template.Template("{{ value | multiply(0.1) }}", self.hass),
)
entity.update()
assert STATE_ON == entity.state
def test_sensor_off(self):
"""Test setting the state with a template."""
data = command_line.CommandSensorData(self.hass, 'echo 0', 15)
data = command_line.CommandSensorData(self.hass, "echo 0", 15)
entity = command_line.CommandBinarySensor(
self.hass, data, 'test', None, '1', '0', None)
self.hass, data, "test", None, "1", "0", None
)
entity.update()
assert STATE_OFF == entity.state
+43 -33
View File
@@ -8,16 +8,26 @@ import pytest
from homeassistant.components.cover import DOMAIN
import homeassistant.components.command_line.cover as cmd_rs
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_CLOSE_COVER, SERVICE_OPEN_COVER,
SERVICE_STOP_COVER)
ATTR_ENTITY_ID,
SERVICE_CLOSE_COVER,
SERVICE_OPEN_COVER,
SERVICE_STOP_COVER,
)
from homeassistant.setup import async_setup_component
@pytest.fixture
def rs(hass):
"""Return CommandCover instance."""
return cmd_rs.CommandCover(hass, 'foo', 'command_open', 'command_close',
'command_stop', 'command_state', None)
return cmd_rs.CommandCover(
hass,
"foo",
"command_open",
"command_close",
"command_stop",
"command_state",
None,
)
def test_should_poll_new(rs):
@@ -29,47 +39,47 @@ def test_should_poll_new(rs):
def test_query_state_value(rs):
"""Test with state value."""
with mock.patch('subprocess.check_output') as mock_run:
mock_run.return_value = b' foo bar '
result = rs._query_state_value('runme')
assert 'foo bar' == result
with mock.patch("subprocess.check_output") as mock_run:
mock_run.return_value = b" foo bar "
result = rs._query_state_value("runme")
assert "foo bar" == result
assert mock_run.call_count == 1
assert mock_run.call_args == mock.call('runme', shell=True)
assert mock_run.call_args == mock.call("runme", shell=True)
async def test_state_value(hass):
"""Test with state value."""
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'cover_status')
path = os.path.join(tempdirname, "cover_status")
test_cover = {
'command_state': 'cat {}'.format(path),
'command_open': 'echo 1 > {}'.format(path),
'command_close': 'echo 1 > {}'.format(path),
'command_stop': 'echo 0 > {}'.format(path),
'value_template': '{{ value }}'
"command_state": "cat {}".format(path),
"command_open": "echo 1 > {}".format(path),
"command_close": "echo 1 > {}".format(path),
"command_stop": "echo 0 > {}".format(path),
"value_template": "{{ value }}",
}
assert await async_setup_component(hass, DOMAIN, {
'cover': {
'platform': 'command_line',
'covers': {
'test': test_cover
}
}
}) is True
assert (
await async_setup_component(
hass,
DOMAIN,
{"cover": {"platform": "command_line", "covers": {"test": test_cover}}},
)
is True
)
assert 'unknown' == hass.states.get('cover.test').state
assert "unknown" == hass.states.get("cover.test").state
await hass.services.async_call(
DOMAIN, SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: 'cover.test'}, blocking=True)
assert 'open' == hass.states.get('cover.test').state
DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True
)
assert "open" == hass.states.get("cover.test").state
await hass.services.async_call(
DOMAIN, SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: 'cover.test'}, blocking=True)
assert 'open' == hass.states.get('cover.test').state
DOMAIN, SERVICE_CLOSE_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True
)
assert "open" == hass.states.get("cover.test").state
await hass.services.async_call(
DOMAIN, SERVICE_STOP_COVER,
{ATTR_ENTITY_ID: 'cover.test'}, blocking=True)
assert 'closed' == hass.states.get('cover.test').state
DOMAIN, SERVICE_STOP_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True
)
assert "closed" == hass.states.get("cover.test").state
+42 -32
View File
@@ -23,22 +23,22 @@ class TestCommandLine(unittest.TestCase):
def test_setup(self):
"""Test setup."""
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, 'notify', {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat); exit 1', }
})
assert setup_component(
self.hass,
"notify",
{
"notify": {
"name": "test",
"platform": "command_line",
"command": "echo $(cat); exit 1",
}
},
)
assert handle_config[notify.DOMAIN]
def test_bad_config(self):
"""Test set up the platform with bad/missing configuration."""
config = {
notify.DOMAIN: {
'name': 'test',
'platform': 'command_line',
}
}
config = {notify.DOMAIN: {"name": "test", "platform": "command_line"}}
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
@@ -46,38 +46,48 @@ class TestCommandLine(unittest.TestCase):
def test_command_line_output(self):
"""Test the command line output."""
with tempfile.TemporaryDirectory() as tempdirname:
filename = os.path.join(tempdirname, 'message.txt')
message = 'one, two, testing, testing'
filename = os.path.join(tempdirname, "message.txt")
message = "one, two, testing, testing"
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat) > {}'.format(filename)
}
})
assert setup_component(
self.hass,
notify.DOMAIN,
{
"notify": {
"name": "test",
"platform": "command_line",
"command": "echo $(cat) > {}".format(filename),
}
},
)
assert handle_config[notify.DOMAIN]
assert self.hass.services.call(
'notify', 'test', {'message': message}, blocking=True)
"notify", "test", {"message": message}, blocking=True
)
with open(filename) as fil:
# the echo command adds a line break
assert fil.read() == "{}\n".format(message)
@patch('homeassistant.components.command_line.notify._LOGGER.error')
@patch("homeassistant.components.command_line.notify._LOGGER.error")
def test_error_for_none_zero_exit_code(self, mock_error):
"""Test if an error is logged for non zero exit codes."""
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat); exit 1'
}
})
assert setup_component(
self.hass,
notify.DOMAIN,
{
"notify": {
"name": "test",
"platform": "command_line",
"command": "echo $(cat); exit 1",
}
},
)
assert handle_config[notify.DOMAIN]
assert self.hass.services.call('notify', 'test', {'message': 'error'},
blocking=True)
assert self.hass.services.call(
"notify", "test", {"message": "error"}, blocking=True
)
assert 1 == mock_error.call_count
+78 -71
View File
@@ -24,11 +24,12 @@ class TestCommandSensorSensor(unittest.TestCase):
def test_setup(self):
"""Test sensor setup."""
config = {'name': 'Test',
'unit_of_measurement': 'in',
'command': 'echo 5',
'command_timeout': 15
}
config = {
"name": "Test",
"unit_of_measurement": "in",
"command": "echo 5",
"command_timeout": 15,
}
devices = []
def add_dev_callback(devs, update):
@@ -41,27 +42,31 @@ class TestCommandSensorSensor(unittest.TestCase):
assert 1 == len(devices)
entity = devices[0]
entity.update()
assert 'Test' == entity.name
assert 'in' == entity.unit_of_measurement
assert '5' == entity.state
assert "Test" == entity.name
assert "in" == entity.unit_of_measurement
assert "5" == entity.state
def test_template(self):
"""Test command sensor with template."""
data = command_line.CommandSensorData(self.hass, 'echo 50', 15)
data = command_line.CommandSensorData(self.hass, "echo 50", 15)
entity = command_line.CommandSensor(
self.hass, data, 'test', 'in',
Template('{{ value | multiply(0.1) }}', self.hass), [])
self.hass,
data,
"test",
"in",
Template("{{ value | multiply(0.1) }}", self.hass),
[],
)
entity.update()
assert 5 == float(entity.state)
def test_template_render(self):
"""Ensure command with templates get rendered properly."""
self.hass.states.set('sensor.test_state', 'Works')
self.hass.states.set("sensor.test_state", "Works")
data = command_line.CommandSensorData(
self.hass,
'echo {{ states.sensor.test_state.state }}', 15
self.hass, "echo {{ states.sensor.test_state.state }}", 15
)
data.update()
@@ -69,7 +74,7 @@ class TestCommandSensorSensor(unittest.TestCase):
def test_bad_command(self):
"""Test bad command."""
data = command_line.CommandSensorData(self.hass, 'asdfasdf', 15)
data = command_line.CommandSensorData(self.hass, "asdfasdf", 15)
data.update()
assert data.value is None
@@ -78,58 +83,54 @@ class TestCommandSensorSensor(unittest.TestCase):
"""Test attributes get extracted from a JSON result."""
data = command_line.CommandSensorData(
self.hass,
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
15
(
'echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'
),
15,
)
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
None, None, ['key',
'another_key',
'key_three'])
self.sensor = command_line.CommandSensor(
self.hass, data, "test", None, None, ["key", "another_key", "key_three"]
)
self.sensor.update()
assert 'some_json_value' == \
self.sensor.device_state_attributes['key']
assert 'another_json_value' == \
self.sensor.device_state_attributes['another_key']
assert 'value_three' == \
self.sensor.device_state_attributes['key_three']
assert "some_json_value" == self.sensor.device_state_attributes["key"]
assert (
"another_json_value" == self.sensor.device_state_attributes["another_key"]
)
assert "value_three" == self.sensor.device_state_attributes["key_three"]
@patch('homeassistant.components.command_line.sensor._LOGGER')
@patch("homeassistant.components.command_line.sensor._LOGGER")
def test_update_with_json_attrs_no_data(self, mock_logger):
"""Test attributes when no JSON result fetched."""
data = command_line.CommandSensorData(
self.hass,
'echo ', 15
data = command_line.CommandSensorData(self.hass, "echo ", 15)
self.sensor = command_line.CommandSensor(
self.hass, data, "test", None, None, ["key"]
)
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
None, None, ['key'])
self.sensor.update()
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@patch('homeassistant.components.command_line.sensor._LOGGER')
@patch("homeassistant.components.command_line.sensor._LOGGER")
def test_update_with_json_attrs_not_dict(self, mock_logger):
"""Test attributes get extracted from a JSON result."""
data = command_line.CommandSensorData(
self.hass,
'echo [1, 2, 3]', 15
data = command_line.CommandSensorData(self.hass, "echo [1, 2, 3]", 15)
self.sensor = command_line.CommandSensor(
self.hass, data, "test", None, None, ["key"]
)
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
None, None, ['key'])
self.sensor.update()
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@patch('homeassistant.components.command_line.sensor._LOGGER')
@patch("homeassistant.components.command_line.sensor._LOGGER")
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
"""Test attributes get extracted from a JSON result."""
data = command_line.CommandSensorData(
self.hass,
'echo This is text rather than JSON data.', 15
self.hass, "echo This is text rather than JSON data.", 15
)
self.sensor = command_line.CommandSensor(
self.hass, data, "test", None, None, ["key"]
)
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
None, None, ['key'])
self.sensor.update()
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@@ -138,40 +139,46 @@ class TestCommandSensorSensor(unittest.TestCase):
"""Test attributes get extracted from a JSON result."""
data = command_line.CommandSensorData(
self.hass,
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
15
(
'echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'
),
15,
)
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
None, None, ['key',
'another_key',
'key_three',
'special_key'])
self.sensor = command_line.CommandSensor(
self.hass,
data,
"test",
None,
None,
["key", "another_key", "key_three", "special_key"],
)
self.sensor.update()
assert 'some_json_value' == \
self.sensor.device_state_attributes['key']
assert 'another_json_value' == \
self.sensor.device_state_attributes['another_key']
assert 'value_three' == \
self.sensor.device_state_attributes['key_three']
assert not ('special_key' in self.sensor.device_state_attributes)
assert "some_json_value" == self.sensor.device_state_attributes["key"]
assert (
"another_json_value" == self.sensor.device_state_attributes["another_key"]
)
assert "value_three" == self.sensor.device_state_attributes["key_three"]
assert not ("special_key" in self.sensor.device_state_attributes)
def test_update_with_unnecessary_json_attrs(self):
"""Test attributes get extracted from a JSON result."""
data = command_line.CommandSensorData(
self.hass,
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
15
(
'echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'
),
15,
)
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
None, None, ['key',
'another_key'])
self.sensor = command_line.CommandSensor(
self.hass, data, "test", None, None, ["key", "another_key"]
)
self.sensor.update()
assert 'some_json_value' == \
self.sensor.device_state_attributes['key']
assert 'another_json_value' == \
self.sensor.device_state_attributes['another_key']
assert not ('key_three' in self.sensor.device_state_attributes)
assert "some_json_value" == self.sensor.device_state_attributes["key"]
assert (
"another_json_value" == self.sensor.device_state_attributes["another_key"]
)
assert not ("key_three" in self.sensor.device_state_attributes)
+78 -70
View File
@@ -28,136 +28,144 @@ class TestCommandSwitch(unittest.TestCase):
def test_state_none(self):
"""Test with none state."""
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'switch_status')
path = os.path.join(tempdirname, "switch_status")
test_switch = {
'command_on': 'echo 1 > {}'.format(path),
'command_off': 'echo 0 > {}'.format(path),
"command_on": "echo 1 > {}".format(path),
"command_off": "echo 0 > {}".format(path),
}
assert setup_component(self.hass, switch.DOMAIN, {
'switch': {
'platform': 'command_line',
'switches': {
'test': test_switch
assert setup_component(
self.hass,
switch.DOMAIN,
{
"switch": {
"platform": "command_line",
"switches": {"test": test_switch},
}
}
})
},
)
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert 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')
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" }}'
"command_state": "cat {}".format(path),
"command_on": "echo 1 > {}".format(path),
"command_off": "echo 0 > {}".format(path),
"value_template": '{{ value=="1" }}',
}
assert setup_component(self.hass, switch.DOMAIN, {
'switch': {
'platform': 'command_line',
'switches': {
'test': test_switch
assert setup_component(
self.hass,
switch.DOMAIN,
{
"switch": {
"platform": "command_line",
"switches": {"test": test_switch},
}
}
})
},
)
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert 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'})
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" }}'
"command_state": "cat {}".format(path),
"command_on": "echo '{}' > {}".format(oncmd, path),
"command_off": "echo '{}' > {}".format(offcmd, path),
"value_template": '{{ value_json.status=="ok" }}',
}
assert setup_component(self.hass, switch.DOMAIN, {
'switch': {
'platform': 'command_line',
'switches': {
'test': test_switch
assert setup_component(
self.hass,
switch.DOMAIN,
{
"switch": {
"platform": "command_line",
"switches": {"test": test_switch},
}
}
})
},
)
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert 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')
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),
"command_state": "cat {}".format(path),
"command_on": "echo 1 > {}".format(path),
"command_off": "echo 0 > {}".format(path),
}
assert setup_component(self.hass, switch.DOMAIN, {
'switch': {
'platform': 'command_line',
'switches': {
'test': test_switch
assert setup_component(
self.hass,
switch.DOMAIN,
{
"switch": {
"platform": "command_line",
"switches": {"test": test_switch},
}
}
})
},
)
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
state = self.hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
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):
@@ -178,7 +186,7 @@ class TestCommandSwitch(unittest.TestCase):
assert no_state_device.assumed_state
# Set state command
init_args[-2] = 'cat {}'
init_args[-2] = "cat {}"
state_device = command_line.CommandSwitch(*init_args)
assert not state_device.assumed_state
@@ -196,5 +204,5 @@ class TestCommandSwitch(unittest.TestCase):
]
test_switch = command_line.CommandSwitch(*init_args)
assert test_switch.entity_id == 'switch.test_device_name'
assert test_switch.name == 'Test friendly name!'
assert test_switch.entity_id == "switch.test_device_name"
assert test_switch.name == "Test friendly name!"