Fix unit tests for wake_on_lan (#97542)
parent
121fc7778d
commit
a8a3b6778e
|
@ -1,7 +1,8 @@
|
|||
"""Test fixtures for Wake on Lan."""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -11,3 +12,19 @@ def mock_send_magic_packet() -> AsyncMock:
|
|||
"""Mock magic packet."""
|
||||
with patch("wakeonlan.send_magic_packet") as mock_send:
|
||||
yield mock_send
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def subprocess_call_return_value() -> int | None:
|
||||
"""Return value for subprocess."""
|
||||
return 1
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_subprocess_call(
|
||||
subprocess_call_return_value: int,
|
||||
) -> Generator[None, None, MagicMock]:
|
||||
"""Mock magic packet."""
|
||||
with patch("homeassistant.components.wake_on_lan.switch.sp.call") as mock_sp:
|
||||
mock_sp.return_value = subprocess_call_return_value
|
||||
yield mock_sp
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
"""The tests for the wake on lan switch platform."""
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from homeassistant.components import switch
|
||||
|
@ -38,7 +37,7 @@ async def test_valid_hostname(
|
|||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
with patch.object(subprocess, "call", return_value=0):
|
||||
with patch("homeassistant.components.wake_on_lan.switch.sp.call", return_value=0):
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
|
@ -85,17 +84,16 @@ async def test_broadcast_config_ip_and_port(
|
|||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
with patch.object(subprocess, "call", return_value=0):
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_send_magic_packet.assert_called_with(
|
||||
mac, ip_address=broadcast_address, port=port
|
||||
)
|
||||
mock_send_magic_packet.assert_called_with(
|
||||
mac, ip_address=broadcast_address, port=port
|
||||
)
|
||||
|
||||
|
||||
async def test_broadcast_config_ip(
|
||||
|
@ -122,15 +120,14 @@ async def test_broadcast_config_ip(
|
|||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
with patch.object(subprocess, "call", return_value=0):
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_send_magic_packet.assert_called_with(mac, ip_address=broadcast_address)
|
||||
mock_send_magic_packet.assert_called_with(mac, ip_address=broadcast_address)
|
||||
|
||||
|
||||
async def test_broadcast_config_port(
|
||||
|
@ -151,15 +148,14 @@ async def test_broadcast_config_port(
|
|||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
with patch.object(subprocess, "call", return_value=0):
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_send_magic_packet.assert_called_with(mac, port=port)
|
||||
mock_send_magic_packet.assert_called_with(mac, port=port)
|
||||
|
||||
|
||||
async def test_off_script(
|
||||
|
@ -185,7 +181,7 @@ async def test_off_script(
|
|||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
with patch.object(subprocess, "call", return_value=0):
|
||||
with patch("homeassistant.components.wake_on_lan.switch.sp.call", return_value=0):
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
|
@ -197,7 +193,7 @@ async def test_off_script(
|
|||
assert state.state == STATE_ON
|
||||
assert len(calls) == 0
|
||||
|
||||
with patch.object(subprocess, "call", return_value=2):
|
||||
with patch("homeassistant.components.wake_on_lan.switch.sp.call", return_value=1):
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
|
@ -230,23 +226,22 @@ async def test_no_hostname_state(
|
|||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
with patch.object(subprocess, "call", return_value=0):
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_ON
|
||||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: "switch.wake_on_lan"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
state = hass.states.get("switch.wake_on_lan")
|
||||
assert state.state == STATE_OFF
|
||||
|
|
Loading…
Reference in New Issue