From c8961fcf99ce134a56a605b295122bd5a09b79d0 Mon Sep 17 00:00:00 2001 From: Bart274 Date: Wed, 13 Jan 2016 11:52:42 +0100 Subject: [PATCH 1/9] Create command_sensor.py This adds a binary command_sensor --- .../binary_sensor/command_sensor.py | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 homeassistant/components/binary_sensor/command_sensor.py diff --git a/homeassistant/components/binary_sensor/command_sensor.py b/homeassistant/components/binary_sensor/command_sensor.py new file mode 100644 index 00000000000..1f86a86ff14 --- /dev/null +++ b/homeassistant/components/binary_sensor/command_sensor.py @@ -0,0 +1,101 @@ +""" +homeassistant.components.sensor.command_sensor +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Allows to configure custom shell commands to turn a value for a sensor. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.command_sensor/ +""" +import logging +import subprocess +from datetime import timedelta + +from homeassistant.const import CONF_VALUE_TEMPLATE +from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.util import template, Throttle + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_NAME = "Command Sensor" +DEFAULT_PAYLOAD_ON = 'ON' +DEFAULT_PAYLOAD_OFF = 'OFF' + +# Return cached results if last scan was less then this time ago +MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) + + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Add the Command Sensor. """ + + if config.get('command') is None: + _LOGGER.error('Missing required variable: "command"') + return False + + data = CommandSensorData(config.get('command')) + + add_devices([CommandBinarySensor( + hass, + data, + config.get('name', DEFAULT_NAME), + config.get('payload_on', DEFAULT_PAYLOAD_ON), + config.get('payload_off', DEFAULT_PAYLOAD_OFF), + config.get(CONF_VALUE_TEMPLATE) + )]) + + +# pylint: disable=too-many-arguments +class CommandBinarySensor(BinarySensorDevice): + """ Represents a binary sensor that is returning a value of a shell commands. """ + def __init__(self, hass, data, name, payload_on, payload_off, value_template): + self._hass = hass + self.data = data + self._name = name + self._state = False + self._payload_on = payload_on + self._payload_off = payload_off + self._value_template = value_template + self.update() + + @property + def name(self): + """ The name of the sensor. """ + return self._name + + @property + def is_on(self): + """ True if the binary sensor is on. """ + return self._state + + def update(self): + """ Gets the latest data and updates the state. """ + self.data.update() + value = self.data.value + + if self._value_template is not None: + value = template.render_with_possible_json_value( + self._hass, self.value_template, value, False) + if value == self._payload_on: + self._state = True + elif value == self._payload_off: + self._state = False + + +# pylint: disable=too-few-public-methods +class CommandSensorData(object): + """ Class for handling the data retrieval. """ + + def __init__(self, command): + self.command = command + self.value = None + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update(self): + """ Gets the latest data with a shell command. """ + _LOGGER.info('Running command: %s', self.command) + + try: + return_value = subprocess.check_output(self.command, shell=True) + self.value = return_value.strip().decode('utf-8') + except subprocess.CalledProcessError: + _LOGGER.error('Command failed: %s', self.command) From e5919c1bfe3d89d30275018f9c823763af05eb36 Mon Sep 17 00:00:00 2001 From: Bart274 Date: Wed, 13 Jan 2016 12:19:20 +0100 Subject: [PATCH 2/9] Update command_sensor.py --- homeassistant/components/binary_sensor/command_sensor.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/binary_sensor/command_sensor.py b/homeassistant/components/binary_sensor/command_sensor.py index 1f86a86ff14..dd365fec468 100644 --- a/homeassistant/components/binary_sensor/command_sensor.py +++ b/homeassistant/components/binary_sensor/command_sensor.py @@ -1,10 +1,7 @@ """ -homeassistant.components.sensor.command_sensor +homeassistant.components.binary_sensor.command_sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Allows to configure custom shell commands to turn a value for a sensor. - -For more details about this platform, please refer to the documentation at -https://home-assistant.io/components/sensor.command_sensor/ +Allows to configure custom shell commands to turn a value into a logical value for a binary sensor. """ import logging import subprocess @@ -16,7 +13,7 @@ from homeassistant.util import template, Throttle _LOGGER = logging.getLogger(__name__) -DEFAULT_NAME = "Command Sensor" +DEFAULT_NAME = "Binary Command Sensor" DEFAULT_PAYLOAD_ON = 'ON' DEFAULT_PAYLOAD_OFF = 'OFF' From c2e8646aed697a8263e4ec8b4e543f55af78ff2e Mon Sep 17 00:00:00 2001 From: Bart274 Date: Wed, 13 Jan 2016 12:20:37 +0100 Subject: [PATCH 3/9] Update .coveragerc --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index 272ace975c4..4b7dbcf856c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -41,6 +41,7 @@ omit = homeassistant/components/binary_sensor/arest.py homeassistant/components/binary_sensor/rest.py + homeassistant/components/binary_sensor/command_sensor.py homeassistant/components/browser.py homeassistant/components/camera/* homeassistant/components/device_tracker/actiontec.py From d17aa103b4ea2dc67f4c766d14c8cabedc2a794e Mon Sep 17 00:00:00 2001 From: Bart274 Date: Thu, 14 Jan 2016 10:13:57 +0100 Subject: [PATCH 4/9] Update command_sensor.py --- homeassistant/components/binary_sensor/command_sensor.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/binary_sensor/command_sensor.py b/homeassistant/components/binary_sensor/command_sensor.py index dd365fec468..5e59818ee22 100644 --- a/homeassistant/components/binary_sensor/command_sensor.py +++ b/homeassistant/components/binary_sensor/command_sensor.py @@ -1,7 +1,8 @@ """ homeassistant.components.binary_sensor.command_sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Allows to configure custom shell commands to turn a value into a logical value for a binary sensor. +Allows to configure custom shell commands to turn a value +into a logical value for a binary sensor. """ import logging import subprocess @@ -43,8 +44,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None): # pylint: disable=too-many-arguments class CommandBinarySensor(BinarySensorDevice): - """ Represents a binary sensor that is returning a value of a shell commands. """ - def __init__(self, hass, data, name, payload_on, payload_off, value_template): + """ Represents a binary sensor that is returning + a value of a shell commands. """ + def __init__(self, hass, data, name, payload_on, + payload_off, value_template): self._hass = hass self.data = data self._name = name From d0d375d433ce4ef07cd14c5c8a9b0180e8cae886 Mon Sep 17 00:00:00 2001 From: Bart274 Date: Thu, 14 Jan 2016 10:27:11 +0100 Subject: [PATCH 5/9] Update command_sensor.py --- homeassistant/components/binary_sensor/command_sensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/binary_sensor/command_sensor.py b/homeassistant/components/binary_sensor/command_sensor.py index 5e59818ee22..025b2dfe0ba 100644 --- a/homeassistant/components/binary_sensor/command_sensor.py +++ b/homeassistant/components/binary_sensor/command_sensor.py @@ -1,7 +1,7 @@ """ homeassistant.components.binary_sensor.command_sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Allows to configure custom shell commands to turn a value +Allows to configure custom shell commands to turn a value into a logical value for a binary sensor. """ import logging @@ -44,9 +44,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): # pylint: disable=too-many-arguments class CommandBinarySensor(BinarySensorDevice): - """ Represents a binary sensor that is returning + """ Represents a binary sensor that is returning a value of a shell commands. """ - def __init__(self, hass, data, name, payload_on, + def __init__(self, hass, data, name, payload_on, payload_off, value_template): self._hass = hass self.data = data @@ -74,7 +74,7 @@ class CommandBinarySensor(BinarySensorDevice): if self._value_template is not None: value = template.render_with_possible_json_value( - self._hass, self.value_template, value, False) + self._hass, self._value_template, value, False) if value == self._payload_on: self._state = True elif value == self._payload_off: From 41acc8fa4326672c5eb4f327589e1071247f5260 Mon Sep 17 00:00:00 2001 From: Bart274 Date: Fri, 15 Jan 2016 08:59:11 +0100 Subject: [PATCH 6/9] Update .coveragerc This shouldn't be excluded according to @balloob because it's a pure python component --- .coveragerc | 1 - 1 file changed, 1 deletion(-) diff --git a/.coveragerc b/.coveragerc index 4b7dbcf856c..272ace975c4 100644 --- a/.coveragerc +++ b/.coveragerc @@ -41,7 +41,6 @@ omit = homeassistant/components/binary_sensor/arest.py homeassistant/components/binary_sensor/rest.py - homeassistant/components/binary_sensor/command_sensor.py homeassistant/components/browser.py homeassistant/components/camera/* homeassistant/components/device_tracker/actiontec.py From d40e889d3b03918cea48e9a373aaa7cbe4799ad3 Mon Sep 17 00:00:00 2001 From: Bart274 Date: Fri, 15 Jan 2016 09:01:58 +0100 Subject: [PATCH 7/9] Update command_sensor.py Importing CommandSensorData from the command_sensor in sensors in order not to duplicate code --- .../binary_sensor/command_sensor.py | 21 +------------------ 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/homeassistant/components/binary_sensor/command_sensor.py b/homeassistant/components/binary_sensor/command_sensor.py index 025b2dfe0ba..e6b5116a60f 100644 --- a/homeassistant/components/binary_sensor/command_sensor.py +++ b/homeassistant/components/binary_sensor/command_sensor.py @@ -10,6 +10,7 @@ from datetime import timedelta from homeassistant.const import CONF_VALUE_TEMPLATE from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.component.sensor.command_sensor import CommandSensorData from homeassistant.util import template, Throttle _LOGGER = logging.getLogger(__name__) @@ -79,23 +80,3 @@ class CommandBinarySensor(BinarySensorDevice): self._state = True elif value == self._payload_off: self._state = False - - -# pylint: disable=too-few-public-methods -class CommandSensorData(object): - """ Class for handling the data retrieval. """ - - def __init__(self, command): - self.command = command - self.value = None - - @Throttle(MIN_TIME_BETWEEN_UPDATES) - def update(self): - """ Gets the latest data with a shell command. """ - _LOGGER.info('Running command: %s', self.command) - - try: - return_value = subprocess.check_output(self.command, shell=True) - self.value = return_value.strip().decode('utf-8') - except subprocess.CalledProcessError: - _LOGGER.error('Command failed: %s', self.command) From a80917f53012aafb03b7889810740f545d788e61 Mon Sep 17 00:00:00 2001 From: Bart274 Date: Mon, 18 Jan 2016 09:24:38 +0100 Subject: [PATCH 8/9] Update command_sensor.py --- homeassistant/components/binary_sensor/command_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/binary_sensor/command_sensor.py b/homeassistant/components/binary_sensor/command_sensor.py index e6b5116a60f..11c8af141bd 100644 --- a/homeassistant/components/binary_sensor/command_sensor.py +++ b/homeassistant/components/binary_sensor/command_sensor.py @@ -10,7 +10,7 @@ from datetime import timedelta from homeassistant.const import CONF_VALUE_TEMPLATE from homeassistant.components.binary_sensor import BinarySensorDevice -from homeassistant.component.sensor.command_sensor import CommandSensorData +from homeassistant.components.sensor.command_sensor import CommandSensorData from homeassistant.util import template, Throttle _LOGGER = logging.getLogger(__name__) From 3b423900623fc1fc801f638ba4bb94b04ba40506 Mon Sep 17 00:00:00 2001 From: Bart274 Date: Mon, 18 Jan 2016 13:48:09 +0100 Subject: [PATCH 9/9] Update command_sensor.py --- homeassistant/components/binary_sensor/command_sensor.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/binary_sensor/command_sensor.py b/homeassistant/components/binary_sensor/command_sensor.py index 11c8af141bd..8798e457e71 100644 --- a/homeassistant/components/binary_sensor/command_sensor.py +++ b/homeassistant/components/binary_sensor/command_sensor.py @@ -5,13 +5,12 @@ Allows to configure custom shell commands to turn a value into a logical value for a binary sensor. """ import logging -import subprocess from datetime import timedelta from homeassistant.const import CONF_VALUE_TEMPLATE from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.sensor.command_sensor import CommandSensorData -from homeassistant.util import template, Throttle +from homeassistant.util import template _LOGGER = logging.getLogger(__name__)