2019-04-03 15:40:03 +00:00
|
|
|
"""Support for wake on lan."""
|
2016-03-18 14:01:53 +00:00
|
|
|
import logging
|
|
|
|
import platform
|
|
|
|
import subprocess as sp
|
|
|
|
|
2016-08-25 04:36:41 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-09-10 14:07:31 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
2016-08-25 04:36:41 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-11-09 16:44:30 +00:00
|
|
|
from homeassistant.helpers.script import Script
|
2016-03-18 14:01:53 +00:00
|
|
|
|
2016-08-25 04:36:41 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-09-10 14:07:31 +00:00
|
|
|
CONF_BROADCAST_ADDRESS = 'broadcast_address'
|
2016-08-25 04:36:41 +00:00
|
|
|
CONF_MAC_ADDRESS = 'mac_address'
|
2016-11-09 16:44:30 +00:00
|
|
|
CONF_OFF_ACTION = 'turn_off'
|
2016-08-25 04:36:41 +00:00
|
|
|
|
|
|
|
DEFAULT_NAME = 'Wake on LAN'
|
2016-03-18 14:01:53 +00:00
|
|
|
DEFAULT_PING_TIMEOUT = 1
|
|
|
|
|
2016-08-25 04:36:41 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_MAC_ADDRESS): cv.string,
|
2017-04-18 16:09:06 +00:00
|
|
|
vol.Optional(CONF_BROADCAST_ADDRESS): cv.string,
|
2018-09-10 14:07:31 +00:00
|
|
|
vol.Optional(CONF_HOST): cv.string,
|
2016-08-25 04:36:41 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2016-11-09 16:44:30 +00:00
|
|
|
vol.Optional(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA,
|
2016-08-25 04:36:41 +00:00
|
|
|
})
|
|
|
|
|
2016-03-18 14:01:53 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-10-30 08:58:34 +00:00
|
|
|
"""Set up a wake on lan switch."""
|
2018-09-10 14:07:31 +00:00
|
|
|
broadcast_address = config.get(CONF_BROADCAST_ADDRESS)
|
2016-08-25 04:36:41 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
mac_address = config.get(CONF_MAC_ADDRESS)
|
2018-09-10 14:07:31 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2016-11-09 16:44:30 +00:00
|
|
|
off_action = config.get(CONF_OFF_ACTION)
|
2016-08-25 04:36:41 +00:00
|
|
|
|
2018-09-10 14:07:31 +00:00
|
|
|
add_entities([WOLSwitch(
|
|
|
|
hass, name, host, mac_address, off_action, broadcast_address)], True)
|
2016-03-18 14:01:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WOLSwitch(SwitchDevice):
|
|
|
|
"""Representation of a wake on lan switch."""
|
|
|
|
|
2018-09-10 14:07:31 +00:00
|
|
|
def __init__(
|
|
|
|
self, hass, name, host, mac_address, off_action,
|
|
|
|
broadcast_address):
|
2016-03-18 14:01:53 +00:00
|
|
|
"""Initialize the WOL switch."""
|
2018-02-06 00:04:31 +00:00
|
|
|
import wakeonlan
|
2016-03-18 14:01:53 +00:00
|
|
|
self._hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._host = host
|
|
|
|
self._mac_address = mac_address
|
2017-04-18 16:09:06 +00:00
|
|
|
self._broadcast_address = broadcast_address
|
2016-11-09 16:44:30 +00:00
|
|
|
self._off_script = Script(hass, off_action) if off_action else None
|
2016-03-18 14:01:53 +00:00
|
|
|
self._state = False
|
2018-02-06 00:04:31 +00:00
|
|
|
self._wol = wakeonlan
|
2016-03-18 14:01:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Return true if switch is on."""
|
2016-03-18 14:01:53 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the name of the switch."""
|
2016-03-18 14:01:53 +00:00
|
|
|
return self._name
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-18 14:01:53 +00:00
|
|
|
"""Turn the device on."""
|
2017-04-18 16:09:06 +00:00
|
|
|
if self._broadcast_address:
|
2017-05-02 20:47:20 +00:00
|
|
|
self._wol.send_magic_packet(
|
|
|
|
self._mac_address, ip_address=self._broadcast_address)
|
2017-03-27 12:02:43 +00:00
|
|
|
else:
|
|
|
|
self._wol.send_magic_packet(self._mac_address)
|
2016-03-18 14:01:53 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-11-09 16:44:30 +00:00
|
|
|
"""Turn the device off if an off action is present."""
|
|
|
|
if self._off_script is not None:
|
|
|
|
self._off_script.run()
|
2016-06-25 04:27:40 +00:00
|
|
|
|
2016-03-18 14:01:53 +00:00
|
|
|
def update(self):
|
|
|
|
"""Check if device is on and update the state."""
|
2016-10-30 08:58:34 +00:00
|
|
|
if platform.system().lower() == 'windows':
|
2017-03-08 04:57:35 +00:00
|
|
|
ping_cmd = ['ping', '-n', '1', '-w',
|
2017-03-12 19:46:58 +00:00
|
|
|
str(DEFAULT_PING_TIMEOUT * 1000), str(self._host)]
|
2016-03-18 14:01:53 +00:00
|
|
|
else:
|
2017-03-08 04:57:35 +00:00
|
|
|
ping_cmd = ['ping', '-c', '1', '-W',
|
2017-03-12 19:46:58 +00:00
|
|
|
str(DEFAULT_PING_TIMEOUT), str(self._host)]
|
2016-03-18 14:01:53 +00:00
|
|
|
|
2017-06-18 09:44:41 +00:00
|
|
|
status = sp.call(ping_cmd, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
|
2016-03-18 14:01:53 +00:00
|
|
|
self._state = not bool(status)
|