home-assistant.github.io/cookbook/python_component_simple_alarm/index.html
2016-04-08 08:33:10 +00:00

297 lines
No EOL
15 KiB
HTML

<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Flash lights when intruder detected - Home Assistant</title>
<meta name="author" content="Paulus Schoutsen">
<meta name="description" content="Detect intruders by checking if the light is turning on while no one is home.">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/cookbook/python_component_simple_alarm/">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="Flash lights when intruder detected">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/cookbook/python_component_simple_alarm/">
<meta property="og:type" content="article">
<meta property="og:description" content="Detect intruders by checking if the light is turning on while no one is home.">
<meta property="og:image" content="https://home-assistant.io/images/home-assistant-logo-2164x2164.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@home_assistant">
<meta name="twitter:title" content="Flash lights when intruder detected">
<meta name="twitter:description" content="Detect intruders by checking if the light is turning on while no one is home.">
<meta name="twitter:image" content="https://home-assistant.io/images/home-assistant-logo-2164x2164.png">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet">
<link href="/atom.xml" rel="alternate" title="Home Assistant" type="application/atom+xml">
<link rel='shortcut icon' href='/images/favicon.ico' />
<link rel='icon' type='image/png' href='/images/favicon-192x192.png' sizes='192x192' />
</head>
<body >
<header>
<div class="grid-wrapper">
<div class="grid">
<div class="grid__item three-tenths lap-two-sixths palm-one-whole ha-title">
<a href="/" class="site-title">
<img width='40' src='/demo/favicon-192x192.png'>
<span>Home Assistant</span>
</a>
</div>
<div class="grid__item seven-tenths lap-four-sixths palm-one-whole">
<nav>
<input type="checkbox" id="toggle">
<label for="toggle" class="toggle" data-open="Main Menu" data-close="Close Menu"></label>
<ul class="menu pull-right">
<li>
<a>Getting started <i class="icon icon-caret-down"></i></a>
<ul>
<li><a href='/getting-started/'>Installing Home Assistant</a></li>
<li><a href='/getting-started/configuration/'>Configuration Basics</a></li>
<li><a href='/getting-started/devices/'>Adding devices</a></li>
<li><a href='/getting-started/presence-detection/'>Presence Detection</a></li>
<li><a href='/getting-started/automation/'>Automation</a></li>
<li><a href='/topics/'>Advanced Topics</a></li>
</ul>
</li>
<li><a href='/components/'>Components</a></li>
<li><a href='/cookbook'>Examples</a></li>
<li>
<a>Developers <i class="icon icon-caret-down"></i></a>
<ul>
<li><a href="/developers/">Setup Development</a></li>
<li><a href="/developers/architecture/">Architecture</a></li>
<li><a href="/developers/frontend/">Frontend Development</a></li>
<li><a href="/developers/creating_components/">
Creating Components
</a></li>
<li><a href="/developers/add_new_platform/">
Adding Platform Support
</a></li>
<li><a href="/developers/api/">API and SSE</a></li>
<li><a href="/developers/credits/">Credits</a></li>
</ul>
</li>
<li><a href="/blog/">Blog</a></li>
<li><a href="/help/">Need help?</a></li>
</ul>
</nav>
</div>
</div>
</div>
</header>
<div class="grid-wrapper">
<div class="grid grid-center">
<div class="grid__item two-thirds lap-one-whole palm-one-whole">
<article class="page">
<header>
<h1 class="title indent">
Flash Lights When Intruder Detected
</h1>
</header>
<hr class="divider">
<p>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 <a href="/components/notify/">the notifiy component</a>. It will also flash a specific light when a known person comes home.</p>
<p>This component depends on the components <a href="/components/device_tracker/">device_tracker</a> and <a href="/components/light/">light</a> being setup.</p>
<p>To set it up, add the following lines to your <code>configuration.yaml</code> file:</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="comment"># Example configuration.yaml entry</span>
<span class="key">simple_alarm</span>:
<span class="key">known_light</span>: <span class="string"><span class="content">light.Bowl</span></span>
<span class="key">unknown_light</span>: <span class="string"><span class="content">group.living_room</span></span>
</pre></div>
</div>
</div>
<p>Configuration variables:</p>
<ul>
<li><strong>known_light</strong> (<em>Optional</em>): Which light/light group has to flash when a known device comes home.</li>
<li><strong>unknown_light</strong> (<em>Optional</em>): Which light/light group has to flash red when light turns on while no one home.</li>
</ul>
<p>Create the file <code>&lt;config dir&gt;/custom_components/simple_alarm.py</code> and copy paste the content below:</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Simple alarm component.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">import</span> <span class="include">logging</span>
<span class="keyword">import</span> <span class="include">homeassistant.loader</span> <span class="keyword">as</span> loader
<span class="keyword">from</span> <span class="include">homeassistant.components</span> <span class="keyword">import</span> <span class="include">device_tracker</span>, <span class="include">light</span>, <span class="include">notify</span>
<span class="keyword">from</span> <span class="include">homeassistant.helpers.event</span> <span class="keyword">import</span> <span class="include">track_state_change</span>
<span class="keyword">from</span> <span class="include">homeassistant.const</span> <span class="keyword">import</span> <span class="include">STATE_ON</span>, <span class="include">STATE_OFF</span>, <span class="include">STATE_HOME</span>, <span class="include">STATE_NOT_HOME</span>
DOMAIN = <span class="string"><span class="delimiter">&quot;</span><span class="content">simple_alarm</span><span class="delimiter">&quot;</span></span>
DEPENDENCIES = [<span class="string"><span class="delimiter">'</span><span class="content">group</span><span class="delimiter">'</span></span>, <span class="string"><span class="delimiter">'</span><span class="content">device_tracker</span><span class="delimiter">'</span></span>, <span class="string"><span class="delimiter">'</span><span class="content">light</span><span class="delimiter">'</span></span>]
<span class="comment"># Attribute to tell which light has to flash whem a known person comes home</span>
<span class="comment"># If omitted will flash all.</span>
CONF_KNOWN_LIGHT = <span class="string"><span class="delimiter">&quot;</span><span class="content">known_light</span><span class="delimiter">&quot;</span></span>
<span class="comment"># Attribute to tell which light has to flash whem an unknown person comes home</span>
<span class="comment"># If omitted will flash all.</span>
CONF_UNKNOWN_LIGHT = <span class="string"><span class="delimiter">&quot;</span><span class="content">unknown_light</span><span class="delimiter">&quot;</span></span>
<span class="comment"># Services to test the alarms</span>
SERVICE_TEST_KNOWN_ALARM = <span class="string"><span class="delimiter">&quot;</span><span class="content">test_known</span><span class="delimiter">&quot;</span></span>
SERVICE_TEST_UNKNOWN_ALARM = <span class="string"><span class="delimiter">&quot;</span><span class="content">test_unknown</span><span class="delimiter">&quot;</span></span>
<span class="keyword">def</span> <span class="function">setup</span>(hass, config):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Sets up the simple alarms. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
logger = logging.getLogger(__name__)
light_ids = []
<span class="keyword">for</span> conf_key <span class="keyword">in</span> (CONF_KNOWN_LIGHT, CONF_UNKNOWN_LIGHT):
light_id = config[DOMAIN].get(conf_key, light.ENTITY_ID_ALL_LIGHTS)
<span class="keyword">if</span> hass.states.get(light_id) <span class="keyword">is</span> <span class="predefined-constant">None</span>:
logger.error(
<span class="string"><span class="delimiter">'</span><span class="content">Light id %s could not be found in state machine</span><span class="delimiter">'</span></span>, light_id)
<span class="keyword">return</span> <span class="predefined-constant">False</span>
light_ids.append(light_id)
<span class="comment"># pylint: disable=unbalanced-tuple-unpacking</span>
known_light_id, unknown_light_id = light_ids
<span class="keyword">if</span> hass.states.get(device_tracker.ENTITY_ID_ALL_DEVICES) <span class="keyword">is</span> <span class="predefined-constant">None</span>:
logger.error(<span class="string"><span class="delimiter">'</span><span class="content">No devices are being tracked, cannot setup alarm</span><span class="delimiter">'</span></span>)
<span class="keyword">return</span> <span class="predefined-constant">False</span>
<span class="keyword">def</span> <span class="function">known_alarm</span>():
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Fire an alarm if a known person arrives home. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
light.turn_on(hass, known_light_id, flash=light.FLASH_SHORT)
<span class="keyword">def</span> <span class="function">unknown_alarm</span>():
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Fire an alarm if the light turns on while no one is home. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
light.turn_on(
hass, unknown_light_id,
flash=light.FLASH_LONG, rgb_color=[<span class="integer">255</span>, <span class="integer">0</span>, <span class="integer">0</span>])
<span class="comment"># Send a message to the user</span>
notify.send_message(
hass, <span class="string"><span class="delimiter">&quot;</span><span class="content">The lights just got turned on while no one was home.</span><span class="delimiter">&quot;</span></span>)
<span class="comment"># Setup services to test the effect</span>
hass.services.register(
DOMAIN, SERVICE_TEST_KNOWN_ALARM, <span class="keyword">lambda</span> call: known_alarm())
hass.services.register(
DOMAIN, SERVICE_TEST_UNKNOWN_ALARM, <span class="keyword">lambda</span> call: unknown_alarm())
<span class="keyword">def</span> <span class="function">unknown_alarm_if_lights_on</span>(entity_id, old_state, new_state):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Called when a light has been turned on. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">if</span> <span class="keyword">not</span> 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)
<span class="keyword">def</span> <span class="function">ring_known_alarm</span>(entity_id, old_state, new_state):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Called when a known person comes home. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">if</span> light.is_on(hass, known_light_id):
known_alarm()
<span class="comment"># Track home coming of each device</span>
track_state_change(
hass, hass.states.entity_ids(device_tracker.DOMAIN),
ring_known_alarm, STATE_NOT_HOME, STATE_HOME)
<span class="keyword">return</span> <span class="predefined-constant">True</span>
</pre></div>
</div>
</div>
</article>
</div>
<aside id="sidebar" class="grid__item one-third lap-one-whole palm-one-whole">
<div class="grid">
<section class="aside-module grid__item one-whole lap-one-half">
<div class='edit-github'><a href='https://github.com/balloob/home-assistant.io/tree/master/source/_cookbook/python_component_simple_alarm.markdown'>Edit this page on GitHub</a></div>
<div class='section'>
<a href='/cookbook'>Back to the cookbook</a>
</div>
<div class='section'>
<h1 class="title delta">Automation in Python Examples</h1>
<ul class='divided'>
<li>
Flash lights when intruder detected
</li>
</ul>
</div>
</section>
</div>
</aside>
</div>
</div>
<footer>
<div class="grid-wrapper">
<div class="grid">
<div class="grid__item">
<div class="copyright">
<a rel="me" href='https://twitter.com/home_assistant'><i class="icon-twitter"></i></a>
<a rel="me" href='https://github.com/balloob/home-assistant'><i class="icon-github"></i></a>
<div class="credit">
Website powered by <a href='http://jekyllrb.com/'>Jekyll</a> and the <a href='https://github.com/coogie/oscailte'>Oscalite theme</a>.<br />
Hosted by <a href='https://pages.github.com/'>GitHub</a> and served by <a href='https://cloudflare.com'>CloudFlare</a>.
</div>
</div>
</div>
</div>
</div>
</footer>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]-->
<script>
var _gaq=[['_setAccount','UA-57927901-1'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
</script>
</body>
</html>