mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Improve command_line switch tests (#40749)
This commit is contained in:
parent
fc0344399b
commit
27350f41c9
@ -2,27 +2,20 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
|
||||||
|
|
||||||
import homeassistant.components.command_line.switch as command_line
|
import homeassistant.components.command_line.switch as command_line
|
||||||
import homeassistant.components.switch as switch
|
import homeassistant.components.switch as switch
|
||||||
from homeassistant.const import STATE_OFF, STATE_ON
|
from homeassistant.const import (
|
||||||
from homeassistant.setup import setup_component
|
ATTR_ENTITY_ID,
|
||||||
|
SERVICE_TURN_OFF,
|
||||||
from tests.common import get_test_home_assistant
|
SERVICE_TURN_ON,
|
||||||
from tests.components.switch import common
|
STATE_OFF,
|
||||||
|
STATE_ON,
|
||||||
|
)
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=invalid-name
|
async def test_state_none(hass):
|
||||||
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):
|
|
||||||
"""Test with none state."""
|
"""Test with none state."""
|
||||||
with tempfile.TemporaryDirectory() as tempdirname:
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
path = os.path.join(tempdirname, "switch_status")
|
path = os.path.join(tempdirname, "switch_status")
|
||||||
@ -30,8 +23,8 @@ class TestCommandSwitch(unittest.TestCase):
|
|||||||
"command_on": f"echo 1 > {path}",
|
"command_on": f"echo 1 > {path}",
|
||||||
"command_off": f"echo 0 > {path}",
|
"command_off": f"echo 0 > {path}",
|
||||||
}
|
}
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass,
|
hass,
|
||||||
switch.DOMAIN,
|
switch.DOMAIN,
|
||||||
{
|
{
|
||||||
"switch": {
|
"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
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
common.turn_on(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
common.turn_off(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
def test_state_value(self):
|
|
||||||
|
async def test_state_value(hass):
|
||||||
"""Test with state value."""
|
"""Test with state value."""
|
||||||
with tempfile.TemporaryDirectory() as tempdirname:
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
path = os.path.join(tempdirname, "switch_status")
|
path = os.path.join(tempdirname, "switch_status")
|
||||||
@ -67,8 +69,8 @@ class TestCommandSwitch(unittest.TestCase):
|
|||||||
"command_off": f"echo 0 > {path}",
|
"command_off": f"echo 0 > {path}",
|
||||||
"value_template": '{{ value=="1" }}',
|
"value_template": '{{ value=="1" }}',
|
||||||
}
|
}
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass,
|
hass,
|
||||||
switch.DOMAIN,
|
switch.DOMAIN,
|
||||||
{
|
{
|
||||||
"switch": {
|
"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
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
common.turn_on(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
common.turn_off(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
def test_state_json_value(self):
|
|
||||||
|
async def test_state_json_value(hass):
|
||||||
"""Test with state JSON value."""
|
"""Test with state JSON value."""
|
||||||
with tempfile.TemporaryDirectory() as tempdirname:
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
path = os.path.join(tempdirname, "switch_status")
|
path = os.path.join(tempdirname, "switch_status")
|
||||||
@ -106,8 +117,8 @@ class TestCommandSwitch(unittest.TestCase):
|
|||||||
"command_off": f"echo '{offcmd}' > {path}",
|
"command_off": f"echo '{offcmd}' > {path}",
|
||||||
"value_template": '{{ value_json.status=="ok" }}',
|
"value_template": '{{ value_json.status=="ok" }}',
|
||||||
}
|
}
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass,
|
hass,
|
||||||
switch.DOMAIN,
|
switch.DOMAIN,
|
||||||
{
|
{
|
||||||
"switch": {
|
"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
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
common.turn_on(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
common.turn_off(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
def test_state_code(self):
|
|
||||||
|
async def test_state_code(hass):
|
||||||
"""Test with state code."""
|
"""Test with state code."""
|
||||||
with tempfile.TemporaryDirectory() as tempdirname:
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
path = os.path.join(tempdirname, "switch_status")
|
path = os.path.join(tempdirname, "switch_status")
|
||||||
@ -142,8 +162,8 @@ class TestCommandSwitch(unittest.TestCase):
|
|||||||
"command_on": f"echo 1 > {path}",
|
"command_on": f"echo 1 > {path}",
|
||||||
"command_off": f"echo 0 > {path}",
|
"command_off": f"echo 0 > {path}",
|
||||||
}
|
}
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass,
|
hass,
|
||||||
switch.DOMAIN,
|
switch.DOMAIN,
|
||||||
{
|
{
|
||||||
"switch": {
|
"switch": {
|
||||||
@ -152,28 +172,38 @@ 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
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
common.turn_on(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
common.turn_off(self.hass, "switch.test")
|
await hass.services.async_call(
|
||||||
self.hass.block_till_done()
|
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
|
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."""
|
"""Test with state value."""
|
||||||
# args: hass, device_name, friendly_name, command_on, command_off,
|
# args: hass, device_name, friendly_name, command_on, command_off,
|
||||||
# command_state, value_template
|
# command_state, value_template
|
||||||
init_args = [
|
init_args = [
|
||||||
self.hass,
|
hass,
|
||||||
"test_device_name",
|
"test_device_name",
|
||||||
"Test friendly name!",
|
"Test friendly name!",
|
||||||
"echo 'on command'",
|
"echo 'on command'",
|
||||||
@ -192,10 +222,11 @@ class TestCommandSwitch(unittest.TestCase):
|
|||||||
state_device = command_line.CommandSwitch(*init_args)
|
state_device = command_line.CommandSwitch(*init_args)
|
||||||
assert not state_device.assumed_state
|
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."""
|
"""Test that entity_id is set correctly from object_id."""
|
||||||
init_args = [
|
init_args = [
|
||||||
self.hass,
|
hass,
|
||||||
"test_device_name",
|
"test_device_name",
|
||||||
"Test friendly name!",
|
"Test friendly name!",
|
||||||
"echo 'on command'",
|
"echo 'on command'",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user