Merge pull request #608 from fabaff/contact

Binary sensor component
pull/645/head
Paulus Schoutsen 2015-11-20 08:37:10 -08:00
commit 3493db1052
3 changed files with 94 additions and 1 deletions

View File

@ -0,0 +1,50 @@
"""
homeassistant.components.binary_sensor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Component to interface with binary sensors (sensors which only know two states)
that can be monitored.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/binary_sensor/
"""
import logging
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity import Entity
from homeassistant.const import (STATE_ON, STATE_OFF)
DOMAIN = 'binary_sensor'
DEPENDENCIES = []
SCAN_INTERVAL = 30
ENTITY_ID_FORMAT = DOMAIN + '.{}'
def setup(hass, config):
""" Track states and offer events for binary sensors. """
component = EntityComponent(
logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL)
component.setup(config)
return True
# pylint: disable=no-self-use
class BinarySensorDevice(Entity):
""" Represents a binary sensor. """
@property
def is_on(self):
""" True if the binary sensor is on. """
return None
@property
def state(self):
""" Returns the state of the binary sensor. """
return STATE_ON if self.is_on else STATE_OFF
@property
def friendly_state(self):
""" Returns the friendly state of the binary sensor. """
return None

View File

@ -0,0 +1,43 @@
"""
homeassistant.components.binary_sensor.demo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo platform that has two fake binary sensors.
"""
from homeassistant.components.binary_sensor import BinarySensorDevice
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the Demo binary sensors. """
add_devices([
DemoBinarySensor('Window Bathroom', True, None),
DemoBinarySensor('Floor Basement', False, None),
])
class DemoBinarySensor(BinarySensorDevice):
""" A Demo binary sensor. """
def __init__(self, name, state, icon=None):
self._name = name
self._state = state
self._icon = icon
@property
def should_poll(self):
""" No polling needed for a demo binary sensor. """
return False
@property
def name(self):
""" Returns the name of the binary sensor. """
return self._name
@property
def icon(self):
""" Returns the icon to use for device if any. """
return self._icon
@property
def is_on(self):
""" True if the binary sensor is on. """
return self._state

View File

@ -18,7 +18,7 @@ DEPENDENCIES = ['conversation', 'introduction', 'zone']
COMPONENTS_WITH_DEMO_PLATFORM = [
'device_tracker', 'light', 'media_player', 'notify', 'switch', 'sensor',
'thermostat', 'camera']
'thermostat', 'camera', 'binary_sensor']
def setup(hass, config):