Skip to content
Snippets Groups Projects
Commit b4054add authored by Diefferson Koderer Môro's avatar Diefferson Koderer Môro Committed by Paulus Schoutsen
Browse files

Move imports in wake_on_lan component (#28100)

* Move imports in wake_on_lan component

* Fix tox tests
parent c8b28601
No related branches found
No related tags found
No related merge requests found
......@@ -128,3 +128,6 @@ monkeytype.sqlite3
# This is left behind by Azure Restore Cache
tmp_cache
# python-language-server / Rope
.ropeproject
......@@ -3,6 +3,7 @@ from functools import partial
import logging
import voluptuous as vol
import wakeonlan
from homeassistant.const import CONF_MAC
import homeassistant.helpers.config_validation as cv
......@@ -22,7 +23,6 @@ WAKE_ON_LAN_SEND_MAGIC_PACKET_SCHEMA = vol.Schema(
async def async_setup(hass, config):
"""Set up the wake on LAN component."""
import wakeonlan
async def send_magic_packet(call):
"""Send magic packet to wake up a device."""
......
......@@ -4,6 +4,7 @@ import platform
import subprocess as sp
import voluptuous as vol
import wakeonlan
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import CONF_HOST, CONF_NAME
......@@ -48,8 +49,6 @@ class WOLSwitch(SwitchDevice):
def __init__(self, hass, name, host, mac_address, off_action, broadcast_address):
"""Initialize the WOL switch."""
import wakeonlan
self._hass = hass
self._name = name
self._host = host
......@@ -57,7 +56,6 @@ class WOLSwitch(SwitchDevice):
self._broadcast_address = broadcast_address
self._off_script = Script(hass, off_action) if off_action else None
self._state = False
self._wol = wakeonlan
@property
def is_on(self):
......@@ -72,11 +70,11 @@ class WOLSwitch(SwitchDevice):
def turn_on(self, **kwargs):
"""Turn the device on."""
if self._broadcast_address:
self._wol.send_magic_packet(
wakeonlan.send_magic_packet(
self._mac_address, ip_address=self._broadcast_address
)
else:
self._wol.send_magic_packet(self._mac_address)
wakeonlan.send_magic_packet(self._mac_address)
def turn_off(self, **kwargs):
"""Turn the device off if an off action is present."""
......
"""Tests for Wake On LAN component."""
import asyncio
from unittest import mock
import pytest
import voluptuous as vol
from homeassistant.setup import async_setup_component
from homeassistant.components import wake_on_lan
from homeassistant.components.wake_on_lan import DOMAIN, SERVICE_SEND_MAGIC_PACKET
from homeassistant.setup import async_setup_component
@pytest.fixture
def mock_wakeonlan():
"""Mock mock_wakeonlan."""
module = mock.MagicMock()
with mock.patch.dict("sys.modules", {"wakeonlan": module}):
yield module
from tests.common import MockDependency
@asyncio.coroutine
def test_send_magic_packet(hass, caplog, mock_wakeonlan):
async def test_send_magic_packet(hass):
"""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
with pytest.raises(vol.Invalid):
yield from hass.services.async_call(
with MockDependency("wakeonlan") as mocked_wakeonlan:
mac = "aa:bb:cc:dd:ee:ff"
bc_ip = "192.168.255.255"
wake_on_lan.wakeonlan = mocked_wakeonlan
await async_setup_component(hass, DOMAIN, {})
await hass.services.async_call(
DOMAIN,
SERVICE_SEND_MAGIC_PACKET,
{"broadcast_address": bc_ip},
{"mac": mac, "broadcast_address": bc_ip},
blocking=True,
)
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]
assert len(mocked_wakeonlan.mock_calls) == 1
assert mocked_wakeonlan.mock_calls[-1][1][0] == mac
assert mocked_wakeonlan.mock_calls[-1][2]["ip_address"] == bc_ip
with pytest.raises(vol.Invalid):
await hass.services.async_call(
DOMAIN,
SERVICE_SEND_MAGIC_PACKET,
{"broadcast_address": bc_ip},
blocking=True,
)
assert len(mocked_wakeonlan.mock_calls) == 1
await hass.services.async_call(
DOMAIN, SERVICE_SEND_MAGIC_PACKET, {"mac": mac}, blocking=True
)
assert len(mocked_wakeonlan.mock_calls) == 2
assert mocked_wakeonlan.mock_calls[-1][1][0] == mac
assert not mocked_wakeonlan.mock_calls[-1][2]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment