1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Adapt Axis integration to library refactoring (#110898)

* Adapt Axis integration to library refactoring

* Bump axis to v49
This commit is contained in:
Robert Svensson
2024-02-28 14:36:32 +01:00
committed by GitHub
parent 2b3630b054
commit c478b1416c
17 changed files with 403 additions and 199 deletions

View File

@@ -1,6 +1,7 @@
"""Axis switch platform tests."""
from unittest.mock import AsyncMock
from unittest.mock import patch
from axis.vapix.models.api import CONTEXT
import pytest
from homeassistant.components.axis.const import DOMAIN as AXIS_DOMAIN
@@ -32,18 +33,22 @@ async def test_no_switches(hass: HomeAssistant, setup_config_entry) -> None:
assert not hass.states.async_entity_ids(SWITCH_DOMAIN)
PORT_DATA = """root.IOPort.I0.Configurable=yes
root.IOPort.I0.Direction=output
root.IOPort.I0.Output.Name=Doorbell
root.IOPort.I0.Output.Active=closed
root.IOPort.I1.Configurable=yes
root.IOPort.I1.Direction=output
root.IOPort.I1.Output.Name=
root.IOPort.I1.Output.Active=open
"""
@pytest.mark.parametrize("param_ports_payload", [PORT_DATA])
async def test_switches_with_port_cgi(
hass: HomeAssistant, setup_config_entry, mock_rtsp_event
) -> None:
"""Test that switches are loaded properly using port.cgi."""
device = hass.data[AXIS_DOMAIN][setup_config_entry.entry_id]
device.api.vapix.ports = {"0": AsyncMock(), "1": AsyncMock()}
device.api.vapix.ports["0"].name = "Doorbell"
device.api.vapix.ports["0"].open = AsyncMock()
device.api.vapix.ports["0"].close = AsyncMock()
device.api.vapix.ports["1"].name = ""
mock_rtsp_event(
topic="tns1:Device/Trigger/Relay",
data_type="LogicalState",
@@ -72,36 +77,61 @@ async def test_switches_with_port_cgi(
assert relay_0.state == STATE_OFF
assert relay_0.name == f"{NAME} Doorbell"
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
device.api.vapix.ports["0"].close.assert_called_once()
with patch("axis.vapix.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")
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
device.api.vapix.ports["0"].open.assert_called_once()
with patch("axis.vapix.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",
"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])
async def test_switches_with_port_management(
hass: HomeAssistant, setup_config_entry, mock_rtsp_event
) -> None:
"""Test that switches are loaded properly using port management."""
device = hass.data[AXIS_DOMAIN][setup_config_entry.entry_id]
device.api.vapix.ports = {"0": AsyncMock(), "1": AsyncMock()}
device.api.vapix.ports["0"].name = "Doorbell"
device.api.vapix.ports["0"].open = AsyncMock()
device.api.vapix.ports["0"].close = AsyncMock()
device.api.vapix.ports["1"].name = ""
mock_rtsp_event(
topic="tns1:Device/Trigger/Relay",
data_type="LogicalState",
@@ -143,18 +173,20 @@ async def test_switches_with_port_management(
assert hass.states.get(f"{SWITCH_DOMAIN}.{NAME}_relay_1").state == STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
device.api.vapix.ports["0"].close.assert_called_once()
with patch("axis.vapix.vapix.IoPortManagement.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")
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
device.api.vapix.ports["0"].open.assert_called_once()
with patch("axis.vapix.vapix.IoPortManagement.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")