From 5f89db59577b78e0f3117cf10d5cdcf2480b1641 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 19 Nov 2015 19:00:22 +0100 Subject: [PATCH 1/4] Binary sensor component --- .../components/binary_sensor/__init__.py | 44 +++++++++++++++++ .../components/binary_sensor/demo.py | 49 +++++++++++++++++++ homeassistant/components/demo.py | 2 +- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/binary_sensor/__init__.py create mode 100644 homeassistant/components/binary_sensor/demo.py diff --git a/homeassistant/components/binary_sensor/__init__.py b/homeassistant/components/binary_sensor/__init__.py new file mode 100644 index 00000000000..b02d38a9f81 --- /dev/null +++ b/homeassistant/components/binary_sensor/__init__.py @@ -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 diff --git a/homeassistant/components/binary_sensor/demo.py b/homeassistant/components/binary_sensor/demo.py new file mode 100644 index 00000000000..266b46b7e17 --- /dev/null +++ b/homeassistant/components/binary_sensor/demo.py @@ -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 diff --git a/homeassistant/components/demo.py b/homeassistant/components/demo.py index 7c873e834bd..3022a0e9393 100644 --- a/homeassistant/components/demo.py +++ b/homeassistant/components/demo.py @@ -18,7 +18,7 @@ DEPENDENCIES = ['conversation', 'introduction', 'zone'] COMPONENTS_WITH_DEMO_PLATFORM = [ 'device_tracker', 'light', 'media_player', 'notify', 'switch', 'sensor', - 'thermostat'] + 'thermostat', 'binary_sensor'] def setup(hass, config): From b9730e6914f4dfece1656bb587492545640e3ead Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 19 Nov 2015 19:00:22 +0100 Subject: [PATCH 2/4] Binary sensor component --- .../components/binary_sensor/__init__.py | 44 +++++++++++++++++ .../components/binary_sensor/demo.py | 49 +++++++++++++++++++ homeassistant/components/demo.py | 2 +- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/binary_sensor/__init__.py create mode 100644 homeassistant/components/binary_sensor/demo.py diff --git a/homeassistant/components/binary_sensor/__init__.py b/homeassistant/components/binary_sensor/__init__.py new file mode 100644 index 00000000000..b02d38a9f81 --- /dev/null +++ b/homeassistant/components/binary_sensor/__init__.py @@ -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 diff --git a/homeassistant/components/binary_sensor/demo.py b/homeassistant/components/binary_sensor/demo.py new file mode 100644 index 00000000000..266b46b7e17 --- /dev/null +++ b/homeassistant/components/binary_sensor/demo.py @@ -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 diff --git a/homeassistant/components/demo.py b/homeassistant/components/demo.py index c8888a71c1b..547a1d54a03 100644 --- a/homeassistant/components/demo.py +++ b/homeassistant/components/demo.py @@ -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): From a6006b1835d53292c48050f32ec61118369ece86 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 20 Nov 2015 14:58:49 +0100 Subject: [PATCH 3/4] Move state --- homeassistant/components/binary_sensor/__init__.py | 10 ++++++++-- homeassistant/components/binary_sensor/demo.py | 10 ++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/binary_sensor/__init__.py b/homeassistant/components/binary_sensor/__init__.py index b02d38a9f81..2ef9e83cc30 100644 --- a/homeassistant/components/binary_sensor/__init__.py +++ b/homeassistant/components/binary_sensor/__init__.py @@ -11,6 +11,7 @@ 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 = [] @@ -31,12 +32,17 @@ def setup(hass, config): # pylint: disable=no-self-use class BinarySensorDevice(Entity): - """ Represents a binary sensor.. """ + """ Represents a binary sensor. """ @property def is_on(self): """ True if the binary sensor is on. """ - return False + 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): diff --git a/homeassistant/components/binary_sensor/demo.py b/homeassistant/components/binary_sensor/demo.py index 266b46b7e17..a24b893c610 100644 --- a/homeassistant/components/binary_sensor/demo.py +++ b/homeassistant/components/binary_sensor/demo.py @@ -4,7 +4,6 @@ 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): @@ -19,7 +18,7 @@ class DemoBinarySensor(BinarySensorDevice): """ A Demo binary sensor. """ def __init__(self, name, state, icon=None): - self._name = name or DEVICE_DEFAULT_NAME + self._name = name self._state = state self._icon = icon @@ -40,10 +39,5 @@ class DemoBinarySensor(BinarySensorDevice): @property def is_on(self): - """ True if the sensor is on. """ + """ True if the binary 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 From d254e7e9e50271442f5c680cc5ff1b957ac96639 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 20 Nov 2015 15:29:36 +0100 Subject: [PATCH 4/4] Fix pylint issue --- homeassistant/components/binary_sensor/demo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/binary_sensor/demo.py b/homeassistant/components/binary_sensor/demo.py index 22eeaa7b8a5..a24b893c610 100644 --- a/homeassistant/components/binary_sensor/demo.py +++ b/homeassistant/components/binary_sensor/demo.py @@ -41,4 +41,3 @@ class DemoBinarySensor(BinarySensorDevice): def is_on(self): """ True if the binary sensor is on. """ return self._state -