Site updated at 2016-02-22 15:17:46 UTC

This commit is contained in:
Travis CI 2016-02-22 15:17:46 +00:00
parent c3ae07fc4d
commit f390b84e87
15 changed files with 186 additions and 186 deletions

View file

@ -113,20 +113,20 @@
<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>&lt;config dir&gt;/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>&lt;config dir&gt;/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>
<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">&quot;</span><span class="content">hello_service</span><span class="delimiter">&quot;</span></span>
<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>
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="keyword">def</span> <span class="function">setup</span>(hass, config):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Setup is called when Home Assistant is loading our component. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Setup is called when Home Assistant is loading our component.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">def</span> <span class="function">handle_hello</span>(call):
name = call.data.get(ATTR_NAME, DEFAULT_NAME)
@ -135,7 +135,7 @@ DEFAULT_NAME = <span class="string"><span class="delimiter">'</span><span class=
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="comment"># return boolean to indicate that initialization was successful</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>

View file

@ -116,15 +116,15 @@
<p>This is a simple hello world example to show the basics for setting a state. To use this example, 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="comment"># The domain of your component. Should be equal to the name of your component</span>
DOMAIN = <span class="string"><span class="delimiter">&quot;</span><span class="content">hello_state</span><span class="delimiter">&quot;</span></span>
<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_state</span><span class="delimiter">'</span></span>
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="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Setup is called when Home Assistant is loading our component. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Setup is called when Home Assistant is loading our component.</span><span class="delimiter">&quot;&quot;&quot;</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)
@ -132,7 +132,7 @@ DEFAULT_NAME = <span class="string"><span class="delimiter">'</span><span class=
<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 successful</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>

View file

@ -124,10 +124,10 @@ This example requires you to have the <a href="/components/mqtt/">MQTT component
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="keyword">import</span> <span class="include">homeassistant.loader</span> <span class="keyword">as</span> loader
<span class="comment"># The domain of your component. Should be equal to the name of your component</span>
<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">&quot;</span><span class="content">hello_mqtt</span><span class="delimiter">&quot;</span></span>
<span class="comment"># List of component names (string) your component depends upon</span>
<span class="comment"># List of component names (string) your component depends upon.</span>
DEPENDENCIES = [<span class="string"><span class="delimiter">'</span><span class="content">mqtt</span><span class="delimiter">'</span></span>]
@ -136,31 +136,31 @@ DEFAULT_TOPIC = <span class="string"><span class="delimiter">'</span><span class
<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 our hello_mqtt component. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Setup the Hello MQTT component.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
mqtt = loader.get_component(<span class="string"><span class="delimiter">'</span><span class="content">mqtt</span><span class="delimiter">'</span></span>)
topic = config[DOMAIN].get(<span class="string"><span class="delimiter">'</span><span class="content">topic</span><span class="delimiter">'</span></span>, DEFAULT_TOPIC)
entity_id = <span class="string"><span class="delimiter">'</span><span class="content">hello_mqtt.last_message</span><span class="delimiter">'</span></span>
<span class="comment"># Listener to be called when we receive a message</span>
<span class="comment"># Listener to be called when we receive a message.</span>
<span class="keyword">def</span> <span class="function">message_received</span>(topic, payload, qos):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> A new MQTT message has been received. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">A new MQTT message has been received.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
hass.states.set(entity_id, payload)
<span class="comment"># Subscribe our listener to a topic</span>
<span class="comment"># Subscribe our listener to a topic.</span>
mqtt.subscribe(hass, topic, message_received)
<span class="comment"># Set the intial state</span>
hass.states.set(entity_id, <span class="string"><span class="delimiter">'</span><span class="content">No messages</span><span class="delimiter">'</span></span>)
<span class="comment"># Service to publish a message on MQTT</span>
<span class="comment"># Service to publish a message on MQTT.</span>
<span class="keyword">def</span> <span class="function">set_state_service</span>(call):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content"> Service to send a message. </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Service to send a message.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
mqtt.publish(hass, topic, call.data.get(<span class="string"><span class="delimiter">'</span><span class="content">new_state</span><span class="delimiter">'</span></span>))
<span class="comment"># Register our service with Home Assistant</span>
<span class="comment"># Register our service with Home Assistant.</span>
hass.services.register(DOMAIN, <span class="string"><span class="delimiter">'</span><span class="content">set_state</span><span class="delimiter">'</span></span>, set_state_service)
<span class="comment"># return boolean to indicate that initialization was successful</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>