Improve command_line switch tests (#40749)

This commit is contained in:
Franck Nijhof 2020-09-30 13:29:43 +02:00 committed by GitHub
parent fc0344399b
commit 27350f41c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,27 +2,20 @@
import json
import os
import tempfile
import unittest
import homeassistant.components.command_line.switch as command_line
import homeassistant.components.switch as switch
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant
from tests.components.switch import common
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
)
from homeassistant.setup import async_setup_component
# pylint: disable=invalid-name
class TestCommandSwitch(unittest.TestCase):
"""Test the command switch."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.addCleanup(self.hass.stop)
def test_state_none(self):
async def test_state_none(hass):
"""Test with none state."""
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, "switch_status")
@ -30,8 +23,8 @@ class TestCommandSwitch(unittest.TestCase):
"command_on": f"echo 1 > {path}",
"command_off": f"echo 0 > {path}",
}
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
switch.DOMAIN,
{
"switch": {
@ -40,24 +33,33 @@ class TestCommandSwitch(unittest.TestCase):
}
},
)
self.hass.block_till_done()
await hass.async_block_till_done()
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_OFF == state.state
def test_state_value(self):
async def test_state_value(hass):
"""Test with state value."""
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, "switch_status")
@ -67,8 +69,8 @@ class TestCommandSwitch(unittest.TestCase):
"command_off": f"echo 0 > {path}",
"value_template": '{{ value=="1" }}',
}
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
switch.DOMAIN,
{
"switch": {
@ -77,24 +79,33 @@ class TestCommandSwitch(unittest.TestCase):
}
},
)
self.hass.block_till_done()
await hass.async_block_till_done()
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_OFF == state.state
def test_state_json_value(self):
async def test_state_json_value(hass):
"""Test with state JSON value."""
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, "switch_status")
@ -106,8 +117,8 @@ class TestCommandSwitch(unittest.TestCase):
"command_off": f"echo '{offcmd}' > {path}",
"value_template": '{{ value_json.status=="ok" }}',
}
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
switch.DOMAIN,
{
"switch": {
@ -116,24 +127,33 @@ class TestCommandSwitch(unittest.TestCase):
}
},
)
self.hass.block_till_done()
await hass.async_block_till_done()
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_OFF == state.state
def test_state_code(self):
async def test_state_code(hass):
"""Test with state code."""
with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, "switch_status")
@ -142,8 +162,8 @@ class TestCommandSwitch(unittest.TestCase):
"command_on": f"echo 1 > {path}",
"command_off": f"echo 0 > {path}",
}
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
switch.DOMAIN,
{
"switch": {
@ -152,28 +172,38 @@ class TestCommandSwitch(unittest.TestCase):
}
},
)
self.hass.block_till_done()
state = self.hass.states.get("switch.test")
await hass.async_block_till_done()
state = hass.states.get("switch.test")
assert STATE_OFF == state.state
common.turn_on(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_ON == state.state
common.turn_off(self.hass, "switch.test")
self.hass.block_till_done()
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.test"},
blocking=True,
)
state = self.hass.states.get("switch.test")
state = hass.states.get("switch.test")
assert STATE_ON == state.state
def test_assumed_state_should_be_true_if_command_state_is_none(self):
def test_assumed_state_should_be_true_if_command_state_is_none(hass):
"""Test with state value."""
# args: hass, device_name, friendly_name, command_on, command_off,
# command_state, value_template
init_args = [
self.hass,
hass,
"test_device_name",
"Test friendly name!",
"echo 'on command'",
@ -192,10 +222,11 @@ class TestCommandSwitch(unittest.TestCase):
state_device = command_line.CommandSwitch(*init_args)
assert not state_device.assumed_state
def test_entity_id_set_correctly(self):
def test_entity_id_set_correctly(hass):
"""Test that entity_id is set correctly from object_id."""
init_args = [
self.hass,
hass,
"test_device_name",
"Test friendly name!",
"echo 'on command'",