Site updated at 2016-07-24 17:39:50 UTC

This commit is contained in:
Travis CI 2016-07-24 17:39:51 +00:00
parent 2d9a218a1b
commit 29118ce15d
1007 changed files with 168343 additions and 140 deletions

View file

@ -0,0 +1,289 @@
<!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>Basic State Setting Example - Home Assistant</title>
<meta name="author" content="Home Assistant">
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/cookbook/python_component_basic_state/">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="Basic State Setting Example">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/cookbook/python_component_basic_state/">
<meta property="og:type" content="article">
<meta property="og:description" content="">
<meta property="og:image" content="https://home-assistant.io/images/default-social.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@home_assistant">
<meta name="twitter:title" content="Basic State Setting Example">
<meta name="twitter:description" content="">
<meta name="twitter:image" content="https://home-assistant.io/images/default-social.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 href='/getting-started/'>Getting started</a></li>
<li><a href='/components/'>Components</a></li>
<li><a href='/cookbook/'>Examples</a></li>
<li><a href="/developers/">Developers</a></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">
Basic State Setting Example
</h1>
</header>
<hr class="divider">
<p>This is a simple tutorial/example on how to write a component for <a href="https://home-assistant.io/">Home Assistant</a>. We will work on a component called “hello_state” to begin with. The purpose of this component is to display a given text in the frontend.</p>
<p>The setup of a development environment is described in the <a href="/developers/#starting-development">Developers section</a> of the documentation.</p>
<h2><a class="title-link" name="component" href="#component"></a> Component</h2>
<p>To get started, create the file <code>&lt;config dir&gt;/custom_components/hello_state.py</code> and copy the below example code.</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">
</span><span class="content">Support for showing text in the frontend.</span><span class="content">
</span><span class="content">
</span><span class="content">For more details about this component, please refer to the documentation at</span><span class="content">
</span><span class="content">https://home-assistant.io/components/hello_state/</span><span class="content">
</span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">import</span> <span class="include">logging</span>
_LOGGER = logging.getLogger(__name__)
DOMAIN = <span class="string"><span class="delimiter">'</span><span class="content">hello_state</span><span class="delimiter">'</span></span>
DEPENDENCIES = []
<span class="keyword">def</span> <span class="function">setup</span>(hass, config=<span class="predefined-constant">None</span>):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Setup the Hello State component. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
_LOGGER.info(<span class="string"><span class="delimiter">&quot;</span><span class="content">The 'hello state' component is ready!</span><span class="delimiter">&quot;</span></span>)
<span class="keyword">return</span> <span class="predefined-constant">True</span>
</pre></div>
</div>
</div>
<ol>
<li>In the file header we decided to add some details: A short description and the link to the documentation.</li>
<li>We want to do some logging. This means that we import the Python logging module and create an alias.</li>
<li>The component name is equal to the domain name.</li>
<li>At the moment this component has no dependencies. For detail check <a href="/developers/creating_components/#dependencies">dependencies</a> section.</li>
<li>
<p>The <code>setup</code> function will take care of the initialization of our component.<br />
The component will only write a log message. Keep in mind for later that you have several options for the severity:</p>
<ul>
<li><code>_LOGGER.info(msg)</code></li>
<li><code>_LOGGER.warning(msg)</code></li>
<li><code>_LOGGER.error(msg)</code></li>
<li><code>_LOGGER.critical(msg)</code></li>
<li><code>_LOGGER.exception(msg)</code></li>
</ul>
</li>
<li>We return <code>True</code> if everything is ok.</li>
</ol>
<p>Add the component to your <code>configuration.yaml</code> file.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">hello_state</span>:
</pre></div>
</div>
</div>
<p>After a start or a restart of Home Assistant the component will create an entry in the log.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>16-03-12 14:16:42 INFO (MainThread) [custom_components.hello_state] The 'hello state' component is ready!
</pre></div>
</div>
</div>
<p>The next step is the introduction of configuration options. Most configuration details are coming out of the <code>configuration.yaml</code> file. To do that we need to update the <code>def setup()</code> method to accept configuration information and access the configuration variable in the <code>setup</code> method.</p>
<p>More details about this topic can be found in the <a href="/developers/creating_components/#config-user-given-configuration">User given configuration</a> section.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="keyword">import</span> <span class="include">logging</span>
_LOGGER = logging.getLogger(__name__)
DOMAIN = <span class="string"><span class="delimiter">'</span><span class="content">hello_state</span><span class="delimiter">'</span></span>
DEPENDENCIES = []
CONF_TEXT = <span class="string"><span class="delimiter">'</span><span class="content">text</span><span class="delimiter">'</span></span>
DEFAULT_TEXT = <span class="string"><span class="delimiter">'</span><span class="content">No text!</span><span class="delimiter">'</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">Setup the Hello State component. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="comment"># Get the text from the configuration. Use DEFAULT_TEXT if no name is provided.</span>
text = config[DOMAIN].get(CONF_TEXT, DEFAULT_TEXT)
<span class="comment"># States are in the format DOMAIN.OBJECT_ID</span>
hass.states.set(<span class="string"><span class="delimiter">'</span><span class="content">hello_state.Hello_State</span><span class="delimiter">'</span></span>, text)
<span class="keyword">return</span> <span class="predefined-constant">True</span>
</pre></div>
</div>
</div>
<p>To add the latest feature of our component, update the entry in your <code>configuration.yaml</code> file.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">hello_state</span>:
<span class="key">text</span>: <span class="string"><span class="content">'Hello, World!'</span></span>
</pre></div>
</div>
</div>
<p>Thanks to <code>DEFAULT_TEXT</code> variable the component will launch even if no <code>text:</code> field is used in the <code>configuration.yaml</code> file. Quite often there are variables which are required. Its important to check if all mandatory configuration variables are provided. If not, the setup should fail. We will use the <code>validate_config</code> function as a helper to achive this. The next listing shows the essential parts.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="keyword">from</span> <span class="include">homeassistant.helpers</span> <span class="keyword">import</span> <span class="include">validate_config</span>
[...]
<span class="keyword">if</span> <span class="keyword">not</span> validate_config(config, {DOMAIN: [CONF_TEXT]}, _LOGGER):
<span class="keyword">return</span> <span class="predefined-constant">False</span>
</pre></div>
</div>
</div>
<p>If <code>text:</code> is missing, there will be a warning in the log file.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>16-03-12 14:37:37 ERROR (MainThread) [custom_components.hello_state] Missing required configuration items in hello_state: text
16-03-12 14:37:37 ERROR (MainThread) [homeassistant.bootstrap] component hello_state failed to initialize
</pre></div>
</div>
</div>
<p>After a start or a restart of Home Assistant the component will be visible in the frontend if the <code>configuration.yaml</code> file is up-to-date.</p>
<p class="img">
<img src="/images/screenshots/create-component01.png" />
</p>
<p>To get your component included in the Home Assistant releases, follow the steps described in the <a href="https://home-assistant.io/developers/#submitting-improvements">Submitting improvements</a> section. Basically you only need to move your component in the <code>homeassistant/component/</code> directory of your fork and create a Pull Request.</p>
</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/home-assistant/home-assistant.io/tree/master/source/_cookbook/python_component_basic_state.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">Custom Python Component Examples</h1>
<ul class='divided'>
<li>
<a href='/cookbook/python_component_mqtt_basic/'>Basic MQTT Example</a>
</li>
<li>
<a href='/cookbook/python_component_basic_service/'>Basic Service Example</a>
</li>
<li>
Basic State Setting Example
</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/home-assistant/home-assistant'><i class="icon-github"></i></a>
<div class="credit">
Contact us at <a href='mailto:hello@home-assistant.io'>hello@home-assistant.io</a>.<br>
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>
<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>