Site updated at 2016-04-17 06:02:22 UTC
This commit is contained in:
parent
6c1cceb031
commit
a9fe695036
36 changed files with 1663 additions and 317 deletions
|
@ -89,103 +89,24 @@
|
|||
<hr class="divider">
|
||||
|
||||
|
||||
<p>Home Assistant offers <a href="/components/">built-in components</a> but it is easy to build your own. If you are the kind of person that likes to learn from code rather then guide then head over to the <a href="https://github.com/home-assistant/home-assistant/tree/master/config/custom_components"><code>config/custom_components</code></a> folder in the repository for two example components. Or visit the <a href="/cookbook/#custom-python-component-examples">Custom Python Component Examples</a>.</p>
|
||||
<p>Alright, you’re ready to make your first component. AWESOME. Don’t worry, we’ve tried hard to keep it as easy as possible.</p>
|
||||
|
||||
<p>The first is <a href="https://github.com/home-assistant/home-assistant/blob/master/config/custom_components/hello_world.py">hello_world.py</a> (this is similar to the <a href="https://home-assistant.io/cookbook/python_component_basic_state/">Basic State Setting Example</a>), which is the classic “Hello World” example for Home Assistant. The second one is <a href="https://github.com/home-assistant/home-assistant/blob/master/config/custom_components/example.py">example.py</a> which showcases various ways you can tap into Home Assistant to be notified when certain events occur.</p>
|
||||
<h3><a class="title-link" name="example-component" href="#example-component"></a> Example component</h3>
|
||||
|
||||
<p>If you want to load these components in Home Assistant, add the following lines to your <code>configuration.yaml</code> file:</p>
|
||||
<p>Add <code>hello_state:</code> to your <code>configuration.yaml</code> file and create a file <code><config_dir>/custom_components/hello_state.py</code> with the below code to test it locally.</p>
|
||||
|
||||
<div class="highlighter-coderay"><div class="CodeRay">
|
||||
<div class="code"><pre><span class="key">hello_world</span>:
|
||||
<div class="code"><pre>DOMAIN = <span class="string"><span class="delimiter">'</span><span class="content">hello_state</span><span class="delimiter">'</span></span>
|
||||
|
||||
<span class="key">example</span>:
|
||||
<span class="key">target</span>: <span class="string"><span class="content">TARGET_ENTITY</span></span>
|
||||
<span class="keyword">def</span> <span class="function">setup</span>(hass, config):
|
||||
hass.states.set(<span class="string"><span class="delimiter">'</span><span class="content">hello.world</span><span class="delimiter">'</span></span>, <span class="string"><span class="delimiter">'</span><span class="content">Paulus</span><span class="delimiter">'</span></span>)
|
||||
|
||||
<span class="keyword">return</span> <span class="predefined-constant">True</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p><code>TARGET_ENTITY</code> should be one of your devices that can be turned on and off, ie a light or a switch. Example value could be <code>light.Ceiling</code> or <code>switch.AC</code> (if you have these devices with those names).</p>
|
||||
|
||||
<h2><a class="title-link" name="loading-components" href="#loading-components"></a> Loading components</h2>
|
||||
|
||||
<p>A component will be loaded on start if a section (ie. <code>light:</code>) for it exists in the config file. A component can also be loaded if another component is loaded that depends on it. When loading a component Home Assistant will check the following paths:</p>
|
||||
|
||||
<ul>
|
||||
<li><code><config directory>/custom_components/<component name></code></li>
|
||||
<li><code>homeassistant/components/<component name></code> (built-in components)</li>
|
||||
</ul>
|
||||
|
||||
<p>Once loaded, a component will only be setup if all dependencies can be loaded and are able to setup. Keep an eye on the logs to see if your component could be loaded and initialized.</p>
|
||||
|
||||
<p class="note warning">
|
||||
You can override a built-in component by having a component with the same name in your <code>config/custom_components</code> folder. This is not recommended and will probably break things!
|
||||
</p>
|
||||
|
||||
<p class="note">
|
||||
Home Assistant will use the directory that contains your config file as the directory that holds your customizations. By default this is the <code>config</code> folder in your current work directory. You can use a different folder by running Home Assistant with the –config argument: <code>python3 homeassistant --config /YOUR/CONFIG/PATH/</code>.
|
||||
</p>
|
||||
|
||||
<h2><a class="title-link" name="dependencies" href="#dependencies"></a> Dependencies</h2>
|
||||
|
||||
<p>Home Assistant allows components and platforms to specify their dependencies and requirements using the variables <code>DEPENDENCIES</code> and <code>REQUIREMENTS</code>. Both are lists that contain strings.</p>
|
||||
|
||||
<p>Dependencies are other Home Assistant components that should be setup before the platform is loaded. An example is the MQTT sensor component, which requires an active connection to an MQTT broker. If Home Assistant is unable to load and setup the MQTT component, it will not setup the MQTT sensor component.</p>
|
||||
|
||||
<p>Requirements are Python libraries that you would normally install using <code>pip</code>. Each entry in a requirement list is a pip compatible string. For example, the media player Cast platform depends on the Python package PyChromecast thus <code>REQUIREMENTS = ['pychromecast==0.6.12']</code>. If Home Assistant is unable to install the package or verify it is installed, the component will fail to load.</p>
|
||||
|
||||
<h2><a class="title-link" name="initializing-components" href="#initializing-components"></a> Initializing components</h2>
|
||||
|
||||
<p>After loading, the bootstrapper will call <code>setup(hass, config)</code> method on the component to initialize it.</p>
|
||||
|
||||
<h3><a class="title-link" name="hass-the-home-assistant-instance" href="#hass-the-home-assistant-instance"></a> <code>hass</code>: the Home Assistant instance</h3>
|
||||
|
||||
<p>The Home Assistant instance contains three objects to help you interact with the system.</p>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Object</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><code>hass.config</code></td>
|
||||
<td>This is the core configuration of Home Assistant exposing location, temperature preferences and config directory path. <a href="https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/core.py#L687">Details</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>hass.states</code></td>
|
||||
<td>This is the StateMachine. It allows you to set states and track when they are changed. <a href="https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/core.py#L434">See available methods</a>.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>hass.bus</code></td>
|
||||
<td>This is the EventBus. It allows you to trigger and listen for events.<br /><a href="https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/core.py#L229">See available methods</a>.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>hass.services</code></td>
|
||||
<td>This is the ServiceRegistry. It allows you to register services.<br /><a href="https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/core.py#L568">See available methods</a>.</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3><a class="title-link" name="config-user-given-configuration" href="#config-user-given-configuration"></a> <code>config</code>: User given configuration.</h3>
|
||||
|
||||
<p>The <code>config</code> parameter is a dictionary containing the user supplied configuration. The keys of the dictionary are the component names and the value is another dictionary with the component configuration.</p>
|
||||
|
||||
<p>If your configuration file contains the following lines:</p>
|
||||
|
||||
<div class="highlighter-coderay"><div class="CodeRay">
|
||||
<div class="code"><pre><span class="key">example</span>:
|
||||
<span class="key">host</span>: <span class="string"><span class="content">paulusschoutsen.nl</span></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p>Then in the setup method of your component you will be able to refer to <code>config['example']['host']</code> to get the value <code>paulusschoutsen.nl</code>.</p>
|
||||
|
||||
<h2><a class="title-link" name="responding-to-events" href="#responding-to-events"></a> Responding to events</h2>
|
||||
|
||||
<p>Home Assistant has different ways of responding to events that occur in Home Assistant. These have been organized in <a href="https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/helpers/event.py">helper methods</a>. Examples are <code>track_state_change</code>, <code>track_point_in_time</code>, <code>track_time_change</code>.</p>
|
||||
<p>For more examples, see the <a href="/cookbook/#custom-python-component-examples">Custom Python Component Examples</a> on our examples page.</p>
|
||||
|
||||
|
||||
</article>
|
||||
|
@ -211,6 +132,23 @@ Home Assistant will use the directory that contains your config file as the dire
|
|||
<li><a href='/developers/development_environment/'>Setup Dev Environment </a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
<a href='/developers/add_new_platform/'>Support a new device (as a platform) </a>
|
||||
<ul>
|
||||
<li><a href='/developers/platform_example_sensor/'>Example sensor platform </a></li>
|
||||
<li><a href='/developers/platform_example_light/'>Example light platform </a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
<a class='active' href='/developers/creating_components/'>Adding a new component </a>
|
||||
<ul>
|
||||
<li><a href='/developers/component_loading/'>Loading components </a></li>
|
||||
<li><a href='/developers/component_deps_and_reqs/'>Requirements & Dependencies </a></li>
|
||||
<li><a href='/developers/component_initialization/'>Initialization </a></li>
|
||||
<li><a href='/developers/component_events/'>Handling events </a></li>
|
||||
<li><a href='/developers/component_discovery/'>Component Discovery </a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
Frontend Development
|
||||
<ul>
|
||||
|
@ -219,14 +157,6 @@ Home Assistant will use the directory that contains your config file as the dire
|
|||
<li><a href='/developers/frontend_add_more_info/'>Add More Info Dialog </a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
Extending Home Assistant
|
||||
<ul>
|
||||
<li><a class='active' href='/developers/creating_components/'>Creating Components </a></li>
|
||||
<li><a href='/developers/add_new_platform/'>Adding Platform Support </a></li>
|
||||
<li><a href='/developers/platform_discovery/'>Platform Discovery </a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
API
|
||||
<ul>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue