home-assistant.github.io/developers/add_new_platform/index.html
2016-04-17 00:12:10 +00:00

270 lines
No EOL
14 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>Adding support for a new platform - Home Assistant</title>
<meta name="author" content="Paulus Schoutsen">
<meta name="description" content="Hints and tips for when you're adding a new platform to Home Assistant.">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/developers/add_new_platform/">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="Adding support for a new platform">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/developers/add_new_platform/">
<meta property="og:type" content="website">
<meta property="og:description" content="Hints and tips for when you're adding a new platform to Home Assistant.">
<meta property="og:image" content="https://home-assistant.io/images/home-assistant-logo-2164x2164.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@home_assistant">
<meta name="twitter:title" content="Adding support for a new platform">
<meta name="twitter:description" content="Hints and tips for when you're adding a new platform to Home Assistant.">
<meta name="twitter:image" content="https://home-assistant.io/images/home-assistant-logo-2164x2164.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">
Adding Support for a New Platform
</h1>
</header>
<hr class="divider">
<p>Components that interact with devices are structured in core- and platform logic. This allows the same logic to be used for different platforms.</p>
<p>For example, the built-in <code>switch</code> component consists of various platform in <a href="https://github.com/home-assistant/home-assistant/tree/master/homeassistant/components/switch"><code>homeassistant/components/switch/</code></a>. The file <code>__init__.py</code> contains the core logic of all platform and the <code>vendor_name.py</code> files only the relevant platform code.</p>
<p>If you are planning to add support for a new type of device to an existing component, you can get away with only writing platform logic. Have a look at how the component works with other platforms and create a similar file for the platform that you would like to add.</p>
<h3><a class="title-link" name="interfacing-with-devices" href="#interfacing-with-devices"></a> Interfacing with devices</h3>
<p>One of the rules for Home Assistant is that platform logic should never interface directly with devices but use a third-party Python 3 library to do so. This way Home Assistant is able to share code with the Python community and we can keep the project maintainable.</p>
<p>Platforms can specify dependencies and requirements the same way as a component does.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>REQUIREMENTS = [<span class="string"><span class="delimiter">'</span><span class="content">some-package==2.0.0</span><span class="delimiter">'</span></span>, <span class="string"><span class="delimiter">'</span><span class="content">some-other-package==2.5.0</span><span class="delimiter">'</span></span>]
DEPENDENCIES = [<span class="string"><span class="delimiter">'</span><span class="content">mqtt</span><span class="delimiter">'</span></span>]
</pre></div>
</div>
</div>
<h3><a class="title-link" name="platform-example" href="#platform-example"></a> Platform example</h3>
<p>Entities are Home Assistants representation of lights, switches, sensors, etc. and are derived from the <a href="https://github.com/home-assistant/home-assistant/blob/master/homeassistant/helpers/entity.py">Entity Abstract Class</a>. This abstract class contains logic for integrating most standard features into your entities, such as visibility, entity IDs, updates, and many more.</p>
<p>This example is for adding support for the imaginary Awesome Lights.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="keyword">import</span> <span class="include">logging</span>
<span class="comment"># Import the device class from the component that you want to support</span>
<span class="keyword">from</span> <span class="include">homeassistant.components.light</span> <span class="keyword">import</span> <span class="include">Light</span>
<span class="keyword">from</span> <span class="include">homeassistant.const</span> <span class="keyword">import</span> <span class="include">CONF_HOST</span>, <span class="include">CONF_USERNAME</span>, <span class="include">CONF_PASSWORD</span>
<span class="comment"># Home Assistant depends on 3rd party packages for API specific code.</span>
REQUIREMENTS = [<span class="string"><span class="delimiter">'</span><span class="content">awesome_lights==1.2.3</span><span class="delimiter">'</span></span>]
_LOGGER = logging.getLogger(__name__)
setup_platform(hass, config, add_devices, discovery_info=<span class="predefined-constant">None</span>):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Initialize Awesome Light platform.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">import</span> <span class="include">awesomelights</span>
<span class="comment"># Validate passed in config</span>
host = config.get(CONF_HOST)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
<span class="keyword">if</span> host <span class="keyword">is</span> <span class="predefined-constant">None</span> <span class="keyword">or</span> username <span class="keyword">is</span> <span class="predefined-constant">None</span> <span class="keyword">or</span> password <span class="keyword">is</span> <span class="predefined-constant">None</span>:
_LOGGER.error(<span class="string"><span class="delimiter">'</span><span class="content">Invalid config. Expected %s, %s and %s</span><span class="delimiter">'</span></span>,
CONF_HOST, CONF_USERNAME, CONF_PASSWORD)
<span class="keyword">return</span> <span class="predefined-constant">False</span>
<span class="comment"># Setup connection with devices/cloud</span>
hub = awesomelights.Hub(host, username, password)
<span class="comment"># Verify that passed in config works</span>
<span class="keyword">if</span> <span class="keyword">not</span> hub.is_valid_login():
_LOGGER.error(<span class="string"><span class="delimiter">'</span><span class="content">Could not connect to AwesomeLight hub</span><span class="delimiter">'</span></span>)
<span class="keyword">return</span> <span class="predefined-constant">False</span>
<span class="comment"># Add devices</span>
add_devices(AwesomeLight(light) <span class="keyword">for</span> light <span class="keyword">in</span> hub.lights())
<span class="keyword">class</span> <span class="class">AwesomeLight</span>(Light):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Represents an AwesomeLight in Home Assistant.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">def</span> <span class="function">__init__</span>(<span class="predefined-constant">self</span>, light):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Initialize an AwesomeLight.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="predefined-constant">self</span>._light = light
<span class="keyword">def</span> <span class="function">update</span>(<span class="predefined-constant">self</span>):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Fetch new state data for this light.</span><span class="content">
</span><span class="content">
</span><span class="content"> This is the only method that should fetch new data for Home Assitant.</span><span class="content">
</span><span class="content"> </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="predefined-constant">self</span>._light.update()
<span class="keyword">def</span> <span class="function">brightness</span>(<span class="predefined-constant">self</span>):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">Brightness of the light.</span><span class="content">
</span><span class="content">
</span><span class="content"> This method is optional. Removing it indicates to Home Assistant</span><span class="content">
</span><span class="content"> that brightness is not supported for this light.</span><span class="content">
</span><span class="content"> </span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">return</span> <span class="predefined-constant">self</span>._light.brightness
<span class="keyword">def</span> <span class="function">is_on</span>(<span class="predefined-constant">self</span>):
<span class="docstring"><span class="delimiter">&quot;&quot;&quot;</span><span class="content">If light is on.</span><span class="delimiter">&quot;&quot;&quot;</span></span>
<span class="keyword">return</span> <span class="predefined-constant">self</span>._light.is_on()
</pre></div>
</div>
</div>
</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/developers/add_new_platform.markdown'>Edit this page on GitHub</a></div>
<div class='section'>
<h1 class="title delta">Development Guide</h1>
<ul class='divided sidebar-menu'>
<li>
<a href='/developers/'>Introduction </a>
<ul>
<li><a href='/developers/architecture/'>Architecture </a></li>
<li><a href='/developers/architecture_components/'>Components </a></li>
<li><a href='/developers/development_environment/'>Setup Dev Environment </a></li>
</ul>
</li>
<li>
Frontend Development
<ul>
<li><a href='/developers/frontend/'>Setup Frontend Environment </a></li>
<li><a href='/developers/frontend_add_card/'>Add State Card </a></li>
<li><a href='/developers/frontend_add_more_info/'>Add More Info Dialog </a></li>
</ul>
</li>
<li>
Extending Home Assistant
<ul>
<li><a href='/developers/creating_components/'>Creating Components </a></li>
<li><a class='active' 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>
<li><a href='/developers/rest_api/'>RESTful API </a></li>
<li><a href='/developers/python_api/'>Python API </a></li>
<li><a href='/developers/server_sent_events/'>Server-sent events </a></li>
</ul>
</li>
<li><a href='/developers/multiple_instances/'>Multiple Instances </a></li>
<li><a href='/developers/website/'>Home-Assitant.io </a></li>
<li><a href='/developers/credits/'>Credits </a></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">
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>