Implement Wake On Lan Dummy State (#47719)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Nathan Tilley 2021-03-15 11:20:47 -05:00 committed by GitHub
parent 8239fb76d1
commit 93c38551d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 3 deletions

View File

@ -521,6 +521,7 @@ homeassistant/components/vizio/* @raman325
homeassistant/components/vlc_telnet/* @rodripf @dmcc homeassistant/components/vlc_telnet/* @rodripf @dmcc
homeassistant/components/volkszaehler/* @fabaff homeassistant/components/volkszaehler/* @fabaff
homeassistant/components/volumio/* @OnFreund homeassistant/components/volumio/* @OnFreund
homeassistant/components/wake_on_lan/* @ntilley905
homeassistant/components/waqi/* @andrey-git homeassistant/components/waqi/* @andrey-git
homeassistant/components/watson_tts/* @rutkai homeassistant/components/watson_tts/* @rutkai
homeassistant/components/weather/* @fabaff homeassistant/components/weather/* @fabaff

View File

@ -3,5 +3,5 @@
"name": "Wake on LAN", "name": "Wake on LAN",
"documentation": "https://www.home-assistant.io/integrations/wake_on_lan", "documentation": "https://www.home-assistant.io/integrations/wake_on_lan",
"requirements": ["wakeonlan==1.1.6"], "requirements": ["wakeonlan==1.1.6"],
"codeowners": [] "codeowners": ["@ntilley905"]
} }

View File

@ -57,7 +57,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
broadcast_port, 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 Script(hass, off_action, name, domain) if off_action else None
) )
self._state = False self._state = False
self._assumed_state = host is None
@property @property
def is_on(self): def is_on(self):
@ -97,6 +98,16 @@ class WolSwitch(SwitchEntity):
"""Return the name of the switch.""" """Return the name of the switch."""
return self._name 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): def turn_on(self, **kwargs):
"""Turn the device on.""" """Turn the device on."""
service_kwargs = {} service_kwargs = {}
@ -114,13 +125,21 @@ class WolSwitch(SwitchEntity):
wakeonlan.send_magic_packet(self._mac_address, **service_kwargs) 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): 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."""
if self._off_script is not None: if self._off_script is not None:
self._off_script.run(context=self._context) self._off_script.run(context=self._context)
if self._assumed_state:
self._state = False
self.async_write_ha_state()
def update(self): 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": if platform.system().lower() == "windows":
ping_cmd = [ ping_cmd = [
"ping", "ping",

View File

@ -275,3 +275,44 @@ async def test_invalid_hostname_windows(hass):
state = hass.states.get("switch.wake_on_lan") state = hass.states.get("switch.wake_on_lan")
assert STATE_OFF == state.state 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