mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Code quality tests Wake on Lan (#82048)
This commit is contained in:
parent
1c6b4967cf
commit
2ca61eef79
13
tests/components/wake_on_lan/conftest.py
Normal file
13
tests/components/wake_on_lan/conftest.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
"""Test fixtures for Wake on Lan."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_send_magic_packet() -> AsyncMock:
|
||||||
|
"""Mock magic packet."""
|
||||||
|
with patch("wakeonlan.send_magic_packet") as mock_send:
|
||||||
|
yield mock_send
|
@ -1,14 +1,17 @@
|
|||||||
"""Tests for Wake On LAN component."""
|
"""Tests for Wake On LAN component."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.wake_on_lan import DOMAIN, SERVICE_SEND_MAGIC_PACKET
|
from homeassistant.components.wake_on_lan import DOMAIN, SERVICE_SEND_MAGIC_PACKET
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
|
||||||
async def test_send_magic_packet(hass):
|
async def test_send_magic_packet(hass: HomeAssistant) -> None:
|
||||||
"""Test of send magic packet service call."""
|
"""Test of send magic packet service call."""
|
||||||
with patch("homeassistant.components.wake_on_lan.wakeonlan") as mocked_wakeonlan:
|
with patch("homeassistant.components.wake_on_lan.wakeonlan") as mocked_wakeonlan:
|
||||||
mac = "aa:bb:cc:dd:ee:ff"
|
mac = "aa:bb:cc:dd:ee:ff"
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
"""The tests for the wake on lan switch platform."""
|
"""The tests for the wake on lan switch platform."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
import pytest
|
from homeassistant.components import switch
|
||||||
|
|
||||||
import homeassistant.components.switch as switch
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
SERVICE_TURN_OFF,
|
SERVICE_TURN_OFF,
|
||||||
@ -12,19 +12,15 @@ from homeassistant.const import (
|
|||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import async_mock_service
|
from tests.common import async_mock_service
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
async def test_valid_hostname(
|
||||||
def mock_send_magic_packet():
|
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
|
||||||
"""Mock magic packet."""
|
) -> None:
|
||||||
with patch("wakeonlan.send_magic_packet") as mock_send:
|
|
||||||
yield mock_send
|
|
||||||
|
|
||||||
|
|
||||||
async def test_valid_hostname(hass):
|
|
||||||
"""Test with valid hostname."""
|
"""Test with valid hostname."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
@ -65,7 +61,9 @@ async def test_valid_hostname(hass):
|
|||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
|
|
||||||
async def test_broadcast_config_ip_and_port(hass, mock_send_magic_packet):
|
async def test_broadcast_config_ip_and_port(
|
||||||
|
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
|
||||||
|
) -> None:
|
||||||
"""Test with broadcast address and broadcast port config."""
|
"""Test with broadcast address and broadcast port config."""
|
||||||
mac = "00-01-02-03-04-05"
|
mac = "00-01-02-03-04-05"
|
||||||
broadcast_address = "255.255.255.255"
|
broadcast_address = "255.255.255.255"
|
||||||
@ -102,7 +100,9 @@ async def test_broadcast_config_ip_and_port(hass, mock_send_magic_packet):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_broadcast_config_ip(hass, mock_send_magic_packet):
|
async def test_broadcast_config_ip(
|
||||||
|
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
|
||||||
|
) -> None:
|
||||||
"""Test with only broadcast address."""
|
"""Test with only broadcast address."""
|
||||||
|
|
||||||
mac = "00-01-02-03-04-05"
|
mac = "00-01-02-03-04-05"
|
||||||
@ -136,7 +136,9 @@ async def test_broadcast_config_ip(hass, mock_send_magic_packet):
|
|||||||
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(hass, mock_send_magic_packet):
|
async def test_broadcast_config_port(
|
||||||
|
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
|
||||||
|
) -> None:
|
||||||
"""Test with only broadcast port config."""
|
"""Test with only broadcast port config."""
|
||||||
|
|
||||||
mac = "00-01-02-03-04-05"
|
mac = "00-01-02-03-04-05"
|
||||||
@ -164,7 +166,9 @@ async def test_broadcast_config_port(hass, mock_send_magic_packet):
|
|||||||
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(hass):
|
async def test_off_script(
|
||||||
|
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
|
||||||
|
) -> None:
|
||||||
"""Test with turn off script."""
|
"""Test with turn off script."""
|
||||||
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
@ -212,7 +216,9 @@ async def test_off_script(hass):
|
|||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_no_hostname_state(hass):
|
async def test_no_hostname_state(
|
||||||
|
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
|
||||||
|
) -> None:
|
||||||
"""Test that the state updates if we do not pass in a hostname."""
|
"""Test that the state updates if we do not pass in a hostname."""
|
||||||
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user