Site updated at 2016-08-22 08:21:42 UTC
This commit is contained in:
parent
f9d65cbe57
commit
4acb07bf8e
559 changed files with 18878 additions and 21688 deletions
|
@ -89,53 +89,47 @@
|
|||
<hr class="divider">
|
||||
|
||||
|
||||
<p>This is a simple “hello world” example to show the basics of registering a service. To use this example, create the file <code><config dir>/custom_components/hello_service.py</code> and copy the below example code.</p>
|
||||
<p>This is a simple “hello world” example to show the basics of registering a service. To use this example, create the file <code class="highlighter-rouge"><config dir>/custom_components/hello_service.py</code> and copy the below example code.</p>
|
||||
|
||||
<p>Services can be called from automation and from the service “Developer tools” in the frontend.</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>
|
||||
DOMAIN = <span class="string"><span class="delimiter">'</span><span class="content">hello_service</span><span class="delimiter">'</span></span>
|
||||
<div class="language-python highlighter-rouge"><pre class="highlight"><code><span class="c"># The domain of your component. Should be equal to the name of your component.</span>
|
||||
<span class="n">DOMAIN</span> <span class="o">=</span> <span class="s">'hello_service'</span>
|
||||
|
||||
ATTR_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="n">ATTR_NAME</span> <span class="o">=</span> <span class="s">'name'</span>
|
||||
<span class="n">DEFAULT_NAME</span> <span class="o">=</span> <span class="s">'World'</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="k">def</span> <span class="nf">setup</span><span class="p">(</span><span class="n">hass</span><span class="p">,</span> <span class="n">config</span><span class="p">):</span>
|
||||
<span class="s">"""Setup is called when Home Assistant is loading our component."""</span>
|
||||
|
||||
<span class="keyword">def</span> <span class="function">handle_hello</span>(call):
|
||||
name = call.data.get(ATTR_NAME, DEFAULT_NAME)
|
||||
<span class="k">def</span> <span class="nf">handle_hello</span><span class="p">(</span><span class="n">call</span><span class="p">):</span>
|
||||
<span class="n">name</span> <span class="o">=</span> <span class="n">call</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">ATTR_NAME</span><span class="p">,</span> <span class="n">DEFAULT_NAME</span><span class="p">)</span>
|
||||
|
||||
hass.states.set(<span class="string"><span class="delimiter">'</span><span class="content">hello_service.hello</span><span class="delimiter">'</span></span>, name)
|
||||
<span class="n">hass</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="nb">set</span><span class="p">(</span><span class="s">'hello_service.hello'</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
|
||||
|
||||
hass.services.register(DOMAIN, <span class="string"><span class="delimiter">'</span><span class="content">hello</span><span class="delimiter">'</span></span>, handle_hello)
|
||||
<span class="n">hass</span><span class="o">.</span><span class="n">services</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">DOMAIN</span><span class="p">,</span> <span class="s">'hello'</span><span class="p">,</span> <span class="n">handle_hello</span><span class="p">)</span>
|
||||
|
||||
<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>
|
||||
<span class="c"># Return boolean to indicate that initialization was successfully.</span>
|
||||
<span class="k">return</span> <span class="bp">True</span>
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<p>Load the component by adding the following to your <code>configuration.yaml</code>. When your component is loaded, a new service should be available to call.</p>
|
||||
<p>Load the component by adding the following to your <code class="highlighter-rouge">configuration.yaml</code>. When your component is loaded, a new service should be available to call.</p>
|
||||
|
||||
<div class="highlighter-coderay"><div class="CodeRay">
|
||||
<div class="code"><pre><span class="comment"># configuration.yaml entry</span>
|
||||
<span class="key">hello_service</span>:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="c1"># configuration.yaml entry</span>
|
||||
<span class="s">hello_service</span><span class="pi">:</span>
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<p>Open the frontend and in the sidebar, click the first icon in the developer tool section. This will open the Call Service developer tool. On the right, find your service and click on it. This will automatically fill in the correct values.</p>
|
||||
|
||||
<p>Pressing “Call Service” will now call your service without any parameters. This will cause your service to create a state with the default name ‘World’. If you want to specify the name, you have to specify parameters. Add the following JSON as Service Data and press “Call Service again”.</p>
|
||||
|
||||
<div class="highlighter-coderay"><div class="CodeRay">
|
||||
<div class="code"><pre>{
|
||||
<span class="key"><span class="delimiter">"</span><span class="content">name</span><span class="delimiter">"</span></span>: <span class="string"><span class="delimiter">"</span><span class="content">Planet</span><span class="delimiter">"</span></span>
|
||||
}
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="language-json highlighter-rouge"><pre class="highlight"><code><span class="p">{</span><span class="w">
|
||||
</span><span class="nt">"name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Planet"</span><span class="w">
|
||||
</span><span class="p">}</span><span class="w">
|
||||
</span></code></pre>
|
||||
</div>
|
||||
|
||||
<p>The service will now overwrite the previous state with “Planet”.</p>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue