Add support for off script to WOL switch (#4258)
This commit is contained in:
parent
71da21dcc8
commit
d404ac8978
1 changed files with 11 additions and 4 deletions
|
@ -12,6 +12,7 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.script import Script
|
||||
from homeassistant.const import (CONF_HOST, CONF_NAME)
|
||||
|
||||
REQUIREMENTS = ['wakeonlan==0.2.2']
|
||||
|
@ -19,6 +20,7 @@ REQUIREMENTS = ['wakeonlan==0.2.2']
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_MAC_ADDRESS = 'mac_address'
|
||||
CONF_OFF_ACTION = 'turn_off'
|
||||
|
||||
DEFAULT_NAME = 'Wake on LAN'
|
||||
DEFAULT_PING_TIMEOUT = 1
|
||||
|
@ -27,6 +29,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
vol.Required(CONF_MAC_ADDRESS): cv.string,
|
||||
vol.Optional(CONF_HOST): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA,
|
||||
})
|
||||
|
||||
|
||||
|
@ -35,20 +38,22 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
name = config.get(CONF_NAME)
|
||||
host = config.get(CONF_HOST)
|
||||
mac_address = config.get(CONF_MAC_ADDRESS)
|
||||
off_action = config.get(CONF_OFF_ACTION)
|
||||
|
||||
add_devices([WOLSwitch(hass, name, host, mac_address)])
|
||||
add_devices([WOLSwitch(hass, name, host, mac_address, off_action)])
|
||||
|
||||
|
||||
class WOLSwitch(SwitchDevice):
|
||||
"""Representation of a wake on lan switch."""
|
||||
|
||||
def __init__(self, hass, name, host, mac_address):
|
||||
def __init__(self, hass, name, host, mac_address, off_action):
|
||||
"""Initialize the WOL switch."""
|
||||
from wakeonlan import wol
|
||||
self._hass = hass
|
||||
self._name = name
|
||||
self._host = host
|
||||
self._mac_address = mac_address
|
||||
self._off_script = Script(hass, off_action) if off_action else None
|
||||
self._state = False
|
||||
self._wol = wol
|
||||
self.update()
|
||||
|
@ -74,8 +79,10 @@ class WOLSwitch(SwitchDevice):
|
|||
self.update_ha_state()
|
||||
|
||||
def turn_off(self):
|
||||
"""Do nothing."""
|
||||
pass
|
||||
"""Turn the device off if an off action is present."""
|
||||
if self._off_script is not None:
|
||||
self._off_script.run()
|
||||
self.update_ha_state()
|
||||
|
||||
def update(self):
|
||||
"""Check if device is on and update the state."""
|
||||
|
|
Loading…
Add table
Reference in a new issue