2019-04-03 15:40:03 +00:00
|
|
|
"""Support for custom shell commands to turn a switch on/off."""
|
2015-06-02 13:40:02 +00:00
|
|
|
import logging
|
|
|
|
|
2016-09-02 14:09:09 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-08-08 20:36:59 +00:00
|
|
|
from homeassistant.components.switch import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT,
|
2019-12-08 16:55:57 +00:00
|
|
|
PLATFORM_SCHEMA,
|
2020-04-26 16:50:37 +00:00
|
|
|
SwitchEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-09-02 14:09:09 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_COMMAND_OFF,
|
|
|
|
CONF_COMMAND_ON,
|
|
|
|
CONF_COMMAND_STATE,
|
2019-12-08 16:55:57 +00:00
|
|
|
CONF_FRIENDLY_NAME,
|
|
|
|
CONF_SWITCHES,
|
|
|
|
CONF_VALUE_TEMPLATE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-08 16:55:57 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-15 05:56:08 +00:00
|
|
|
|
2020-08-05 03:00:02 +00:00
|
|
|
from . import call_shell_with_timeout, check_output_or_log
|
|
|
|
from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT
|
|
|
|
|
2015-06-02 13:40:02 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SWITCH_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_COMMAND_OFF, default="true"): cv.string,
|
|
|
|
vol.Optional(CONF_COMMAND_ON, default="true"): cv.string,
|
|
|
|
vol.Optional(CONF_COMMAND_STATE): cv.string,
|
|
|
|
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
2020-08-05 03:00:02 +00:00
|
|
|
vol.Optional(CONF_COMMAND_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2016-09-02 14:09:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_SWITCHES): cv.schema_with_slug_keys(SWITCH_SCHEMA)}
|
|
|
|
)
|
2016-09-02 14:09:09 +00:00
|
|
|
|
2015-06-02 13:40:02 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Find and return switches controlled by shell commands."""
|
2016-09-02 14:09:09 +00:00
|
|
|
devices = config.get(CONF_SWITCHES, {})
|
|
|
|
switches = []
|
2015-06-02 13:40:02 +00:00
|
|
|
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
for object_id, device_config in devices.items():
|
2016-09-28 04:29:55 +00:00
|
|
|
value_template = device_config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
|
|
|
|
if value_template is not None:
|
|
|
|
value_template.hass = hass
|
|
|
|
|
2016-09-02 14:09:09 +00:00
|
|
|
switches.append(
|
2015-06-02 13:40:02 +00:00
|
|
|
CommandSwitch(
|
2015-12-22 00:49:39 +00:00
|
|
|
hass,
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
object_id,
|
|
|
|
device_config.get(CONF_FRIENDLY_NAME, object_id),
|
2020-08-05 03:00:02 +00:00
|
|
|
device_config[CONF_COMMAND_ON],
|
|
|
|
device_config[CONF_COMMAND_OFF],
|
2016-09-02 14:09:09 +00:00
|
|
|
device_config.get(CONF_COMMAND_STATE),
|
2019-07-31 19:25:30 +00:00
|
|
|
value_template,
|
2020-08-05 03:00:02 +00:00
|
|
|
device_config[CONF_COMMAND_TIMEOUT],
|
2016-09-02 14:09:09 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not switches:
|
|
|
|
_LOGGER.error("No switches added")
|
|
|
|
return False
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(switches)
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class CommandSwitch(SwitchEntity):
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass,
|
|
|
|
object_id,
|
|
|
|
friendly_name,
|
|
|
|
command_on,
|
|
|
|
command_off,
|
|
|
|
command_state,
|
|
|
|
value_template,
|
2020-08-05 03:00:02 +00:00
|
|
|
timeout,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the switch."""
|
2015-12-22 00:49:39 +00:00
|
|
|
self._hass = hass
|
Use entity_id for backend, friendly name for frontend (#4343)
* Use entity_id for backend, friendly name for frontend
Closes https://github.com/home-assistant/home-assistant/issues/3434
Command line switches had the option to set a `friendly_name` reportedly
for use in the front end. However, if set, it was also being used as the
`entity_id`.
This did not seem like obvious behavior to me. This PR changes the
behavior so the entity_id is the object_id, which must already be
unique, and is an obvious place to have a very predictable slug (even if
long or unsightly), and the friendly name (if set) is used for the
display.
Example:
```yaml
switch:
platform: command_line
switches:
rf_kitchen_light_one:
command_on: switch_command on kitchen
command_off: switch_command off kitchen
command_state: query_command kitchen
value_template: '{{ value == "online" }}'
friendly_name: "Beautiful bright kitchen light!"
```
If you were using in an automation or from dev tools, would use:
`switch.rf_kitchen_light_one`, but your front end would still show `Beautiful
bright kitchen light!`
* Add new arg to test_assumed_state_should_be_true_if_command_state_is_false
* Import ENTITY_ID _FORMAT from existing, rename device_name to object_id
* Rename `device_name` to `object_id`
* Test that `entity_id` and `name` are set as expected
2016-11-13 06:46:23 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
|
|
|
|
self._name = friendly_name
|
2015-06-13 21:56:20 +00:00
|
|
|
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
|
2020-08-05 03:00:02 +00:00
|
|
|
self._timeout = timeout
|
2015-06-02 13:40:02 +00:00
|
|
|
|
2020-08-05 03:00:02 +00:00
|
|
|
def _switch(self, command):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Execute the actual commands."""
|
2017-08-08 20:36:59 +00:00
|
|
|
_LOGGER.info("Running command: %s", command)
|
2015-06-02 13:40:02 +00:00
|
|
|
|
2020-08-05 03:00:02 +00:00
|
|
|
success = call_shell_with_timeout(command, self._timeout) == 0
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
if not success:
|
2017-08-08 20:36:59 +00:00
|
|
|
_LOGGER.error("Command failed: %s", command)
|
2015-06-02 13:40:02 +00:00
|
|
|
|
|
|
|
return success
|
|
|
|
|
2020-08-05 03:00:02 +00:00
|
|
|
def _query_state_value(self, command):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Execute state command for return value."""
|
2020-08-05 03:00:02 +00:00
|
|
|
_LOGGER.info("Running state value command: %s", command)
|
|
|
|
return check_output_or_log(command, self._timeout)
|
2015-12-22 00:49:39 +00:00
|
|
|
|
2020-08-05 03:00:02 +00:00
|
|
|
def _query_state_code(self, command):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Execute state command for return code."""
|
2020-08-05 03:00:02 +00:00
|
|
|
_LOGGER.info("Running state code command: %s", command)
|
2020-08-06 10:36:59 +00:00
|
|
|
return (
|
|
|
|
call_shell_with_timeout(command, self._timeout, log_return_code=False) == 0
|
|
|
|
)
|
2015-12-28 03:49:55 +00:00
|
|
|
|
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."""
|
2017-01-27 05:41:30 +00:00
|
|
|
return self._command_state is None
|
2016-03-15 12:32:33 +00:00
|
|
|
|
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:
|
2017-08-08 20:36:59 +00:00
|
|
|
_LOGGER.error("No state command specified")
|
2015-12-28 03:49:55 +00:00
|
|
|
return
|
|
|
|
if self._value_template:
|
2020-08-05 03:00:02 +00:00
|
|
|
return self._query_state_value(self._command_state)
|
|
|
|
return self._query_state_code(self._command_state)
|
2015-12-28 03:49:55 +00:00
|
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
payload = self._value_template.render_with_possible_json_value(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."""
|
2020-08-05 03:00:02 +00:00
|
|
|
if self._switch(self._command_on) and not self._command_state:
|
2015-12-31 23:39:40 +00:00
|
|
|
self._state = True
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_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."""
|
2020-08-05 03:00:02 +00:00
|
|
|
if self._switch(self._command_off) and not self._command_state:
|
2015-12-31 23:39:40 +00:00
|
|
|
self._state = False
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|