Add support for sensor classes (#1950)
parent
069a4b1706
commit
4710b38fad
|
@ -7,7 +7,8 @@ https://home-assistant.io/components/binary_sensor.command/
|
||||||
import logging
|
import logging
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
from homeassistant.components.binary_sensor import (BinarySensorDevice,
|
||||||
|
SENSOR_CLASSES)
|
||||||
from homeassistant.components.sensor.command_line import CommandSensorData
|
from homeassistant.components.sensor.command_line import CommandSensorData
|
||||||
from homeassistant.const import CONF_VALUE_TEMPLATE
|
from homeassistant.const import CONF_VALUE_TEMPLATE
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
|
@ -15,6 +16,7 @@ from homeassistant.helpers import template
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = "Binary Command Sensor"
|
DEFAULT_NAME = "Binary Command Sensor"
|
||||||
|
DEFAULT_SENSOR_CLASS = None
|
||||||
DEFAULT_PAYLOAD_ON = 'ON'
|
DEFAULT_PAYLOAD_ON = 'ON'
|
||||||
DEFAULT_PAYLOAD_OFF = 'OFF'
|
DEFAULT_PAYLOAD_OFF = 'OFF'
|
||||||
|
|
||||||
|
@ -29,28 +31,35 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
_LOGGER.error('Missing required variable: "command"')
|
_LOGGER.error('Missing required variable: "command"')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
sensor_class = config.get('sensor_class')
|
||||||
|
if sensor_class not in SENSOR_CLASSES:
|
||||||
|
_LOGGER.warning('Unknown sensor class: %s', sensor_class)
|
||||||
|
sensor_class = DEFAULT_SENSOR_CLASS
|
||||||
|
|
||||||
data = CommandSensorData(config.get('command'))
|
data = CommandSensorData(config.get('command'))
|
||||||
|
|
||||||
add_devices([CommandBinarySensor(
|
add_devices([CommandBinarySensor(
|
||||||
hass,
|
hass,
|
||||||
data,
|
data,
|
||||||
config.get('name', DEFAULT_NAME),
|
config.get('name', DEFAULT_NAME),
|
||||||
|
sensor_class,
|
||||||
config.get('payload_on', DEFAULT_PAYLOAD_ON),
|
config.get('payload_on', DEFAULT_PAYLOAD_ON),
|
||||||
config.get('payload_off', DEFAULT_PAYLOAD_OFF),
|
config.get('payload_off', DEFAULT_PAYLOAD_OFF),
|
||||||
config.get(CONF_VALUE_TEMPLATE)
|
config.get(CONF_VALUE_TEMPLATE)
|
||||||
)])
|
)])
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
||||||
class CommandBinarySensor(BinarySensorDevice):
|
class CommandBinarySensor(BinarySensorDevice):
|
||||||
"""Represent a command line binary sensor."""
|
"""Represent a command line binary sensor."""
|
||||||
|
|
||||||
def __init__(self, hass, data, name, payload_on,
|
def __init__(self, hass, data, name, sensor_class, payload_on,
|
||||||
payload_off, value_template):
|
payload_off, value_template):
|
||||||
"""Initialize the Command line binary sensor."""
|
"""Initialize the Command line binary sensor."""
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
self.data = data
|
self.data = data
|
||||||
self._name = name
|
self._name = name
|
||||||
|
self._sensor_class = sensor_class
|
||||||
self._state = False
|
self._state = False
|
||||||
self._payload_on = payload_on
|
self._payload_on = payload_on
|
||||||
self._payload_off = payload_off
|
self._payload_off = payload_off
|
||||||
|
@ -67,6 +76,11 @@ class CommandBinarySensor(BinarySensorDevice):
|
||||||
"""Return true if the binary sensor is on."""
|
"""Return true if the binary sensor is on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
|
@ property
|
||||||
|
def sensor_class(self):
|
||||||
|
"""Return the class of the binary sensor."""
|
||||||
|
return self._sensor_class
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data and updates the state."""
|
"""Get the latest data and updates the state."""
|
||||||
self.data.update()
|
self.data.update()
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
"""The tests fr the Command line Binary sensor platform."""
|
"""The tests for the Command line Binary sensor platform."""
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from homeassistant.const import (STATE_ON, STATE_OFF)
|
from homeassistant.const import (STATE_ON, STATE_OFF)
|
||||||
|
@ -60,7 +60,8 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
|
||||||
data = command_line.CommandSensorData('echo 10')
|
data = command_line.CommandSensorData('echo 10')
|
||||||
|
|
||||||
entity = command_line.CommandBinarySensor(
|
entity = command_line.CommandBinarySensor(
|
||||||
self.hass, data, 'test', '1.0', '0', '{{ value | multiply(0.1) }}')
|
self.hass, data, 'test', None, '1.0', '0',
|
||||||
|
'{{ value | multiply(0.1) }}')
|
||||||
|
|
||||||
self.assertEqual(STATE_ON, entity.state)
|
self.assertEqual(STATE_ON, entity.state)
|
||||||
|
|
||||||
|
@ -69,6 +70,6 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
|
||||||
data = command_line.CommandSensorData('echo 0')
|
data = command_line.CommandSensorData('echo 0')
|
||||||
|
|
||||||
entity = command_line.CommandBinarySensor(
|
entity = command_line.CommandBinarySensor(
|
||||||
self.hass, data, 'test', '1', '0', None)
|
self.hass, data, 'test', None, '1', '0', None)
|
||||||
|
|
||||||
self.assertEqual(STATE_OFF, entity.state)
|
self.assertEqual(STATE_OFF, entity.state)
|
||||||
|
|
Loading…
Reference in New Issue