Skip to content

Commit

Permalink
Use snapshot in Axis switch tests (#122680)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 authored Jul 30, 2024
1 parent 004ecce commit 70e368a
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 65 deletions.
189 changes: 189 additions & 0 deletions tests/components/axis/snapshots/test_switch.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# serializer version: 1
# name: test_switches_with_port_cgi[root.IOPort.I0.Configurable=yes\nroot.IOPort.I0.Direction=output\nroot.IOPort.I0.Output.Name=Doorbell\nroot.IOPort.I0.Output.Active=closed\nroot.IOPort.I1.Configurable=yes\nroot.IOPort.I1.Direction=output\nroot.IOPort.I1.Output.Name=\nroot.IOPort.I1.Output.Active=open\n][switch.home_doorbell-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.home_doorbell',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': 'Doorbell',
'platform': 'axis',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '00:40:8c:12:34:56-tns1:Device/Trigger/Relay-0',
'unit_of_measurement': None,
})
# ---
# name: test_switches_with_port_cgi[root.IOPort.I0.Configurable=yes\nroot.IOPort.I0.Direction=output\nroot.IOPort.I0.Output.Name=Doorbell\nroot.IOPort.I0.Output.Active=closed\nroot.IOPort.I1.Configurable=yes\nroot.IOPort.I1.Direction=output\nroot.IOPort.I1.Output.Name=\nroot.IOPort.I1.Output.Active=open\n][switch.home_doorbell-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'outlet',
'friendly_name': 'home Doorbell',
}),
'context': <ANY>,
'entity_id': 'switch.home_doorbell',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_switches_with_port_cgi[root.IOPort.I0.Configurable=yes\nroot.IOPort.I0.Direction=output\nroot.IOPort.I0.Output.Name=Doorbell\nroot.IOPort.I0.Output.Active=closed\nroot.IOPort.I1.Configurable=yes\nroot.IOPort.I1.Direction=output\nroot.IOPort.I1.Output.Name=\nroot.IOPort.I1.Output.Active=open\n][switch.home_relay_1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.home_relay_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': 'Relay 1',
'platform': 'axis',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '00:40:8c:12:34:56-tns1:Device/Trigger/Relay-1',
'unit_of_measurement': None,
})
# ---
# name: test_switches_with_port_cgi[root.IOPort.I0.Configurable=yes\nroot.IOPort.I0.Direction=output\nroot.IOPort.I0.Output.Name=Doorbell\nroot.IOPort.I0.Output.Active=closed\nroot.IOPort.I1.Configurable=yes\nroot.IOPort.I1.Direction=output\nroot.IOPort.I1.Output.Name=\nroot.IOPort.I1.Output.Active=open\n][switch.home_relay_1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'outlet',
'friendly_name': 'home Relay 1',
}),
'context': <ANY>,
'entity_id': 'switch.home_relay_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_switches_with_port_management[port_management_payload0-api_discovery_items0][switch.home_doorbell-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.home_doorbell',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': 'Doorbell',
'platform': 'axis',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '00:40:8c:12:34:56-tns1:Device/Trigger/Relay-0',
'unit_of_measurement': None,
})
# ---
# name: test_switches_with_port_management[port_management_payload0-api_discovery_items0][switch.home_doorbell-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'outlet',
'friendly_name': 'home Doorbell',
}),
'context': <ANY>,
'entity_id': 'switch.home_doorbell',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_switches_with_port_management[port_management_payload0-api_discovery_items0][switch.home_relay_1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.home_relay_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': 'Relay 1',
'platform': 'axis',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '00:40:8c:12:34:56-tns1:Device/Trigger/Relay-1',
'unit_of_measurement': None,
})
# ---
# name: test_switches_with_port_management[port_management_payload0-api_discovery_items0][switch.home_relay_1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'outlet',
'friendly_name': 'home Relay 1',
}),
'context': <ANY>,
'entity_id': 'switch.home_relay_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
127 changes: 62 additions & 65 deletions tests/components/axis/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

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
Expand All @@ -28,14 +32,48 @@
root.IOPort.I1.Output.Active=open
"""

PORT_MANAGEMENT_RESPONSE = {
"apiVersion": "1.0",
"method": "getPorts",
"context": CONTEXT,
"data": {
"numberOfPorts": 2,
"items": [
{
"port": "0",
"configurable": True,
"usage": "",
"name": "Doorbell",
"direction": "output",
"state": "open",
"normalState": "open",
},
{
"port": "1",
"configurable": True,
"usage": "",
"name": "",
"direction": "output",
"state": "open",
"normalState": "open",
},
],
},
}


@pytest.mark.parametrize("param_ports_payload", [PORT_DATA])
@pytest.mark.usefixtures("config_entry_setup")
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.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",
Expand All @@ -52,18 +90,10 @@ async def test_switches_with_port_cgi(
)
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,
Expand All @@ -83,44 +113,19 @@ async def test_switches_with_port_cgi(
mock_turn_off.assert_called_once_with("0")


PORT_MANAGEMENT_RESPONSE = {
"apiVersion": "1.0",
"method": "getPorts",
"context": CONTEXT,
"data": {
"numberOfPorts": 2,
"items": [
{
"port": "0",
"configurable": True,
"usage": "",
"name": "Doorbell",
"direction": "output",
"state": "open",
"normalState": "open",
},
{
"port": "1",
"configurable": True,
"usage": "",
"name": "",
"direction": "output",
"state": "open",
"normalState": "open",
},
],
},
}


@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(
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",
Expand All @@ -137,31 +142,10 @@ 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"

# 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

with patch("axis.interfaces.vapix.IoPortManagement.close") as mock_turn_on:
await hass.services.async_call(
SWITCH_DOMAIN,
Expand All @@ -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

0 comments on commit 70e368a

Please sign in to comment.