hass-core/tests/components/test_wake_on_lan.py
Eugenio Panadero 821d01f82c New service send_magic_packet with new component wake_on_lan (#8397)
* New service `send_magic_packet` in new component `wake_on_lan`

* fix

* Unit tests for new component wake_on_lan

* Add wakeonlan to tests requirements

* remove wakeonlan from tests requirements and remake the component tests

* remove wakeonlan from tests requirements

* link domain and service names to component

* fix mocking in test_setup_component

* send_magic_packet as coroutine, better use of mock_calls in tests

* fix imports

* review changes

* better async calls

* Update test_wake_on_lan.py
2017-07-10 18:37:51 -07:00

47 lines
1.5 KiB
Python

"""Tests for Wake On LAN component."""
import asyncio
from unittest import mock
import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components.wake_on_lan import (
DOMAIN, SERVICE_SEND_MAGIC_PACKET)
@pytest.fixture
def mock_wakeonlan():
"""Mock mock_wakeonlan."""
module = mock.MagicMock()
with mock.patch.dict('sys.modules', {
'wakeonlan': module,
}):
yield module
@asyncio.coroutine
def test_send_magic_packet(hass, caplog, mock_wakeonlan):
"""Test of send magic packet service call."""
mac = "aa:bb:cc:dd:ee:ff"
bc_ip = "192.168.255.255"
yield from async_setup_component(hass, DOMAIN, {})
yield from hass.services.async_call(
DOMAIN, SERVICE_SEND_MAGIC_PACKET,
{"mac": mac, "broadcast_address": bc_ip}, blocking=True)
assert len(mock_wakeonlan.mock_calls) == 1
assert mock_wakeonlan.mock_calls[-1][1][0] == mac
assert mock_wakeonlan.mock_calls[-1][2]['ip_address'] == bc_ip
yield from hass.services.async_call(
DOMAIN, SERVICE_SEND_MAGIC_PACKET,
{"broadcast_address": bc_ip}, blocking=True)
assert 'ERROR' in caplog.text
assert len(mock_wakeonlan.mock_calls) == 1
yield from hass.services.async_call(
DOMAIN, SERVICE_SEND_MAGIC_PACKET, {"mac": mac}, blocking=True)
assert len(mock_wakeonlan.mock_calls) == 2
assert mock_wakeonlan.mock_calls[-1][1][0] == mac
assert not mock_wakeonlan.mock_calls[-1][2]