2016-11-18 22:05:03 +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
|
|
|
|
|
2019-03-19 06:07:39 +00:00
|
|
|
import homeassistant.components.command_line.switch as command_line
|
2019-12-08 16:55:57 +00:00
|
|
|
import homeassistant.components.switch as switch
|
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
|
|
|
from homeassistant.setup import setup_component
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
2018-09-27 21:13:11 +00:00
|
|
|
from tests.components.switch import common
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2016-11-18 22:05:03 +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
|
|
|
|
2016-11-18 22:05:03 +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
|
|
|
|
2016-11-18 22:05:03 +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:
|
2019-07-31 19:25:30 +00:00
|
|
|
path = os.path.join(tempdirname, "switch_status")
|
2015-12-28 03:49:55 +00:00
|
|
|
test_switch = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"command_on": "echo 1 > {}".format(path),
|
|
|
|
"command_off": "echo 0 > {}".format(path),
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
switch.DOMAIN,
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "command_line",
|
|
|
|
"switches": {"test": test_switch},
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_OFF == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_on(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_ON == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_off(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
path = os.path.join(tempdirname, "switch_status")
|
2015-12-28 03:49:55 +00:00
|
|
|
test_switch = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"command_state": "cat {}".format(path),
|
|
|
|
"command_on": "echo 1 > {}".format(path),
|
|
|
|
"command_off": "echo 0 > {}".format(path),
|
|
|
|
"value_template": '{{ value=="1" }}',
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
switch.DOMAIN,
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "command_line",
|
|
|
|
"switches": {"test": test_switch},
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_OFF == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_on(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-13 13:19:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_ON == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_off(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
path = os.path.join(tempdirname, "switch_status")
|
|
|
|
oncmd = json.dumps({"status": "ok"})
|
|
|
|
offcmd = json.dumps({"status": "nope"})
|
2015-12-28 03:49:55 +00:00
|
|
|
test_switch = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"command_state": "cat {}".format(path),
|
|
|
|
"command_on": "echo '{}' > {}".format(oncmd, path),
|
|
|
|
"command_off": "echo '{}' > {}".format(offcmd, path),
|
|
|
|
"value_template": '{{ value_json.status=="ok" }}',
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
switch.DOMAIN,
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "command_line",
|
|
|
|
"switches": {"test": test_switch},
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_OFF == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_on(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_ON == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_off(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
path = os.path.join(tempdirname, "switch_status")
|
2015-12-28 03:49:55 +00:00
|
|
|
test_switch = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"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
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
switch.DOMAIN,
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "command_line",
|
|
|
|
"switches": {"test": test_switch},
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_OFF == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_on(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_ON == state.state
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
common.turn_off(self.hass, "switch.test")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-12-28 03:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("switch.test")
|
2018-10-24 10:10:05 +00:00
|
|
|
assert STATE_ON == state.state
|
2016-03-15 12:32:33 +00:00
|
|
|
|
2017-01-27 05:41:30 +00:00
|
|
|
def test_assumed_state_should_be_true_if_command_state_is_none(self):
|
2016-03-15 12:32:33 +00:00
|
|
|
"""Test with state value."""
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
# args: hass, device_name, friendly_name, command_on, command_off,
|
|
|
|
# command_state, value_template
|
|
|
|
init_args = [
|
2016-11-18 22:05:03 +00:00
|
|
|
self.hass,
|
|
|
|
"test_device_name",
|
|
|
|
"Test friendly name!",
|
|
|
|
"echo 'on command'",
|
|
|
|
"echo 'off command'",
|
2017-01-27 05:41:30 +00:00
|
|
|
None,
|
2016-11-18 22:05:03 +00:00
|
|
|
None,
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
no_state_device = command_line.CommandSwitch(*init_args)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert no_state_device.assumed_state
|
2016-03-15 12:32:33 +00:00
|
|
|
|
|
|
|
# Set state command
|
2019-07-31 19:25:30 +00:00
|
|
|
init_args[-2] = "cat {}"
|
2016-03-15 12:32:33 +00:00
|
|
|
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
state_device = command_line.CommandSwitch(*init_args)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not state_device.assumed_state
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
|
|
|
|
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."""
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
init_args = [
|
2016-11-18 22:05:03 +00:00
|
|
|
self.hass,
|
|
|
|
"test_device_name",
|
|
|
|
"Test friendly name!",
|
|
|
|
"echo 'on command'",
|
|
|
|
"echo 'off command'",
|
|
|
|
False,
|
|
|
|
None,
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
test_switch = command_line.CommandSwitch(*init_args)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert test_switch.entity_id == "switch.test_device_name"
|
|
|
|
assert test_switch.name == "Test friendly name!"
|