From 873999167641c1adf75dcbd80b240485b76b4df5 Mon Sep 17 00:00:00 2001 From: cgtobi Date: Wed, 1 Nov 2017 11:15:24 +0100 Subject: [PATCH] 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. --- .coveragerc | 1 - requirements_test_all.txt | 7 + script/gen_requirements_all.py | 3 +- tests/components/switch/test_wake_on_lan.py | 188 ++++++++++++++++++++ 4 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 tests/components/switch/test_wake_on_lan.py diff --git a/.coveragerc b/.coveragerc index 06263103990..95a5e490eee 100644 --- a/.coveragerc +++ b/.coveragerc @@ -612,7 +612,6 @@ omit = homeassistant/components/switch/tplink.py homeassistant/components/switch/telnet.py homeassistant/components/switch/transmission.py - homeassistant/components/switch/wake_on_lan.py homeassistant/components/switch/xiaomi_miio.py homeassistant/components/telegram_bot/* homeassistant/components/thingspeak.py diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 29d2732e1a5..eb8931480db 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -164,6 +164,13 @@ statsd==3.2.1 # homeassistant.components.camera.uvc 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 warrant==0.5.0 diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index 63c4ab2c90d..d2ac40c2550 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -81,7 +81,8 @@ TEST_REQUIREMENTS = ( 'uvcclient', 'warrant', 'yahoo-finance', - 'pythonwhois' + 'pythonwhois', + 'wakeonlan' ) IGNORE_PACKAGES = ( diff --git a/tests/components/switch/test_wake_on_lan.py b/tests/components/switch/test_wake_on_lan.py new file mode 100644 index 00000000000..063cf93d871 --- /dev/null +++ b/tests/components/switch/test_wake_on_lan.py @@ -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)