Added a simple_alarm component

This commit is contained in:
Paulus Schoutsen 2014-12-08 23:47:20 -08:00
parent 6044742cee
commit dd55d6c7f9
3 changed files with 110 additions and 0 deletions

View File

@ -226,6 +226,15 @@ Registers service `browser/browse_url` that opens `url` as specified in event_da
**tellstick_sensor**
Shows the values of that sensors that is connected to your Tellstick.
**simple_alarm**
Will provide simple alarm functionality. Will flash a light shortly if a known device comes home. Will flash the lights red if the lights turn on while no one is home.
Depends on device_tracker, light.
Config options:
known_light: entity id of the light/light group to target to flash when a known device comes home
unknown_light: entity if of the light/light group to target when a light is turned on while no one is at home.
<a name='API'></a>
## Rest API

View File

@ -53,6 +53,12 @@ xbmc=XBMC.App
[example]
[simple_alarm]
# Which light/light group has to flash when a known device comes home
known_light=light.Bowl
# Which light/light group has to flash red when light turns on while no one home
unknown_light=group.living_room
[browser]
[keyboard]

View File

@ -0,0 +1,95 @@
"""
homeassistant.components.simple_alarm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Provides a simple alarm feature:
- flash a light when a known device comes home
- flash a light red if a light turns on while there is no one home.
"""
import logging
import homeassistant.loader as loader
from homeassistant.const import STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME
DOMAIN = "simple_alarm"
DEPENDENCIES = ['group', 'device_tracker', 'light']
# Attribute to tell which light has to flash whem a known person comes home
# If ommitted will flash all.
CONF_KNOWN_LIGHT = "known_light"
# Attribute to tell which light has to flash whem an unknown person comes home
# If ommitted will flash all.
CONF_UNKNOWN_LIGHT = "unknown_light"
# Services to test the alarms
SERVICE_TEST_KNOWN_ALARM = "test_known"
SERVICE_TEST_UNKNOWN_ALARM = "test_unknown"
def setup(hass, config):
""" Sets up the simple alarms. """
logger = logging.getLogger(__name__)
device_tracker = loader.get_component('device_tracker')
light = loader.get_component('light')
light_ids = []
for conf_key in (CONF_KNOWN_LIGHT, CONF_UNKNOWN_LIGHT):
light_id = config[DOMAIN].get(conf_key) or light.ENTITY_ID_ALL_LIGHTS
if hass.states.get(light_id) is None:
logger.error(
'Light id %s could not be found in state machine', light_id)
return False
else:
light_ids.append(light_id)
# pylint: disable=unbalanced-tuple-unpacking
known_light_id, unknown_light_id = light_ids
if hass.states.get(device_tracker.ENTITY_ID_ALL_DEVICES) is None:
logger.error('No devices are being tracked, cannot setup alarm')
return False
def known_alarm():
""" Fire an alarm if a known person arrives home. """
light.turn_on(hass, known_light_id, flash=light.FLASH_SHORT)
def unknown_alarm():
""" Fire an alarm if the light turns on while no one is home. """
light.turn_on(
hass, unknown_light_id,
flash=light.FLASH_LONG, rgb_color=[255, 0, 0])
# Setup services to test the effect
hass.services.register(
DOMAIN, SERVICE_TEST_KNOWN_ALARM, lambda call: known_alarm())
hass.services.register(
DOMAIN, SERVICE_TEST_UNKNOWN_ALARM, lambda call: unknown_alarm())
# pylint: disable=unused-argument
def unknown_alarm_if_lights_on(entity_id, old_state, new_state):
""" Called when a light has been turned on. """
if not device_tracker.is_on(hass):
unknown_alarm()
hass.states.track_change(
light.ENTITY_ID_ALL_LIGHTS,
unknown_alarm_if_lights_on, STATE_OFF, STATE_ON)
# Not defined as a lambda so the __repr__ has a nice name.
# pylint: disable=unused-argument
def ring_known_alarm(entity_id, old_state, new_state):
""" Called when a known person comes home. """
known_alarm()
# Track home coming of each device
hass.states.track_change(
hass.states.entity_ids(device_tracker.DOMAIN),
ring_known_alarm, STATE_NOT_HOME, STATE_HOME)