2015-06-02 13:40:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-06-02 13:54:43 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.switch.command_switch
|
2015-09-07 17:39:16 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-06-02 13:54:43 +00:00
|
|
|
Allows to configure custom shell commands to turn a switch on/off.
|
2015-09-12 06:54:28 +00:00
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
To use the command_line switch you will need to add something like the
|
|
|
|
following to your configuration.yaml file.
|
|
|
|
|
|
|
|
switch:
|
|
|
|
platform: command_switch
|
|
|
|
switches:
|
|
|
|
name_of_the_switch:
|
|
|
|
oncmd: switch_command on for name_of_the_switch
|
|
|
|
offcmd: switch_command off for name_of_the_switch
|
|
|
|
|
|
|
|
Variables:
|
|
|
|
|
|
|
|
These are the variables for the switches array:
|
|
|
|
|
|
|
|
name_of_the_switch
|
|
|
|
*Required
|
|
|
|
Name of the command switch. Multiple entries are possible.
|
|
|
|
|
|
|
|
oncmd
|
|
|
|
*Required
|
|
|
|
The action to take for on.
|
|
|
|
|
|
|
|
offcmd
|
|
|
|
*Required
|
|
|
|
The action to take for off.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.command_switch.html
|
2015-06-02 13:54:43 +00:00
|
|
|
"""
|
2015-06-02 13:40:02 +00:00
|
|
|
import logging
|
2015-06-13 21:56:20 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2015-06-02 13:40:02 +00:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|
|
|
""" Find and return switches controlled by shell commands. """
|
|
|
|
|
|
|
|
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-06-02 13:54:43 +00:00
|
|
|
dev_name,
|
|
|
|
properties.get('oncmd', 'true'),
|
|
|
|
properties.get('offcmd', 'true')))
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
add_devices_callback(devices)
|
|
|
|
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
class CommandSwitch(SwitchDevice):
|
2015-09-07 17:39:16 +00:00
|
|
|
""" Represents a switch that can be togggled using shell commands. """
|
2015-06-02 13:40:02 +00:00
|
|
|
def __init__(self, name, command_on, command_off):
|
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
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _switch(command):
|
2015-09-07 17:39:16 +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
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2015-09-07 17:39:16 +00:00
|
|
|
""" No polling needed. """
|
2015-06-02 13:40:02 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2015-09-07 17:39:16 +00:00
|
|
|
""" The name of the switch. """
|
2015-06-02 13:40:02 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2015-06-02 13:54:43 +00:00
|
|
|
""" True if device is on. """
|
2015-06-13 21:56:20 +00:00
|
|
|
return self._state
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2015-06-02 13:54:43 +00:00
|
|
|
""" Turn the device on. """
|
2015-06-02 13:40:02 +00:00
|
|
|
if CommandSwitch._switch(self._command_on):
|
2015-06-13 21:56:20 +00:00
|
|
|
self._state = True
|
|
|
|
self.update_ha_state()
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2015-06-02 13:54:43 +00:00
|
|
|
""" Turn the device off. """
|
2015-06-02 13:40:02 +00:00
|
|
|
if CommandSwitch._switch(self._command_off):
|
2015-06-13 21:56:20 +00:00
|
|
|
self._state = False
|
|
|
|
self.update_ha_state()
|