mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Add unit test for wake on lan component. (#10262)
* Add unit test for wake on lan component. * Remove unneccessary imports and print calls. * Clean up lint complaints.
This commit is contained in:
parent
26b097b860
commit
8739991676
@ -612,7 +612,6 @@ omit =
|
|||||||
homeassistant/components/switch/tplink.py
|
homeassistant/components/switch/tplink.py
|
||||||
homeassistant/components/switch/telnet.py
|
homeassistant/components/switch/telnet.py
|
||||||
homeassistant/components/switch/transmission.py
|
homeassistant/components/switch/transmission.py
|
||||||
homeassistant/components/switch/wake_on_lan.py
|
|
||||||
homeassistant/components/switch/xiaomi_miio.py
|
homeassistant/components/switch/xiaomi_miio.py
|
||||||
homeassistant/components/telegram_bot/*
|
homeassistant/components/telegram_bot/*
|
||||||
homeassistant/components/thingspeak.py
|
homeassistant/components/thingspeak.py
|
||||||
|
@ -164,6 +164,13 @@ statsd==3.2.1
|
|||||||
# homeassistant.components.camera.uvc
|
# homeassistant.components.camera.uvc
|
||||||
uvcclient==0.10.1
|
uvcclient==0.10.1
|
||||||
|
|
||||||
|
# homeassistant.components.wake_on_lan
|
||||||
|
# homeassistant.components.media_player.panasonic_viera
|
||||||
|
# homeassistant.components.media_player.samsungtv
|
||||||
|
# homeassistant.components.media_player.webostv
|
||||||
|
# homeassistant.components.switch.wake_on_lan
|
||||||
|
wakeonlan==0.2.2
|
||||||
|
|
||||||
# homeassistant.components.cloud
|
# homeassistant.components.cloud
|
||||||
warrant==0.5.0
|
warrant==0.5.0
|
||||||
|
|
||||||
|
@ -81,7 +81,8 @@ TEST_REQUIREMENTS = (
|
|||||||
'uvcclient',
|
'uvcclient',
|
||||||
'warrant',
|
'warrant',
|
||||||
'yahoo-finance',
|
'yahoo-finance',
|
||||||
'pythonwhois'
|
'pythonwhois',
|
||||||
|
'wakeonlan'
|
||||||
)
|
)
|
||||||
|
|
||||||
IGNORE_PACKAGES = (
|
IGNORE_PACKAGES = (
|
||||||
|
188
tests/components/switch/test_wake_on_lan.py
Normal file
188
tests/components/switch/test_wake_on_lan.py
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
"""The tests for the wake on lan switch platform."""
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from homeassistant.setup import setup_component
|
||||||
|
from homeassistant.const import STATE_ON, STATE_OFF
|
||||||
|
import homeassistant.components.switch as switch
|
||||||
|
|
||||||
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
|
|
||||||
|
TEST_STATE = None
|
||||||
|
|
||||||
|
|
||||||
|
def send_magic_packet(*macs, **kwargs):
|
||||||
|
"""Fake call for sending magic packets."""
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def call(cmd, stdout, stderr):
|
||||||
|
"""Return fake subprocess return codes."""
|
||||||
|
if cmd[5] == 'validhostname' and TEST_STATE:
|
||||||
|
return 0
|
||||||
|
return 2
|
||||||
|
|
||||||
|
|
||||||
|
def system():
|
||||||
|
"""Fake system call to test the windows platform."""
|
||||||
|
return 'Windows'
|
||||||
|
|
||||||
|
|
||||||
|
class TestWOLSwitch(unittest.TestCase):
|
||||||
|
"""Test the wol switch."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""Setup things to be run when tests are started."""
|
||||||
|
self.hass = get_test_home_assistant()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
"""Stop everything that was started."""
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
@patch('wakeonlan.wol.send_magic_packet', new=send_magic_packet)
|
||||||
|
@patch('subprocess.call', new=call)
|
||||||
|
def test_valid_hostname(self):
|
||||||
|
"""Test with valid hostname."""
|
||||||
|
global TEST_STATE
|
||||||
|
TEST_STATE = False
|
||||||
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'wake_on_lan',
|
||||||
|
'mac_address': '00-01-02-03-04-05',
|
||||||
|
'host': 'validhostname',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
TEST_STATE = True
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.wake_on_lan')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
switch.turn_off(self.hass, 'switch.wake_on_lan')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
@patch('wakeonlan.wol.send_magic_packet', new=send_magic_packet)
|
||||||
|
@patch('subprocess.call', new=call)
|
||||||
|
@patch('platform.system', new=system)
|
||||||
|
def test_valid_hostname_windows(self):
|
||||||
|
"""Test with valid hostname on windows."""
|
||||||
|
global TEST_STATE
|
||||||
|
TEST_STATE = False
|
||||||
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'wake_on_lan',
|
||||||
|
'mac_address': '00-01-02-03-04-05',
|
||||||
|
'host': 'validhostname',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
TEST_STATE = True
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.wake_on_lan')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
@patch('wakeonlan.wol.send_magic_packet', new=send_magic_packet)
|
||||||
|
def test_minimal_config(self):
|
||||||
|
"""Test with minimal config."""
|
||||||
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'wake_on_lan',
|
||||||
|
'mac_address': '00-01-02-03-04-05',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
@patch('wakeonlan.wol.send_magic_packet', new=send_magic_packet)
|
||||||
|
@patch('subprocess.call', new=call)
|
||||||
|
def test_broadcast_config(self):
|
||||||
|
"""Test with broadcast address config."""
|
||||||
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'wake_on_lan',
|
||||||
|
'mac_address': '00-01-02-03-04-05',
|
||||||
|
'broadcast_address': '255.255.255.255',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.wake_on_lan')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
@patch('wakeonlan.wol.send_magic_packet', new=send_magic_packet)
|
||||||
|
@patch('subprocess.call', new=call)
|
||||||
|
def test_off_script(self):
|
||||||
|
"""Test with turn off script."""
|
||||||
|
global TEST_STATE
|
||||||
|
TEST_STATE = False
|
||||||
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'wake_on_lan',
|
||||||
|
'mac_address': '00-01-02-03-04-05',
|
||||||
|
'host': 'validhostname',
|
||||||
|
'turn_off': {
|
||||||
|
'service': 'shell_command.turn_off_TARGET',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
TEST_STATE = True
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.wake_on_lan')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
TEST_STATE = False
|
||||||
|
|
||||||
|
switch.turn_off(self.hass, 'switch.wake_on_lan')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
@patch('wakeonlan.wol.send_magic_packet', new=send_magic_packet)
|
||||||
|
@patch('subprocess.call', new=call)
|
||||||
|
@patch('platform.system', new=system)
|
||||||
|
def test_invalid_hostname_windows(self):
|
||||||
|
"""Test with invalid hostname on windows."""
|
||||||
|
global TEST_STATE
|
||||||
|
TEST_STATE = False
|
||||||
|
self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'wake_on_lan',
|
||||||
|
'mac_address': '00-01-02-03-04-05',
|
||||||
|
'host': 'invalidhostname',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
TEST_STATE = True
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.wake_on_lan')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.wake_on_lan')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
Loading…
x
Reference in New Issue
Block a user