Merge pull request #743 from philipbl/command_sensor
Command Sensor: Add support for templatepull/923/head^2
commit
946f6cab9d
|
@ -10,8 +10,9 @@ import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from homeassistant.const import CONF_VALUE_TEMPLATE
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import template, Throttle
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -32,25 +33,24 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
data = CommandSensorData(config.get('command'))
|
data = CommandSensorData(config.get('command'))
|
||||||
|
|
||||||
add_devices_callback([CommandSensor(
|
add_devices_callback([CommandSensor(
|
||||||
|
hass,
|
||||||
data,
|
data,
|
||||||
config.get('name', DEFAULT_NAME),
|
config.get('name', DEFAULT_NAME),
|
||||||
config.get('unit_of_measurement'),
|
config.get('unit_of_measurement'),
|
||||||
config.get('correction_factor', None),
|
config.get(CONF_VALUE_TEMPLATE)
|
||||||
config.get('decimal_places', None)
|
|
||||||
)])
|
)])
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments
|
||||||
class CommandSensor(Entity):
|
class CommandSensor(Entity):
|
||||||
""" Represents a sensor that is returning a value of a shell commands. """
|
""" Represents a sensor that is returning a value of a shell commands. """
|
||||||
def __init__(self, data, name, unit_of_measurement, corr_factor,
|
def __init__(self, hass, data, name, unit_of_measurement, value_template):
|
||||||
decimal_places):
|
self._hass = hass
|
||||||
self.data = data
|
self.data = data
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = False
|
self._state = False
|
||||||
self._unit_of_measurement = unit_of_measurement
|
self._unit_of_measurement = unit_of_measurement
|
||||||
self._corr_factor = corr_factor
|
self._value_template = value_template
|
||||||
self._decimal_places = decimal_places
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -73,16 +73,10 @@ class CommandSensor(Entity):
|
||||||
self.data.update()
|
self.data.update()
|
||||||
value = self.data.value
|
value = self.data.value
|
||||||
|
|
||||||
try:
|
if self._value_template is not None:
|
||||||
if value is not None:
|
self._state = template.render_with_possible_json_value(
|
||||||
if self._corr_factor is not None:
|
self._hass, self._value_template, value, 'N/A')
|
||||||
value = float(value) * float(self._corr_factor)
|
else:
|
||||||
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 = value
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue