mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
add statecmd to command_switch
This commit is contained in:
parent
07fb4ff243
commit
d5179b4bdc
@ -10,6 +10,8 @@ import logging
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDevice
|
from homeassistant.components.switch import SwitchDevice
|
||||||
|
from homeassistant.const import CONF_VALUE_TEMPLATE
|
||||||
|
from homeassistant.util import template
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -22,22 +24,36 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||||||
devices = []
|
devices = []
|
||||||
|
|
||||||
for dev_name, properties in switches.items():
|
for dev_name, properties in switches.items():
|
||||||
|
if 'statecmd' in properties and CONF_VALUE_TEMPLATE not in properties:
|
||||||
|
_LOGGER.warn("Specify a %s when using statemcd",
|
||||||
|
CONF_VALUE_TEMPLATE)
|
||||||
|
continue
|
||||||
devices.append(
|
devices.append(
|
||||||
CommandSwitch(
|
CommandSwitch(
|
||||||
|
hass,
|
||||||
properties.get('name', dev_name),
|
properties.get('name', dev_name),
|
||||||
properties.get('oncmd', 'true'),
|
properties.get('oncmd', 'true'),
|
||||||
properties.get('offcmd', 'true')))
|
properties.get('offcmd', 'true'),
|
||||||
|
properties.get('statecmd', False),
|
||||||
|
properties.get(CONF_VALUE_TEMPLATE, False)))
|
||||||
|
|
||||||
add_devices_callback(devices)
|
add_devices_callback(devices)
|
||||||
|
|
||||||
|
|
||||||
class CommandSwitch(SwitchDevice):
|
class CommandSwitch(SwitchDevice):
|
||||||
""" Represents a switch that can be togggled using shell commands. """
|
""" Represents a switch that can be togggled using shell commands. """
|
||||||
def __init__(self, name, command_on, command_off):
|
|
||||||
|
# pylint: disable=too-many-arguments
|
||||||
|
def __init__(self, hass, name, command_on, command_off,
|
||||||
|
command_state, value_template):
|
||||||
|
|
||||||
|
self._hass = hass
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = False
|
self._state = False
|
||||||
self._command_on = command_on
|
self._command_on = command_on
|
||||||
self._command_off = command_off
|
self._command_off = command_off
|
||||||
|
self._command_state = command_state
|
||||||
|
self._value_template = value_template
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _switch(command):
|
def _switch(command):
|
||||||
@ -51,10 +67,21 @@ class CommandSwitch(SwitchDevice):
|
|||||||
|
|
||||||
return success
|
return success
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _query_state(command):
|
||||||
|
""" Execute state command. """
|
||||||
|
_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)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
""" No polling needed. """
|
""" No polling needed. """
|
||||||
return False
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -66,14 +93,24 @@ class CommandSwitch(SwitchDevice):
|
|||||||
""" True if device is on. """
|
""" True if device is on. """
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
""" Update device state. """
|
||||||
|
if self._command_state and self._value_template:
|
||||||
|
payload = CommandSwitch._query_state(self._command_state)
|
||||||
|
payload = template.render_with_possible_json_value(
|
||||||
|
self._hass, self._value_template, payload)
|
||||||
|
self._state = (payload == "True")
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the device on. """
|
""" Turn the device on. """
|
||||||
if CommandSwitch._switch(self._command_on):
|
if CommandSwitch._switch(self._command_on):
|
||||||
self._state = True
|
if not self._command_state:
|
||||||
self.update_ha_state()
|
self._state = True
|
||||||
|
self.update_ha_state()
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turn the device off. """
|
""" Turn the device off. """
|
||||||
if CommandSwitch._switch(self._command_off):
|
if CommandSwitch._switch(self._command_off):
|
||||||
self._state = False
|
if not self._command_state:
|
||||||
self.update_ha_state()
|
self._state = False
|
||||||
|
self.update_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user