core/tests/components/axis/test_switch.py

83 lines
2.3 KiB
Python
Raw Normal View History

"""Axis switch platform tests."""
from homeassistant.components import axis
import homeassistant.components.switch as switch
from homeassistant.setup import async_setup_component
2020-01-02 23:02:59 +00:00
from .test_device import NAME, setup_axis_integration
from tests.async_mock import Mock, call as mock_call
EVENTS = [
{
2019-07-31 19:25:30 +00:00
"operation": "Initialized",
"topic": "tns1:Device/Trigger/Relay",
"source": "RelayToken",
"source_idx": "0",
"type": "LogicalState",
"value": "inactive",
},
{
2019-07-31 19:25:30 +00:00
"operation": "Initialized",
"topic": "tns1:Device/Trigger/Relay",
"source": "RelayToken",
"source_idx": "1",
"type": "LogicalState",
"value": "active",
},
]
async def test_platform_manually_configured(hass):
"""Test that nothing happens when platform is manually configured."""
2019-07-31 19:25:30 +00:00
assert await async_setup_component(
hass, switch.DOMAIN, {"switch": {"platform": axis.DOMAIN}}
)
assert axis.DOMAIN not in hass.data
async def test_no_switches(hass):
"""Test that no output events in Axis results in no switch entities."""
2020-01-02 23:02:59 +00:00
await setup_axis_integration(hass)
2019-07-31 19:25:30 +00:00
assert not hass.states.async_entity_ids("switch")
async def test_switches(hass):
"""Test that switches are loaded properly."""
2020-01-02 23:02:59 +00:00
device = await setup_axis_integration(hass)
2019-07-31 19:25:30 +00:00
device.api.vapix.ports = {"0": Mock(), "1": Mock()}
device.api.vapix.ports["0"].name = "Doorbell"
device.api.vapix.ports["1"].name = ""
for event in EVENTS:
device.api.stream.event.manage_event(event)
await hass.async_block_till_done()
2020-01-02 23:02:59 +00:00
assert len(hass.states.async_entity_ids("switch")) == 2
2020-01-02 23:02:59 +00:00
relay_0 = hass.states.get(f"switch.{NAME}_doorbell")
2019-07-31 19:25:30 +00:00
assert relay_0.state == "off"
2020-01-02 23:02:59 +00:00
assert relay_0.name == f"{NAME} Doorbell"
2020-01-02 23:02:59 +00:00
relay_1 = hass.states.get(f"switch.{NAME}_relay_1")
2019-07-31 19:25:30 +00:00
assert relay_1.state == "on"
2020-01-02 23:02:59 +00:00
assert relay_1.name == f"{NAME} Relay 1"
2019-07-31 19:25:30 +00:00
device.api.vapix.ports["0"].action = Mock()
2019-07-31 19:25:30 +00:00
await hass.services.async_call(
2020-01-02 23:02:59 +00:00
"switch", "turn_on", {"entity_id": f"switch.{NAME}_doorbell"}, blocking=True
2019-07-31 19:25:30 +00:00
)
2019-07-31 19:25:30 +00:00
await hass.services.async_call(
2020-01-02 23:02:59 +00:00
"switch", "turn_off", {"entity_id": f"switch.{NAME}_doorbell"}, blocking=True
2019-07-31 19:25:30 +00:00
)
2019-07-31 19:25:30 +00:00
assert device.api.vapix.ports["0"].action.call_args_list == [
mock_call("/"),
mock_call("\\"),
]