2015-09-13 09:38:06 +00:00
|
|
|
"""
|
|
|
|
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
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/sensor.command_sensor/
|
2015-09-13 09:38:06 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2015-09-14 08:44:07 +00:00
|
|
|
import subprocess
|
2015-09-13 09:38:06 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2015-12-14 17:29:27 +00:00
|
|
|
from homeassistant.const import CONF_VALUE_TEMPLATE
|
2015-09-13 09:38:06 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-02-23 20:06:50 +00:00
|
|
|
from homeassistant.helpers import template
|
|
|
|
from homeassistant.util import Throttle
|
2015-09-13 09:38:06 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = "Command Sensor"
|
|
|
|
|
|
|
|
# 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_callback, discovery_info=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Setup the Command Sensor."""
|
2015-09-13 09:38:06 +00:00
|
|
|
if config.get('command') is None:
|
|
|
|
_LOGGER.error('Missing required variable: "command"')
|
|
|
|
return False
|
|
|
|
|
|
|
|
data = CommandSensorData(config.get('command'))
|
|
|
|
|
|
|
|
add_devices_callback([CommandSensor(
|
2015-12-14 17:29:27 +00:00
|
|
|
hass,
|
2015-09-13 09:38:06 +00:00
|
|
|
data,
|
|
|
|
config.get('name', DEFAULT_NAME),
|
|
|
|
config.get('unit_of_measurement'),
|
2015-12-14 17:29:27 +00:00
|
|
|
config.get(CONF_VALUE_TEMPLATE)
|
2015-09-13 09:38:06 +00:00
|
|
|
)])
|
|
|
|
|
|
|
|
|
2015-09-15 06:37:43 +00:00
|
|
|
# pylint: disable=too-many-arguments
|
2015-09-13 09:38:06 +00:00
|
|
|
class CommandSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a sensor that is using shell commands."""
|
|
|
|
|
2015-12-14 17:29:27 +00:00
|
|
|
def __init__(self, hass, data, name, unit_of_measurement, value_template):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-12-14 17:29:27 +00:00
|
|
|
self._hass = hass
|
2015-09-13 09:38:06 +00:00
|
|
|
self.data = data
|
|
|
|
self._name = name
|
|
|
|
self._state = False
|
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2015-12-14 17:29:27 +00:00
|
|
|
self._value_template = value_template
|
2015-09-13 09:38:06 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-09-13 09:38:06 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit the value is expressed in."""
|
2015-09-13 09:38:06 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the device."""
|
2015-09-13 09:38:06 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data and updates the state."""
|
2015-09-13 09:38:06 +00:00
|
|
|
self.data.update()
|
|
|
|
value = self.data.value
|
|
|
|
|
2015-12-14 17:49:54 +00:00
|
|
|
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
|
2015-09-13 09:38:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class CommandSensorData(object):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""The class for handling the data retrieval."""
|
2015-09-13 09:38:06 +00:00
|
|
|
|
|
|
|
def __init__(self, command):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the data object."""
|
2015-09-13 09:38:06 +00:00
|
|
|
self.command = command
|
|
|
|
self.value = None
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data with a shell command."""
|
2015-09-13 09:38:06 +00:00
|
|
|
_LOGGER.info('Running command: %s', self.command)
|
|
|
|
|
|
|
|
try:
|
2015-10-24 19:40:36 +00:00
|
|
|
return_value = subprocess.check_output(self.command, shell=True)
|
2015-09-13 09:38:06 +00:00
|
|
|
self.value = return_value.strip().decode('utf-8')
|
2015-09-14 08:44:07 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2015-09-13 09:38:06 +00:00
|
|
|
_LOGGER.error('Command failed: %s', self.command)
|