Skip to content

Commit

Permalink
Fix unit tests for wake_on_lan (home-assistant#97542)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh authored Jul 31, 2023
1 parent 121fc77 commit a8a3b67
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
19 changes: 18 additions & 1 deletion tests/components/wake_on_lan/conftest.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
89 changes: 42 additions & 47 deletions tests/components/wake_on_lan/test_switch.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit a8a3b67

Please sign in to comment.