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
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
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
|
2016-03-15 12:32:33 +00:00
|
|
|
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
|
|
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup 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:
|
|
|
|
path = os.path.join(tempdirname, 'switch_status')
|
|
|
|
test_switch = {
|
2016-09-02 14:09:09 +00:00
|
|
|
'command_on': 'echo 1 > {}'.format(path),
|
|
|
|
'command_off': 'echo 0 > {}'.format(path),
|
2015-12-28 03:49:55 +00:00
|
|
|
}
|
2016-09-17 17:29:58 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
2015-12-28 03:49:55 +00:00
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
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 = {
|
2016-09-02 14:09:09 +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
|
|
|
'value_template': '{{ value=="1" }}'
|
|
|
|
}
|
2016-09-17 17:29:58 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
2015-12-28 03:49:55 +00:00
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
2015-12-28 03:49:55 +00:00
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
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 = {
|
2016-09-02 14:09:09 +00:00
|
|
|
'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" }}'
|
|
|
|
}
|
2016-09-17 17:29:58 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
2015-12-28 03:49:55 +00:00
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
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 = {
|
2016-09-02 14:09:09 +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
|
|
|
}
|
2016-09-17 17:29:58 +00:00
|
|
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
2015-12-28 03:49:55 +00:00
|
|
|
'switch': {
|
2016-02-27 23:56:35 +00:00
|
|
|
'platform': 'command_line',
|
2015-12-28 03:49:55 +00:00
|
|
|
'switches': {
|
|
|
|
'test': test_switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_OFF, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(STATE_ON, state.state)
|
|
|
|
|
|
|
|
switch.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
|
|
|
|
|
|
|
state = self.hass.states.get('switch.test')
|
|
|
|
self.assertEqual(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)
|
2016-03-15 12:32:33 +00:00
|
|
|
self.assertTrue(no_state_device.assumed_state)
|
|
|
|
|
|
|
|
# Set state command
|
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[-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)
|
2016-03-15 12:32:33 +00:00
|
|
|
self.assertFalse(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)
|
|
|
|
self.assertEqual(test_switch.entity_id, 'switch.test_device_name')
|
|
|
|
self.assertEqual(test_switch.name, 'Test friendly name!')
|