Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move imports in wake_on_lan component #28100

Merged
merged 2 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ monkeytype.sqlite3

# This is left behind by Azure Restore Cache
tmp_cache

# python-language-server / Rope
.ropeproject
2 changes: 1 addition & 1 deletion homeassistant/components/wake_on_lan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging

import voluptuous as vol
import wakeonlan

from homeassistant.const import CONF_MAC
import homeassistant.helpers.config_validation as cv
Expand All @@ -22,7 +23,6 @@

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."""
Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/wake_on_lan/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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
Expand Down Expand Up @@ -48,16 +49,13 @@ 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
self._mac_address = mac_address
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):
Expand All @@ -72,11 +70,11 @@ def name(self):
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."""
Expand Down
72 changes: 33 additions & 39 deletions tests/components/wake_on_lan/test_init.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,46 @@
"""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]