1
mirror of https://github.com/home-assistant/core synced 2024-09-12 15:16:21 +02:00

Binary sensor component

This commit is contained in:
Fabian Affolter 2015-11-19 19:00:22 +01:00
parent 037bca041e
commit b9730e6914
3 changed files with 94 additions and 1 deletions

View File

@ -0,0 +1,44 @@
"""
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
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 False
@property
def friendly_state(self):
""" Returns the friendly state of the binary sensor. """
return None

View File

@ -0,0 +1,49 @@
"""
homeassistant.components.binary_sensor.demo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo platform that has two fake binary sensors.
"""
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.const import (STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME)
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 or DEVICE_DEFAULT_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 sensor is on. """
return self._state
@property
def state(self):
""" Returns the state of the binary sensor. """
return STATE_ON if self.is_on else STATE_OFF

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):