Add sensor_class to binary_sensor

This adds a 'sensor_class' property and attribute, which should be either
None or one of several defined SENSOR_CLASSES to indicate contextual
information about what the sensor is measuring.
This commit is contained in:
Dan Smith 2016-02-17 15:58:31 -08:00
parent 182fcb5f03
commit 2d932f89fc
2 changed files with 58 additions and 0 deletions

View file

@ -17,6 +17,17 @@ DOMAIN = 'binary_sensor'
SCAN_INTERVAL = 30
ENTITY_ID_FORMAT = DOMAIN + '.{}'
SENSOR_CLASSES = [
None, # Generic on/off
'opening', # Door, window, etc
'motion', # Motion sensor
'gas', # CO, CO2, etc
'smoke', # Smoke detector
'moisture', # Specifically a wetness sensor
'light', # Lightness threshold
'power', # Power, over-current, etc
'safety', # Generic on=unsafe, off=safe
]
def setup(hass, config):
@ -47,3 +58,14 @@ class BinarySensorDevice(Entity):
def friendly_state(self):
""" Returns the friendly state of the binary sensor. """
return None
@property
def sensor_class(self):
""" Returns the class of this sensor, from SENSOR_CASSES. """
return None
@property
def device_state_attributes(self):
return {
'sensor_class': self.sensor_class,
}

View file

@ -0,0 +1,36 @@
"""
tests.components.binary_sensor.test_binary_sensor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test the binary_sensor base class
"""
import unittest
from unittest import mock
from homeassistant.components import binary_sensor
from homeassistant.const import STATE_ON, STATE_OFF
class TestBinarySensor(unittest.TestCase):
def test_state(self):
sensor = binary_sensor.BinarySensorDevice()
self.assertEqual(STATE_OFF, sensor.state)
with mock.patch('homeassistant.components.binary_sensor.'
'BinarySensorDevice.is_on',
new=False):
self.assertEqual(STATE_OFF,
binary_sensor.BinarySensorDevice().state)
with mock.patch('homeassistant.components.binary_sensor.'
'BinarySensorDevice.is_on',
new=True):
self.assertEqual(STATE_ON,
binary_sensor.BinarySensorDevice().state)
def test_attributes(self):
sensor = binary_sensor.BinarySensorDevice()
self.assertEqual({'sensor_class': None},
sensor.device_state_attributes)
with mock.patch('homeassistant.components.binary_sensor.'
'BinarySensorDevice.sensor_class',
new='motion'):
self.assertEqual({'sensor_class': 'motion'},
sensor.device_state_attributes)