2015-06-02 13:54:43 +00:00
|
|
|
"""
|
2016-02-23 20:06:50 +00:00
|
|
|
Support for custom shell commands to turn a switch on/off.
|
2015-09-12 06:54:28 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
2016-05-04 22:32:11 +00:00
|
|
|
https://home-assistant.io/components/switch.command_line/
|
2015-06-02 13:54:43 +00:00
|
|
|
"""
|
2015-06-02 13:40:02 +00:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
2015-09-15 05:56:08 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2015-12-22 00:49:39 +00:00
|
|
|
from homeassistant.const import CONF_VALUE_TEMPLATE
|
2016-02-23 20:06:50 +00:00
|
|
|
from homeassistant.helpers import template
|
2015-09-15 05:56:08 +00:00
|
|
|
|
2015-06-02 13:40:02 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Find and return switches controlled by shell commands."""
|
2015-06-02 13:40:02 +00:00
|
|
|
switches = config.get('switches', {})
|
|
|
|
devices = []
|
|
|
|
|
2015-06-02 13:54:43 +00:00
|
|
|
for dev_name, properties in switches.items():
|
2015-06-02 13:40:02 +00:00
|
|
|
devices.append(
|
|
|
|
CommandSwitch(
|
2015-12-22 00:49:39 +00:00
|
|
|
hass,
|
2015-09-14 06:04:42 +00:00
|
|
|
properties.get('name', dev_name),
|
2015-06-02 13:54:43 +00:00
|
|
|
properties.get('oncmd', 'true'),
|
2015-12-22 00:49:39 +00:00
|
|
|
properties.get('offcmd', 'true'),
|
|
|
|
properties.get('statecmd', False),
|
|
|
|
properties.get(CONF_VALUE_TEMPLATE, False)))
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
add_devices_callback(devices)
|
|
|
|
|
|
|
|
|
2016-03-15 12:32:33 +00:00
|
|
|
# pylint: disable=too-many-instance-attributes
|
2015-06-13 21:56:20 +00:00
|
|
|
class CommandSwitch(SwitchDevice):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation a switch that can be toggled using shell commands."""
|
2015-12-22 00:49:39 +00:00
|
|
|
|
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
def __init__(self, hass, name, command_on, command_off,
|
|
|
|
command_state, value_template):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the switch."""
|
2015-12-22 00:49:39 +00:00
|
|
|
self._hass = hass
|
2015-06-13 21:56:20 +00:00
|
|
|
self._name = name
|
|
|
|
self._state = False
|
2015-06-02 13:40:02 +00:00
|
|
|
self._command_on = command_on
|
|
|
|
self._command_off = command_off
|
2015-12-22 00:49:39 +00:00
|
|
|
self._command_state = command_state
|
|
|
|
self._value_template = value_template
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _switch(command):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Execute the actual commands."""
|
2015-06-02 15:04:14 +00:00
|
|
|
_LOGGER.info('Running command: %s', command)
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
success = (subprocess.call(command, shell=True) == 0)
|
|
|
|
|
|
|
|
if not success:
|
2015-06-02 15:04:14 +00:00
|
|
|
_LOGGER.error('Command failed: %s', command)
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
return success
|
|
|
|
|
2015-12-22 00:49:39 +00:00
|
|
|
@staticmethod
|
2015-12-28 03:49:55 +00:00
|
|
|
def _query_state_value(command):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Execute state command for return value."""
|
2015-12-22 00:49:39 +00:00
|
|
|
_LOGGER.info('Running state command: %s', command)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return_value = subprocess.check_output(command, shell=True)
|
|
|
|
return return_value.strip().decode('utf-8')
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
_LOGGER.error('Command failed: %s', command)
|
|
|
|
|
2015-12-28 03:49:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def _query_state_code(command):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Execute state command for return code."""
|
2015-12-28 03:49:55 +00:00
|
|
|
_LOGGER.info('Running state command: %s', command)
|
|
|
|
return subprocess.call(command, shell=True) == 0
|
|
|
|
|
2015-06-02 13:40:02 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Only poll if we have state command."""
|
2015-12-28 03:49:55 +00:00
|
|
|
return self._command_state is not None
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the switch."""
|
2015-06-02 13:40:02 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if device is on."""
|
2015-06-13 21:56:20 +00:00
|
|
|
return self._state
|
2015-06-02 13:40:02 +00:00
|
|
|
|
2016-03-15 12:32:33 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return true if we do optimistic updates."""
|
|
|
|
return self._command_state is False
|
|
|
|
|
2015-12-28 03:49:55 +00:00
|
|
|
def _query_state(self):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Query for state."""
|
2015-12-28 03:49:55 +00:00
|
|
|
if not self._command_state:
|
|
|
|
_LOGGER.error('No state command specified')
|
|
|
|
return
|
|
|
|
if self._value_template:
|
|
|
|
return CommandSwitch._query_state_value(self._command_state)
|
|
|
|
return CommandSwitch._query_state_code(self._command_state)
|
|
|
|
|
2015-12-22 00:49:39 +00:00
|
|
|
def update(self):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Update device state."""
|
2015-12-28 03:49:55 +00:00
|
|
|
if self._command_state:
|
|
|
|
payload = str(self._query_state())
|
|
|
|
if self._value_template:
|
|
|
|
payload = template.render_with_possible_json_value(
|
|
|
|
self._hass, self._value_template, payload)
|
|
|
|
self._state = (payload.lower() == "true")
|
2015-12-22 00:49:39 +00:00
|
|
|
|
2015-06-02 13:40:02 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Turn the device on."""
|
2015-12-31 23:39:40 +00:00
|
|
|
if (CommandSwitch._switch(self._command_on) and
|
|
|
|
not self._command_state):
|
|
|
|
self._state = True
|
|
|
|
self.update_ha_state()
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Turn the device off."""
|
2015-12-31 23:39:40 +00:00
|
|
|
if (CommandSwitch._switch(self._command_off) and
|
|
|
|
not self._command_state):
|
|
|
|
self._state = False
|
|
|
|
self.update_ha_state()
|