mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 04:50:05 +00:00
Use snapshot in Axis switch tests (#122680)
This commit is contained in:
@@ -4,20 +4,24 @@ from unittest.mock import patch
|
||||
|
||||
from axis.models.api import CONTEXT
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .conftest import RtspEventMock
|
||||
from .conftest import ConfigEntryFactoryType, RtspEventMock
|
||||
from .const import API_DISCOVERY_PORT_MANAGEMENT, NAME
|
||||
|
||||
from tests.common import snapshot_platform
|
||||
|
||||
PORT_DATA = """root.IOPort.I0.Configurable=yes
|
||||
root.IOPort.I0.Direction=output
|
||||
root.IOPort.I0.Output.Name=Doorbell
|
||||
@@ -28,61 +32,6 @@ root.IOPort.I1.Output.Name=
|
||||
root.IOPort.I1.Output.Active=open
|
||||
"""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("param_ports_payload", [PORT_DATA])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_switches_with_port_cgi(
|
||||
hass: HomeAssistant,
|
||||
mock_rtsp_event: RtspEventMock,
|
||||
) -> None:
|
||||
"""Test that switches are loaded properly using port.cgi."""
|
||||
mock_rtsp_event(
|
||||
topic="tns1:Device/Trigger/Relay",
|
||||
data_type="LogicalState",
|
||||
data_value="inactive",
|
||||
source_name="RelayToken",
|
||||
source_idx="0",
|
||||
)
|
||||
mock_rtsp_event(
|
||||
topic="tns1:Device/Trigger/Relay",
|
||||
data_type="LogicalState",
|
||||
data_value="active",
|
||||
source_name="RelayToken",
|
||||
source_idx="1",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
relay_1 = hass.states.get(f"{SWITCH_DOMAIN}.{NAME}_relay_1")
|
||||
assert relay_1.state == STATE_ON
|
||||
assert relay_1.name == f"{NAME} Relay 1"
|
||||
|
||||
entity_id = f"{SWITCH_DOMAIN}.{NAME}_doorbell"
|
||||
|
||||
relay_0 = hass.states.get(entity_id)
|
||||
assert relay_0.state == STATE_OFF
|
||||
assert relay_0.name == f"{NAME} Doorbell"
|
||||
|
||||
with patch("axis.interfaces.vapix.Ports.close") as mock_turn_on:
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
mock_turn_on.assert_called_once_with("0")
|
||||
|
||||
with patch("axis.interfaces.vapix.Ports.open") as mock_turn_off:
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
mock_turn_off.assert_called_once_with("0")
|
||||
|
||||
|
||||
PORT_MANAGEMENT_RESPONSE = {
|
||||
"apiVersion": "1.0",
|
||||
"method": "getPorts",
|
||||
@@ -113,14 +62,18 @@ PORT_MANAGEMENT_RESPONSE = {
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("api_discovery_items", [API_DISCOVERY_PORT_MANAGEMENT])
|
||||
@pytest.mark.parametrize("port_management_payload", [PORT_MANAGEMENT_RESPONSE])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_switches_with_port_management(
|
||||
@pytest.mark.parametrize("param_ports_payload", [PORT_DATA])
|
||||
async def test_switches_with_port_cgi(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
config_entry_factory: ConfigEntryFactoryType,
|
||||
mock_rtsp_event: RtspEventMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test that switches are loaded properly using port management."""
|
||||
"""Test that switches are loaded properly using port.cgi."""
|
||||
with patch("homeassistant.components.axis.PLATFORMS", [Platform.SWITCH]):
|
||||
config_entry = await config_entry_factory()
|
||||
|
||||
mock_rtsp_event(
|
||||
topic="tns1:Device/Trigger/Relay",
|
||||
data_type="LogicalState",
|
||||
@@ -137,30 +90,61 @@ async def test_switches_with_port_management(
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
relay_1 = hass.states.get(f"{SWITCH_DOMAIN}.{NAME}_relay_1")
|
||||
assert relay_1.state == STATE_ON
|
||||
assert relay_1.name == f"{NAME} Relay 1"
|
||||
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
||||
|
||||
entity_id = f"{SWITCH_DOMAIN}.{NAME}_doorbell"
|
||||
|
||||
relay_0 = hass.states.get(entity_id)
|
||||
assert relay_0.state == STATE_OFF
|
||||
assert relay_0.name == f"{NAME} Doorbell"
|
||||
with patch("axis.interfaces.vapix.Ports.close") as mock_turn_on:
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
mock_turn_on.assert_called_once_with("0")
|
||||
|
||||
# State update
|
||||
with patch("axis.interfaces.vapix.Ports.open") as mock_turn_off:
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
mock_turn_off.assert_called_once_with("0")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("api_discovery_items", [API_DISCOVERY_PORT_MANAGEMENT])
|
||||
@pytest.mark.parametrize("port_management_payload", [PORT_MANAGEMENT_RESPONSE])
|
||||
async def test_switches_with_port_management(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
config_entry_factory: ConfigEntryFactoryType,
|
||||
mock_rtsp_event: RtspEventMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test that switches are loaded properly using port management."""
|
||||
with patch("homeassistant.components.axis.PLATFORMS", [Platform.SWITCH]):
|
||||
config_entry = await config_entry_factory()
|
||||
|
||||
mock_rtsp_event(
|
||||
topic="tns1:Device/Trigger/Relay",
|
||||
data_type="LogicalState",
|
||||
data_value="inactive",
|
||||
source_name="RelayToken",
|
||||
source_idx="0",
|
||||
)
|
||||
mock_rtsp_event(
|
||||
topic="tns1:Device/Trigger/Relay",
|
||||
data_type="LogicalState",
|
||||
data_value="active",
|
||||
source_name="RelayToken",
|
||||
source_idx="0",
|
||||
source_idx="1",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(f"{SWITCH_DOMAIN}.{NAME}_relay_1").state == STATE_ON
|
||||
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
||||
|
||||
entity_id = f"{SWITCH_DOMAIN}.{NAME}_doorbell"
|
||||
|
||||
with patch("axis.interfaces.vapix.IoPortManagement.close") as mock_turn_on:
|
||||
await hass.services.async_call(
|
||||
@@ -179,3 +163,16 @@ async def test_switches_with_port_management(
|
||||
blocking=True,
|
||||
)
|
||||
mock_turn_off.assert_called_once_with("0")
|
||||
|
||||
# State update
|
||||
|
||||
mock_rtsp_event(
|
||||
topic="tns1:Device/Trigger/Relay",
|
||||
data_type="LogicalState",
|
||||
data_value="active",
|
||||
source_name="RelayToken",
|
||||
source_idx="0",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(f"{SWITCH_DOMAIN}.{NAME}_relay_1").state == STATE_ON
|
||||
|
||||
Reference in New Issue
Block a user