diff --git a/CODEOWNERS b/CODEOWNERS index bece933d9c8..263e5337c58 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -521,6 +521,7 @@ homeassistant/components/vizio/* @raman325 homeassistant/components/vlc_telnet/* @rodripf @dmcc homeassistant/components/volkszaehler/* @fabaff homeassistant/components/volumio/* @OnFreund +homeassistant/components/wake_on_lan/* @ntilley905 homeassistant/components/waqi/* @andrey-git homeassistant/components/watson_tts/* @rutkai homeassistant/components/weather/* @fabaff diff --git a/homeassistant/components/wake_on_lan/manifest.json b/homeassistant/components/wake_on_lan/manifest.json index c66f87ae26e..bcd7ef58c8c 100644 --- a/homeassistant/components/wake_on_lan/manifest.json +++ b/homeassistant/components/wake_on_lan/manifest.json @@ -3,5 +3,5 @@ "name": "Wake on LAN", "documentation": "https://www.home-assistant.io/integrations/wake_on_lan", "requirements": ["wakeonlan==1.1.6"], - "codeowners": [] + "codeowners": ["@ntilley905"] } diff --git a/homeassistant/components/wake_on_lan/switch.py b/homeassistant/components/wake_on_lan/switch.py index 491bae782c5..eba6897647b 100644 --- a/homeassistant/components/wake_on_lan/switch.py +++ b/homeassistant/components/wake_on_lan/switch.py @@ -57,7 +57,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): broadcast_port, ) ], - True, + host is not None, ) @@ -86,6 +86,7 @@ class WolSwitch(SwitchEntity): Script(hass, off_action, name, domain) if off_action else None ) self._state = False + self._assumed_state = host is None @property def is_on(self): @@ -97,6 +98,16 @@ class WolSwitch(SwitchEntity): """Return the name of the switch.""" return self._name + @property + def assumed_state(self): + """Return true if no host is provided.""" + return self._assumed_state + + @property + def should_poll(self): + """Return false if assumed state is true.""" + return not self._assumed_state + def turn_on(self, **kwargs): """Turn the device on.""" service_kwargs = {} @@ -114,13 +125,21 @@ class WolSwitch(SwitchEntity): wakeonlan.send_magic_packet(self._mac_address, **service_kwargs) + if self._assumed_state: + self._state = True + self.async_write_ha_state() + def turn_off(self, **kwargs): """Turn the device off if an off action is present.""" if self._off_script is not None: self._off_script.run(context=self._context) + if self._assumed_state: + self._state = False + self.async_write_ha_state() + def update(self): - """Check if device is on and update the state.""" + """Check if device is on and update the state. Only called if assumed state is false.""" if platform.system().lower() == "windows": ping_cmd = [ "ping", diff --git a/tests/components/wake_on_lan/test_switch.py b/tests/components/wake_on_lan/test_switch.py index c2e32f77ccf..7b41fd4d75c 100644 --- a/tests/components/wake_on_lan/test_switch.py +++ b/tests/components/wake_on_lan/test_switch.py @@ -275,3 +275,44 @@ async def test_invalid_hostname_windows(hass): state = hass.states.get("switch.wake_on_lan") assert STATE_OFF == state.state + + +async def test_no_hostname_state(hass): + """Test that the state updates if we do not pass in a hostname.""" + + assert await async_setup_component( + hass, + switch.DOMAIN, + { + "switch": { + "platform": "wake_on_lan", + "mac": "00-01-02-03-04-05", + } + }, + ) + await hass.async_block_till_done() + + state = hass.states.get("switch.wake_on_lan") + assert state.state == STATE_OFF + + with patch.object(subprocess, "call", return_value=0): + + await hass.services.async_call( + switch.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "switch.wake_on_lan"}, + blocking=True, + ) + + state = hass.states.get("switch.wake_on_lan") + assert state.state == STATE_ON + + await hass.services.async_call( + switch.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "switch.wake_on_lan"}, + blocking=True, + ) + + state = hass.states.get("switch.wake_on_lan") + assert state.state == STATE_OFF