{"id":3929,"date":"2018-11-21T05:42:20","date_gmt":"2018-11-21T05:42:20","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=3929"},"modified":"2018-12-07T02:54:44","modified_gmt":"2018-12-07T02:54:44","slug":"home-assistant-the-python-approach-to-home-automation-linux-com","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/21\/home-assistant-the-python-approach-to-home-automation-linux-com\/","title":{"rendered":"Home Assistant: The Python Approach to Home Automation | Linux.com"},"content":{"rendered":"<p>A number of home automation platforms support Python as an extension, but if you\u2019re a real Python fiend, you\u2019ll probably want <a href=\"https:\/\/home-assistant.io\/\">Home Assistant<\/a>, which places the programming language front and center. Paulus Schoutsen created Home Assistant in 2013 \u201cas a simple script to turn on the lights when the sun was setting,\u201d as he told attendees of his 2016 <a href=\"https:\/\/events.linuxfoundation.org\/events\/elc-openiot-europe-2018\/\">Embedded Linux Conference and Open IoT<\/a> conference presentation. (You can watch the complete video below.)<\/p>\n<p>Schoutsen, who works as a senior software engineer for AppFolio in San Diego, has attracted 20 active contributors to the project. Home Assistant is now fairly mature, with updates every two weeks and support for more than 240 different smart devices and services. The open source (MIT license) software runs on anything that can run Python 3, from desktop PCs to a Raspberry Pi, and counts thousands of users around the world.<\/p>\n<p>Like most automation systems, Home Assistant offers mobile and desktop browser clients to control smart home devices from afar. It differs from most commercial offerings, however, in that it has no hub appliance, which means there are no built-in radios. You can add the precisely those radios you want, however, using USB sticks. There\u2019s also no cloud component, but Schoutsen argues that any functionality you might sacrifice because of this is more than matched by better security, privacy, and resiliency.<\/p>\n<p>\u201cThere is no dependency on a cloud provider,\u201d said Schoutsen. \u201cEven when the Internet goes down, the home doesn\u2019t shut down, and your very private data stays in your home.\u201d<\/p>\n<p>Schoutsen did not offer much of a promo in his presentation, but quickly set to work explaining how the platform works. Since Home Assistant is not radically different from other IoT frameworks &#8212; one reason why it interfaces easily with platforms ranging from Nest to Arduino to Kodi &#8212; the presentation is a useful introduction to IoT concepts.<\/p>\n<p>To get a better sense of Home Assistant\u2019s strengths, I recently asked Schoutsen for his elevator pitch. He highlighted the free, open source nature of the software, as well as the privacy and security of a local solution. He also noted the ease of setup and discovery, and the strength of the underlying Python language.<\/p>\n<h3>Easy Extensions<\/h3>\n<p>\u201cPython makes it very easy to extend the system,\u201d Schoutsen told me. \u201cAs a dynamic language it allows a flexibility that Java developers can only dream off. It is very easy to test out and prototype new pieces on an existing installation without breaking things permanently. With the recent introduction of MicroPython, which runs on embedded systems as <a href=\"https:\/\/www.linux.com\/news\/dueling-arduinos-include-linux-recent-sbc-announcements\">Arduino and ESP8266<\/a>, we can offer a single language for all levels of IoT: from sensors to automation to integration with third-party services.\u201d<\/p>\n<p>In Schoutsen\u2019s ELC 2016 presentation, he described how Home Assistant is an event-driven program that incorporates a state machine that keeps track of \u201centities\u201d &#8212; all the selected devices and people you want to track. Each entity has an identifier, a state condition, and attributes. The latter describes more about the state, such as the color and intensity of the light on a Philips Hue smart bulb.<\/p>\n<p>To integrate a Philips Hue into the system, for example, you would need to use a light \u201ccomponent,\u201d which is aware of the bulb and how to read its state (off or on). Home Assistant offers components for every supported device or service, as well as easy access to component groups such as lights, thermostats, switches, and garage doors. Setup is eased with a network discovery component that scans the network and, if you have a supported device, sets it up automatically.<\/p>\n<p>The software is further equipped with a service registry, which provides services over the event bus. &#8220;We can register the turn-on command for a light, and have it send an email or SMS,\u201d said Schoutsen. \u201cA timer can send a time change event every second, and a component can ask to be notified at a particular time, or in intervals. Based on time change events, it will trigger the callback of the components.\u201d<\/p>\n<p>Each component writes its state to the state machine, emitting a state change event to the event bus. \u201cThe light component would register its turn on service inside the service registry so that anyone could fire an event to the event bus to turn on the light,\u201d said Schoutsen.<\/p>\n<p>You can easily integrate a light component with a motion detector component using an automation component. This would listen to the motion detector events, and fire a \u201cturn light on\u201d event to the event bus, which in turn would be forwarded to the service registry. The registry would then check to see that the light component can handle the event. \u201cAutomation components can listen for events, observe certain attribute states or triggers, and act on them,\u201d explained Schoutsen.<\/p>\n<p>Another component type handles presence detection. \u201cThe platform can check the router to see which phones are connected in order to see who is home,\u201d said Schoutsen. \u201cOther components are responsible for recording event and state history, or for entity organization &#8212; grouping multiple entities and summarizing their state.\u201d Components are available for integrating third party services, such as MQTT or IFTTT, and other components export data to external databases and analysis tools.<\/p>\n<p>Schoutsen went on to explain concepts such as a \u201cplatform\u201d layer that sits above the entity components. Each platform integrates an \u201cabstract base class,\u201d which \u201cacts as the glue between the real device and the one represented in Home Assistant,\u201d said Schoutsen. Later, he ran through a code example for a basic switch and explored the use of trigger zones for geofencing.<\/p>\n<p>As Schoutsen says, Home Assistant is \u201cgaining a lot of traction.\u201d Check out the complete video to see what happens when Python meets IoT.<\/p>\n<p><a href=\"https:\/\/www.linux.com\/blog\/2018\/11\/home-assistant-python-approach-home-automation\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A number of home automation platforms support Python as an extension, but if you\u2019re a real Python fiend, you\u2019ll probably want Home Assistant, which places the programming language front and center. Paulus Schoutsen created Home Assistant in 2013 \u201cas a simple script to turn on the lights when the sun was setting,\u201d as he told &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/21\/home-assistant-the-python-approach-to-home-automation-linux-com\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Home Assistant: The Python Approach to Home Automation | Linux.com&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3929","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=3929"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3929\/revisions"}],"predecessor-version":[{"id":4673,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3929\/revisions\/4673"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=3929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=3929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=3929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}