Move imports in wake_on_lan component (#28100)

* Move imports in wake_on_lan component

* Fix tox tests
This commit is contained in:
Diefferson Koderer Môro 2019-10-23 06:14:52 +00:00 committed by Paulus Schoutsen
parent c8b2860167
commit b4054add61
4 changed files with 37 additions and 42 deletions

3
.gitignore vendored
View File

@ -128,3 +128,6 @@ monkeytype.sqlite3
# This is left behind by Azure Restore Cache # This is left behind by Azure Restore Cache
tmp_cache tmp_cache
# python-language-server / Rope
.ropeproject

View File

@ -3,6 +3,7 @@ from functools import partial
import logging import logging
import voluptuous as vol import voluptuous as vol
import wakeonlan
from homeassistant.const import CONF_MAC from homeassistant.const import CONF_MAC
import homeassistant.helpers.config_validation as cv 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): async def async_setup(hass, config):
"""Set up the wake on LAN component.""" """Set up the wake on LAN component."""
import wakeonlan
async def send_magic_packet(call): async def send_magic_packet(call):
"""Send magic packet to wake up a device.""" """Send magic packet to wake up a device."""

View File

@ -4,6 +4,7 @@ import platform
import subprocess as sp import subprocess as sp
import voluptuous as vol import voluptuous as vol
import wakeonlan
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import CONF_HOST, CONF_NAME 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): def __init__(self, hass, name, host, mac_address, off_action, broadcast_address):
"""Initialize the WOL switch.""" """Initialize the WOL switch."""
import wakeonlan
self._hass = hass self._hass = hass
self._name = name self._name = name
self._host = host self._host = host
@ -57,7 +56,6 @@ class WOLSwitch(SwitchDevice):
self._broadcast_address = broadcast_address self._broadcast_address = broadcast_address
self._off_script = Script(hass, off_action) if off_action else None self._off_script = Script(hass, off_action) if off_action else None
self._state = False self._state = False
self._wol = wakeonlan
@property @property
def is_on(self): def is_on(self):
@ -72,11 +70,11 @@ class WOLSwitch(SwitchDevice):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the device on.""" """Turn the device on."""
if self._broadcast_address: if self._broadcast_address:
self._wol.send_magic_packet( wakeonlan.send_magic_packet(
self._mac_address, ip_address=self._broadcast_address self._mac_address, ip_address=self._broadcast_address
) )
else: else:
self._wol.send_magic_packet(self._mac_address) wakeonlan.send_magic_packet(self._mac_address)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the device off if an off action is present.""" """Turn the device off if an off action is present."""

View File

@ -1,52 +1,46 @@
"""Tests for Wake On LAN component.""" """Tests for Wake On LAN component."""
import asyncio
from unittest import mock
import pytest import pytest
import voluptuous as vol 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.components.wake_on_lan import DOMAIN, SERVICE_SEND_MAGIC_PACKET
from homeassistant.setup import async_setup_component
from tests.common import MockDependency
@pytest.fixture async def test_send_magic_packet(hass):
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.""" """Test of send magic packet service call."""
mac = "aa:bb:cc:dd:ee:ff" with MockDependency("wakeonlan") as mocked_wakeonlan:
bc_ip = "192.168.255.255" mac = "aa:bb:cc:dd:ee:ff"
bc_ip = "192.168.255.255"
yield from async_setup_component(hass, DOMAIN, {}) wake_on_lan.wakeonlan = mocked_wakeonlan
yield from hass.services.async_call( await async_setup_component(hass, DOMAIN, {})
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): await hass.services.async_call(
yield from hass.services.async_call(
DOMAIN, DOMAIN,
SERVICE_SEND_MAGIC_PACKET, SERVICE_SEND_MAGIC_PACKET,
{"broadcast_address": bc_ip}, {"mac": mac, "broadcast_address": bc_ip},
blocking=True, blocking=True,
) )
assert len(mock_wakeonlan.mock_calls) == 1 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
yield from hass.services.async_call( with pytest.raises(vol.Invalid):
DOMAIN, SERVICE_SEND_MAGIC_PACKET, {"mac": mac}, blocking=True await hass.services.async_call(
) DOMAIN,
assert len(mock_wakeonlan.mock_calls) == 2 SERVICE_SEND_MAGIC_PACKET,
assert mock_wakeonlan.mock_calls[-1][1][0] == mac {"broadcast_address": bc_ip},
assert not mock_wakeonlan.mock_calls[-1][2] 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]