core/homeassistant/components/sensor/command_line.py

110 lines
3.4 KiB
Python
Raw Normal View History

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
2016-05-04 22:32:11 +00:00
https://home-assistant.io/components/sensor.command_line/
2015-09-13 09:38:06 +00:00
"""
from datetime import timedelta
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
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME, CONF_VALUE_TEMPLATE, CONF_UNIT_OF_MEASUREMENT, CONF_COMMAND,
STATE_UNKNOWN)
2015-09-13 09:38:06 +00:00
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
2015-09-13 09:38:06 +00:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'Command Sensor'
2015-09-13 09:38:06 +00:00
SCAN_INTERVAL = timedelta(seconds=60)
2015-09-13 09:38:06 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_COMMAND): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
})
2015-09-13 09:38:06 +00:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Command Sensor."""
name = config.get(CONF_NAME)
command = config.get(CONF_COMMAND)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
value_template = config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
value_template.hass = hass
data = CommandSensorData(command)
add_devices([CommandSensor(hass, data, name, unit, value_template)])
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 = STATE_UNKNOWN
2015-09-13 09:38:06 +00:00
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
if value is None:
value = STATE_UNKNOWN
elif self._value_template is not None:
self._state = self._value_template.render_with_possible_json_value(
value, STATE_UNKNOWN)
2015-12-14 17:49:54 +00:00
else:
self._state = value
2015-09-13 09:38:06 +00:00
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
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest data with a shell command."""
_LOGGER.info("Running command: %s", self.command)
2015-09-13 09:38:06 +00:00
try:
return_value = subprocess.check_output(
self.command, shell=True, timeout=15)
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:
_LOGGER.error("Command failed: %s", self.command)
2016-07-30 17:36:56 +00:00
except subprocess.TimeoutExpired:
_LOGGER.error("Timeout for command: %s", self.command)