Site updated at 2016-03-25 06:17:20 UTC
This commit is contained in:
parent
f902089986
commit
a773a1a71c
14 changed files with 284 additions and 187 deletions
|
@ -113,42 +113,139 @@
|
|||
<hr class="divider">
|
||||
|
||||
|
||||
<p>This is a simple hello world example to show the basics for setting a state. To use this example, create the file <code><config dir>/custom_components/hello_state.py</code> and copy the below example code.</p>
|
||||
<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 beginn 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><config dir>/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="comment"># The domain of your component. Should be equal to the name of your component.</span>
|
||||
<div class="code"><pre><span class="docstring"><span class="delimiter">"""</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">"""</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 = []
|
||||
|
||||
CONF_NAME = <span class="string"><span class="delimiter">'</span><span class="content">name</span><span class="delimiter">'</span></span>
|
||||
DEFAULT_NAME = <span class="string"><span class="delimiter">'</span><span class="content">World</span><span class="delimiter">'</span></span>
|
||||
<span class="keyword">def</span> <span class="function">setup</span>(hass, config=<span class="predefined-constant">None</span>):
|
||||
<span class="docstring"><span class="delimiter">"""</span><span class="content">Setup the Hello State component. </span><span class="delimiter">"""</span></span>
|
||||
_LOGGER.info(<span class="string"><span class="delimiter">"</span><span class="content">The 'hello state' component is ready!</span><span class="delimiter">"</span></span>)
|
||||
|
||||
|
||||
<span class="keyword">def</span> <span class="function">setup</span>(hass, config):
|
||||
<span class="docstring"><span class="delimiter">"""</span><span class="content">Setup is called when Home Assistant is loading our component.</span><span class="delimiter">"""</span></span>
|
||||
|
||||
<span class="comment"># Get the name from the configuration. Use DEFAULT_NAME if no name provided.</span>
|
||||
name = config[DOMAIN].get(CONF_NAME, DEFAULT_NAME)
|
||||
|
||||
<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</span><span class="delimiter">'</span></span>, name)
|
||||
|
||||
<span class="comment"># Return boolean to indicate that initialization was successfully.</span>
|
||||
<span class="keyword">return</span> <span class="predefined-constant">True</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p>Load the component by adding the following to your <code>configuration.yaml</code>:</p>
|
||||
<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="comment"># configuration.yaml entry</span>
|
||||
<span class="key">hello_state</span>:
|
||||
<span class="comment"># optional</span>
|
||||
<span class="key">name</span>: <span class="string"><span class="content">Paulus</span></span>
|
||||
<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">"""</span><span class="content">Setup the Hello State component. </span><span class="delimiter">"""</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">information</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. It’s 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>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue