Code quality tests Wake on Lan (#82048)

This commit is contained in:
G Johansson 2022-11-18 16:27:20 +01:00 committed by GitHub
parent 1c6b4967cf
commit 2ca61eef79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 18 deletions

View 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

View file

@ -1,14 +1,17 @@
"""Tests for Wake On LAN component."""
from __future__ import annotations
from unittest.mock import patch
import pytest
import voluptuous as vol
from homeassistant.components.wake_on_lan import DOMAIN, SERVICE_SEND_MAGIC_PACKET
from homeassistant.core import HomeAssistant
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."""
with patch("homeassistant.components.wake_on_lan.wakeonlan") as mocked_wakeonlan:
mac = "aa:bb:cc:dd:ee:ff"

View file

@ -1,10 +1,10 @@
"""The tests for the wake on lan switch platform."""
from __future__ import annotations
import subprocess
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
import pytest
import homeassistant.components.switch as switch
from homeassistant.components import switch
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
@ -12,19 +12,15 @@ from homeassistant.const import (
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import async_mock_service
@pytest.fixture(autouse=True)
def mock_send_magic_packet():
"""Mock magic packet."""
with patch("wakeonlan.send_magic_packet") as mock_send:
yield mock_send
async def test_valid_hostname(hass):
async def test_valid_hostname(
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
) -> None:
"""Test with valid hostname."""
assert await async_setup_component(
hass,
@ -65,7 +61,9 @@ async def test_valid_hostname(hass):
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."""
mac = "00-01-02-03-04-05"
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."""
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)
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."""
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)
async def test_off_script(hass):
async def test_off_script(
hass: HomeAssistant, mock_send_magic_packet: AsyncMock
) -> None:
"""Test with turn off script."""
assert await async_setup_component(
@ -212,7 +216,9 @@ async def test_off_script(hass):
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."""
assert await async_setup_component(