mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-08-11 18:29:43 +00:00
.github
.themes
_deploy
credits_generator
plugins
sass
source
_addons
_components
_cookbook
automation_enocean_phue.markdown
automation_first_light.markdown
automation_flashing_lights.markdown
automation_for_rainy_days.markdown
automation_kodi_dynamic_input_select.markdown
automation_sun.markdown
automation_telegram_presence_alert.markdown
automation_using_timeinterval_inputboolean.markdown
configuration_yaml_by_alok_saboo.markdown
configuration_yaml_by_aneisch.markdown
configuration_yaml_by_apocrathia.markdown
configuration_yaml_by_bah2830.markdown
configuration_yaml_by_carlo_costanzo.markdown
configuration_yaml_by_cbulock.markdown
configuration_yaml_by_chriskacerguis.markdown
configuration_yaml_by_ciquattrofpv.markdown
configuration_yaml_by_cy1701.markdown
configuration_yaml_by_danichispa.markdown
configuration_yaml_by_daniel_hoyer_iversen.markdown
configuration_yaml_by_fredsmith.markdown
configuration_yaml_by_geekofweek.markdown
configuration_yaml_by_greenturtwig.markdown
configuration_yaml_by_gstevenson.markdown
configuration_yaml_by_gummientchen.markdown
configuration_yaml_by_happyleavesaoc.markdown
configuration_yaml_by_instagraeme.markdown
configuration_yaml_by_jjmontesl.markdown
configuration_yaml_by_lancehaynie.markdown
configuration_yaml_by_mcaminiti.markdown
configuration_yaml_by_mertenats.markdown
configuration_yaml_by_mf_social.markdown
configuration_yaml_by_ntalekt.markdown
configuration_yaml_by_oakbrad.markdown
configuration_yaml_by_scottocs11.markdown
configuration_yaml_by_shortbloke.markdown
configuration_yaml_by_silvrr.markdown
configuration_yaml_by_skalavala.markdown
configuration_yaml_by_stanvx.markdown
configuration_yaml_by_teagan42.markdown
configuration_yaml_by_tinkerer.markdown
configuration_yaml_by_vasiley.markdown
configuration_yaml_from_bassclarinetl2.markdown
custom_panel_using_react.markdown
custom_ui_by_andrey-git.markdown
dim_and_brighten_lights.markdown
dim_lights_when_playing_media.markdown
fail2ban.markdown
foscam_away_mode_PTZ.markdown
google_maps_card.markdown
notify_if__new_ha_release.markdown
notify_if_over_threshold.markdown
owntracks_two_mqtt_broker.markdown
perform_actions_based_on_input_select.markdown
python_component_automation.markdown
python_component_mqtt_basic.markdown
python_component_simple_alarm.markdown
restart_ha_if_wemo_switch_is_not_detected.markdown
send_a_reminder.markdown
sonos_say.markdown
track_battery_level.markdown
turn_on_light_for_10_minutes_when_motion_detected.markdown
_data
_docs
_faq
_includes
_layouts
_lovelace
_posts
addons
assets
blog
cloud
code_of_conduct
components
cookbook
demo
developers
docs
faq
font
getting-started
hassio
help
images
ios
javascripts
join-chat
latest-release-notes
lovelace
privacy
static
tos
CNAME
atom.xml
favicon.png
googlef4f3693c209fe788.html
index.html
robots.txt
service_worker.js
version.json
.editorconfig
.gitattributes
.gitignore
.gitmodules
.powrc
.project
.ruby-version
.slugignore
.travis.yml
CLA.md
CODE_OF_CONDUCT.md
Gemfile
Gemfile.lock
LICENSE.md
README.markdown
Rakefile
_config.yml
config.rb
config.ru
4.2 KiB
4.2 KiB
layout, title, description, date, sidebar, comments, sharing, footer, ha_category
layout | title | description | date | sidebar | comments | sharing | footer | ha_category |
---|---|---|---|---|---|---|---|---|
page | Flash lights when intruder detected | Detect intruders by checking if the light is turning on while no one is home. | 2016-02-14 0:40 -0800 | true | false | true | true | Automation in Python Examples |
This example component will detect intruders. It does so by checking if lights are being turned on while there is no one at home. When this happens it will turn the lights red, flash them for 30 seconds and send a message via the notify component. It will also flash a specific light when a known person comes home.
This component depends on the components device_tracker and light being setup.
To set it up, add the following lines to your configuration.yaml
file:
# Example configuration.yaml entry
simple_alarm:
known_light: light.Bowl
unknown_light: group.living_room
Configuration variables:
- known_light (Optional): Which light/light group has to flash when a known device comes home.
- unknown_light (Optional): Which light/light group has to flash red when light turns on while no one home.
Create the file <config dir>/custom_components/simple_alarm.py
and copy paste the content below:
"""Simple alarm component."""
import logging
import homeassistant.loader as loader
from homeassistant.components import device_tracker, light, notify
from homeassistant.helpers.event import track_state_change
from homeassistant.const import STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'simple_alarm"'
DEPENDENCIES = ['group', 'device_tracker', 'light']
# Attribute to tell which light has to flash when a known person comes home
# If omitted will flash all.
CONF_KNOWN_LIGHT = 'known_light'
# Attribute to tell which light has to flash when an unknown person comes home
# If omitted 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):
"""Set up the simple alarms."""
light_ids = []
for conf_key in (CONF_KNOWN_LIGHT, CONF_UNKNOWN_LIGHT):
light_id = config[DOMAIN].get(conf_key, 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
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])
# Send a message to the user
notify.send_message(
hass, "The lights just got turned on while no one was home.")
# 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())
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()
track_state_change(
hass, light.ENTITY_ID_ALL_LIGHTS,
unknown_alarm_if_lights_on, STATE_OFF, STATE_ON)
def ring_known_alarm(entity_id, old_state, new_state):
"""Called when a known person comes home."""
if light.is_on(hass, known_light_id):
known_alarm()
# Track home coming of each device
track_state_change(
hass, hass.states.entity_ids(device_tracker.DOMAIN),
ring_known_alarm, STATE_NOT_HOME, STATE_HOME)
return True