From 600831cff5a6da5fdfc873dc72f78e4ea7bf717c Mon Sep 17 00:00:00 2001 From: Philip Lundrigan Date: Mon, 14 Dec 2015 10:29:27 -0700 Subject: [PATCH 1/2] Add support for template --- .../components/sensor/command_sensor.py | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/sensor/command_sensor.py b/homeassistant/components/sensor/command_sensor.py index e60723f6bfa..1fd30a8bdb5 100644 --- a/homeassistant/components/sensor/command_sensor.py +++ b/homeassistant/components/sensor/command_sensor.py @@ -10,8 +10,9 @@ import logging import subprocess from datetime import timedelta +from homeassistant.const import CONF_VALUE_TEMPLATE from homeassistant.helpers.entity import Entity -from homeassistant.util import Throttle +from homeassistant.util import template, Throttle _LOGGER = logging.getLogger(__name__) @@ -32,25 +33,24 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): data = CommandSensorData(config.get('command')) add_devices_callback([CommandSensor( + hass, data, config.get('name', DEFAULT_NAME), config.get('unit_of_measurement'), - config.get('correction_factor', None), - config.get('decimal_places', None) + config.get(CONF_VALUE_TEMPLATE) )]) # pylint: disable=too-many-arguments class CommandSensor(Entity): """ Represents a sensor that is returning a value of a shell commands. """ - def __init__(self, data, name, unit_of_measurement, corr_factor, - decimal_places): + def __init__(self, hass, data, name, unit_of_measurement, value_template): + self._hass = hass self.data = data self._name = name self._state = False self._unit_of_measurement = unit_of_measurement - self._corr_factor = corr_factor - self._decimal_places = decimal_places + self._value_template = value_template self.update() @property @@ -73,17 +73,8 @@ class CommandSensor(Entity): self.data.update() value = self.data.value - try: - if value is not None: - if self._corr_factor is not None: - value = float(value) * float(self._corr_factor) - if self._decimal_places is not None: - value = round(value, self._decimal_places) - if self._decimal_places == 0: - value = int(value) - self._state = value - except ValueError: - self._state = value + self._state = template.render_with_possible_json_value( + self._hass, self._value_template, value, 'N/A') # pylint: disable=too-few-public-methods From e821b546d5666f5a51d26738e50d9b0f2a359b9a Mon Sep 17 00:00:00 2001 From: Philip Lundrigan Date: Mon, 14 Dec 2015 10:49:54 -0700 Subject: [PATCH 2/2] Ignore template if it isn't set --- homeassistant/components/sensor/command_sensor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/command_sensor.py b/homeassistant/components/sensor/command_sensor.py index 1fd30a8bdb5..95751e73645 100644 --- a/homeassistant/components/sensor/command_sensor.py +++ b/homeassistant/components/sensor/command_sensor.py @@ -73,8 +73,11 @@ class CommandSensor(Entity): self.data.update() value = self.data.value - self._state = template.render_with_possible_json_value( - self._hass, self._value_template, value, 'N/A') + if self._value_template is not None: + self._state = template.render_with_possible_json_value( + self._hass, self._value_template, value, 'N/A') + else: + self._state = value # pylint: disable=too-few-public-methods