#122: Home Assistant: Pythonic Home Automation Transcript
00:00 Michael Kennedy: The past few years have seen an explosion of IoT devices. Many of these are for the so-called smart home. Their true potential lies in the ability to coordinate and automate them as a group. How can your garage, wifi, Chromecast and window shades work together automatically? Chances are, these are all from different manufactures with different protocols and apps. That's why you need something like Home Assistant. This Python based app brings over 740 devices together and allows you to observe, control, and automate them as a whole. This week, you'll meet Paulus Schoutsen who created Home Assistant. This is Talk Python To Me, episode 122, recorded July 25th, 2017. Welcome to Talk Python To Me, a weekly podcast on Python. The language, the libraries, the ecosystem, and the personalities. This is your host, Michael Kennedy. Follow me on Twitter where I'm @mkennedy. Keep up with the show and listen to past episodes at talkpython.fm and follow the show on Twitter via @talkpython. This episode's brought to you by Rollbar and us at Talk Python Training. Be sure to check out what we're offering during our segments. It really helps support the show. Hey everyone, before we get to the interview with Paulus, let me just share a quick news item with you. So last week I was interviewed on one of the other major Python podcasts, Podcast.__init__. And the topic was integration to MongoDB. So I met with Tobias Macey and we had an hour long conversation about why and how and so on, I migrated all the podcasts and training sites from SQLAlchemy and a relational database over to MongoDB. What went well, what didn't, lessons I learned, things like that. If this is interesting to you, just check it out at podcastinit.com, or if you subscribe to the podcast, you can find it there. Now, let's talk with Paulus about home automation. Paulus, welcome to Talk Python.
02:18 Paulus Schoutsen: Yeah, welcome, hey.
02:19 Michael Kennedy: Hey, great to meet you and talk with you. I'm really excited about this project that you've created. One of the biggest, booming, growing, exciting areas is not really smartphones and devices anymore. It's internet of things and smart homes and little things that used to just be dumb widgets that we can now bring together. And really, what you've built is not about the widgets, but about the bringing together, right?
02:43 Paulus Schoutsen: Yes. The main goal of Home Assistant is to you know, we call it the three pillars. Observe, control, and automate. The observe part is first bringing together, just knowing what's going on in the house. Control is being able to control the devices in your house. And then automate is based on observations, issue control commands.
03:08 Michael Kennedy: Yeah, that's awesome. I'm so looking forward to chatting about it. But before we get into all the details there, tell us about your story. How'd you get into programming and Python?
03:15 Paulus Schoutsen: During college, I studied I initially studied computer science. But I actually dropped out. I was more interested in data. So I started doing a business intelligence major. And there I was always doing stuff with data. And I was always doing small projects in college all the time. And that's where kind of like, Python started to sneak in in the beginning. Just based on how I know used the program it was still a lot of PHP. But I slowly switched over to Python. Actually, when I was doing my master's thesis that's when I started doing a lot more Python. That's also when Home Assistant started.
03:55 Michael Kennedy: At school, what was your thesis on?
03:56 Paulus Schoutsen: My thesis was on Medicaid fraud detection for the federal government here in the United States. Wow, that's really cool. I think there's really an untapped area in all of this medical information. It's pretty locked up, but it's starting to open up for machine learning and things like that. Yeah, I think there will be a huge potential. Just like, even anonymized data can give us so much insight, in all these things that are going on.
04:19 Michael Kennedy: Yeah, it's going to be cool as we get enough of this data out to actually do analysis. Okay, fraud is also good. Make sure that people who paid for the care or are paying for the care get it, that's great.
04:32 Paulus Schoutsen: This is obviously unrelated to the topic of the podcast. But the fraud was actually not the people paying for the insurance. It's was the doctors and the hospitals.
04:41 Michael Kennedy: Wow, that's kind of creepy.
04:43 Paulus Schoutsen: Medicaid is a free insurance. So, those people actually didn't care how much got billed for them. A lot of times.
04:49 Michael Kennedy: Right, they maybe didn't even know. Okay, I can see that. That's a little more deserving. Good that someone's keeping them honest, that's cool. So what do you do day to day these days?
04:58 Paulus Schoutsen: I'm a senior software engineer for Appfolio, which is a software as service company. We provide product for property managers to run their business in the cloud and product for lawyers to run practice management in the cloud. And I work on the lawyer side of things. It's called MyCase.
05:14 Michael Kennedy: Okay, that's really cool. I feel like there's an untapped market, well, not entirely untapped because you guys are doing it, but there's a big market for these super niche toolsets. Just, you pick your vertical, and there's probably a not great solution for most people, right?
05:31 Paulus Schoutsen: Yeah, definitely. And I think so many verticals are still like, those things on pen and paper, or Excel spreadsheets for everything. And being a software as a service, being able to build like an integrated solution that, you know, integrates everything from like intake of clients to like billing clients to keeping track of your work, sharing things with clients. There's a big opportunity.
05:54 Michael Kennedy: Yeah, that sounds really cool. We can definitely talk more about that. But let's talk about home automation. So, you've been working on Home Assistant for about four years. And before we get into Home Assistant, I'd kind of like to get your vision of the perfect home automation. Cause you've written kind of a blog post manifesto type of thing, like, here's where we should aim for. What's your vision?
06:16 Paulus Schoutsen: Yeah, so my vision for home automation is a lot of people kind of like, it's kind of like the early adopter phase where we in are right now. Where all these devices are entering the houses and people are realizing that with their smart phone they can control their lives. They can control like, their thermostat. But they tend to forget that it's usually not faster to, you know, like, use your smartphone. They tend put in all these smart lights and all of the sudden your switches don't work anymore cause your smart lights always need energy to be communicating with the network. And to be part of the internet of things. All of a sudden, people end up not having any light switches. Which means, when they enter a room they have to get their phone out and their phone, make sure it's connected to the network and actually, they actually are instead of improving their lives, they are decreasing their life experience kind of.
07:11 Michael Kennedy: Yeah, I totally think you're right with that. I mean, these things need to be kind of seamless like they are, but better. They need to get better, not just different. Which makes it really a big challenge but a big opportunity. You know, a quick, quick story. I was out biking the other day and I got this smart bike lock. It's super tough, this smart bike lock, Bluetooth. You can control it, do all sorts of stuff. Works kind of okay. But you unlock it with your phone. And the phone wouldn't connect to it. It was locked up. And I was like, what do I do? There's no key. I might be just stuck here cause the stupid Bluetooth won't pair! What am I going to do? And this is like, this is the problem. It's fine until it doesn't work sometimes, right?
07:54 Paulus Schoutsen: Yeah, and I think that's another important part of your internet of things devices. When you want to like, when your phone doesn't work or you can't connect to it, you should still be able to use your device. So in the case of your lock, that's a very bad experience. Like, oh the Bluetooth doesn't work. Well, you're screwed.
08:16 Michael Kennedy: Yeah, yeah. There is a little panel, but it wasn't working as well. So, it was bad.
08:22 Paulus Schoutsen: For example, the Philips light bulbs. When you turn them off with electricity, like just electricity off and then you turn them back on, they will reset and turn on with a white light like you would expect from a normal light bulb. And so that's a nice experience, if you have a light switch you can at least turn your light switch off and on to get the light to turn on.
08:41 Michael Kennedy: Right, so the failure mode is kind of like the dumb home. Or the dumb device in a sense.
08:46 Paulus Schoutsen: Yeah, exactly.
08:48 Michael Kennedy: These are the lights that you can dim, you can change the color. So they just go back to full power light. Or something like this, huh?
08:54 Paulus Schoutsen: Yeah.
08:55 Michael Kennedy: Okay, that makes sense.
08:57 Paulus Schoutsen: Part of the home automation part of it is a lot of people will build automations that are optimized for their own lives. Like, as one person in the household. But most times, you aren't one person in the household. So it's very annoying if an automation triggers and it's not right. Somebody is like, I'm annoyed by it. Foe example, if you have this automation to turn off the lights or dim the lights when you are going to watch a tv series, if somebody is reading a book on the side, then their light is gone. And so it's very difficult. You want to write automations that have a very, very high success rate. And the problem to writing automations with a very high success rate is you either need to have a very scheduled life, you know, you have to constantly be like every day has to be the same and have patterns for everything. But for a lot of us, that's just not the case. So we need to make small automations that improve our life but do not work against you.
10:00 Michael Kennedy: Right.
10:00 Paulus Schoutsen: In the case of going off the pattern.
10:03 Michael Kennedy: Yeah, exactly, like, for example, you come home and normally you unlock the door with your phone. But your phone is dead, now what?
10:09 Paulus Schoutsen: Yeah, exactly. So one of the automations I have which has been very stable is when we open the door my phone is connected to the wifi. So Home Assistant knows I'm home. It will turn on the lights, but only when the sun is down. And so the worst case is that the light is not being turned on for me and I have to hit the switch.
10:29 Michael Kennedy: Right, so the failure case is like, what you had before.
10:32 Paulus Schoutsen: Yeah, exactly.
10:33 Michael Kennedy: Okay, that's really cool. So another thing that you said in your vision was the perfect app is no app. What did you mean by that?
10:42 Paulus Schoutsen: It's kind of what I was talking about before, how the switch is faster than like opening your phone. And for most of these applications that you're having in your house, it is just as convenient, sometimes even more convenient, to just go to the device and control it. The only thing that's coming up now that's going to be faster and more convenient is the voice assistance. So there's like the Google Home, Amazon Echo. Because those devices, even if your hands are dirty, for example you're cooking or something, you can still control it and it's very convenient.
11:13 Michael Kennedy: Yeah, I think this is, that example really highlights some of the huge possibility and benefits but also some of the challenges, maybe even apprehension for some people. That we have this possibility of having this thing listening all the time. You can just speak to it like, like on Star Trek. Like, computer, dim the lights, please. It's amazing, it's just so incredible. But, you know, a lot of people are concerned about having a thing that is constantly listening to them in their house all the time, right?
11:41 Paulus Schoutsen: Yeah, I know that's definitely a valid concern. And so there's actually been a couple of startups that are focusing on local speech to text. So they're doing local listening. And one has recently come up and we've integrated, they actually contributed an integration to Home Assistant to integrate a system that looks very promising. It's called Snips, Snips.ai. They are a French startup. And they're working on making it fully open source. And their system is already free to use on Raspberry Pi. But the idea is they pretty much built like an Amazon Echo, with like the way you configure it. With like, these are the sentences that I want to support. But then they will run completely locally on your device. So there's no cloud connection. And it also means that it is way faster.
12:29 Michael Kennedy: Yeah, that's one of the drawbacks of a lot of these cloud things. I mean, it's ridiculous. I can ask my phone, like, hey, Siri, what time is it? And it will wait like five or ten seconds and go, it's 2:30. Why did it take you ten seconds to know that it's 2:30? This is not a huge challenge, man! Do you think that's a transient situation that we're in? Or is this a permanent thing? Like, is that just because the cloud is where the power is and it's moving to the devices? Or is that like, places like Google would rather have the data to learn from and to massively scale on, rather than make it work at home?
13:09 Paulus Schoutsen: I think there's multiple things in place. One is definitely Google and Apple, et cetera, they want to have your voice. Because they can see, it's their training data, right? So the more voices they have, the better they can learn from. But I think the biggest difference is the way how these voice assistants work. So the Amazon Echo has a very basic approach. As in, if you want to write a skill or voice part for Amazon Echo to know to support like, add something to my shopping list, you have to register with Amazon every single sentence that the user can ever say. So you have to say, you know, you can have like slots that go like be filled in with the actual words. So you can have add item to the shopping list. But then you have to add another sentence, add item to my shopping list. Instead of the shopping list. And so you end up with thousands of variations of your sentences. But the way Amazon Echo, this allows them once the speech to text has been done, they can very quickly map it to a sentence that you can say. Because there are only 10,000 possibilities of all the different skills that are active on your device.
14:19 Michael Kennedy: Right, it's not wide open machine learning. What did that person mean? Sentiment analysis type of thing, right?
14:25 Paulus Schoutsen: Yeah, and so that's where Google is, the Google Home, at least the way I think it works is they do more speech to text and then like get the intent of the text. Which is a different approach. Which is way more difficult to push into the devices. They're going to try, obviously. Obviously Google. But Apple's talking about like adding an AI chip and getting all these things to run locally. Because, you know, privacy is important.
14:52 Michael Kennedy: Yeah, like you said, don't discount latency. That's super annoying that it takes that long to get simple responses. Okay, I think you're right. This is a great way to interact. If you can interact with your home there's some challenges, like discovery. Like what can you say to your home is sometimes hard to know, right?
15:11 Paulus Schoutsen: The problem with a a voice assistant is that we're still in the beginning phase where we don't really understand what you're saying. We have just hard coded every sentence that we expect. So a lot of people call it artificial intelligence, but it's not really artificial intelligence yet. And I think the whole idea of having a normal conversation is not there yet. Once we can kind of like, you know, as if you're talking to a person, like, hey, what can you do? And like, it's easier to interrupt them, et cetera. Then we can probably discover commands easier. Right now, it's, you know, Google Home and Alexa both have newsletters. They'll email you, did you know you can ask this, kind of things. That's how we actually learn.
15:58 Michael Kennedy: Right, right. It's just a home, smart speaker lesson every week. Let's get specific about Home Assistant. So there's a number of, what would you think of it? Like a hub for smart home? Or how would you describe it?
16:14 Paulus Schoutsen: Yeah, I would say it's a hub. Because the way I see a hub is the hub is a device or a piece of software that speaks multiple languages and combines all these devices together and provides the user and interface or ways to control them.
16:32 Michael Kennedy: Well, yeah. You spoke about the challenge of having to open your phone and open an app to do a thing, rather than just hit the switch. Well, there's an app for your plug, there's an app for your lights, there's an app for your garage door. If you've got to go to the ten different apps because you want to do a thing, I think that's super crazy. So Home Assistant will speak the language of all these devices and give you one place to go, right?
16:54 Paulus Schoutsen: Yeah and besides giving the user one place to go, it also means there's one place for automations to run. Because if you have two different vendors, two different products of vendors that don't speak together, don't work together. They have their own apps, their own clouds, then you will not be able to automate them without having an intermediary that knows about both of them.
17:15 Michael Kennedy: Right, for sure.
17:16 Paulus Schoutsen: And so, the motion sensor in a Nest cannot be used to trigger a light being turned on or something like this. Well, Home Assistant knows about your light, it knows about your Nest, it can combine these things.
17:28 Michael Kennedy: Okay, so it basically, it can receive. It does a couple of things. It observes and monitors the state of many things. And it also receives events, or at least state changes, right?
17:39 Paulus Schoutsen: Yeah, so the way it works is that we have a state machine. So Home Assistant, the architecture at the core of Home Assistant is an event bus. In which the events related to your house are being fired. And these are, for example, a button is pressed. Or motion is detected. However, actually motion detected is a bad example because we represent that as a binary sensor because we can say motion is being detected.
18:08 Michael Kennedy: Yeah.
18:08 Paulus Schoutsen: And so, the difference about something that happens right now and something that is happening is we have to capture it differently. We cannot just have an event. And so I'm told the idea of like a state machine. And the state machine captures representations of the devices in your house or other things. So, for example, in the state machine, you have a light. And the light is, the main state is on or off for a light. And they have attributes that describe the state. So for the light, this could be the color, the brightness. Maybe the color temperature. For a media player the state is for example paused or playing. The attributes could be what type of media, is it a TV show, is it a music album. If it's a music album, what's the artist, what's the song title, what's the track number, et cetera.
18:58 Michael Kennedy: Yeah.
18:58 Paulus Schoutsen: And then, whenever we change the state in the state machine we will fire an event to say, hey the state changed. And so it actually changed from this to this. And this allows other automations, for example, that can listen to our events or to state changes to the state change events, to kind of respond to whenever the device representation changes in Home Assistant. And we can start automating things.
19:24 Michael Kennedy: Yeah, that sounds really useful. So you start out observing them and then you layer on state tracking. And then at the Home Assistant level, you fire the event that hey, this state has changed, anybody care about this thing? Maybe they do, maybe they don't. Things like that. This portion of Talk Python To Me has been brought to you by Rollbar. One of the frustrating things about being a developer is dealing with errors, ugh. Relying on users to report errors, digging through log files trying to debug issues, or getting millions of alerts flooding your inbox and ruining your day. With Rollbar's full stack error monitoring, you get the context, insight, and control you need to find and fix bugs faster. Adding Rollbar to your Python app is as easy as pip install Rollbar. You can start tracking production errors in deployments in eight minutes or less. Are you considering self hosting tools for security or compliance reasons? Then you should really check out Rollbar's compliant SaaS option. Get advanced security features and meet compliance without the hassle of self hosting. Including HIPPA, ISO 27701, Privacy Shield, and more. They'd love to give you a demo. Give Rollbar a try today. Go to talkpython.fm/rollbar and check them out. So let's talk a little bit about how you built it, what technology is behind it. It's Python based, right?
20:41 Paulus Schoutsen: Yes, so it's Python based. It's Python 3. It's actually started out on Python 2. I was still learning Python back then and I just kept reading up on it. And I was like, wait, there's a Python 3? And then I was looking more into it. And I was like, Python 3 is the successor? And you know, everybody's moving to Python 3? Even though four years ago there was a lot more Python.
21:03 Michael Kennedy: It was still up for debate at that point, yeah.
21:05 Paulus Schoutsen: Yeah.
21:06 Michael Kennedy: But it's starting to look like a good choice for you at this point, right?
21:09 Paulus Schoutsen: Yeah, but I was looking at like, different things. And I was reading that there was no new features going to be added to Python 2. And Python 3 is being actively developed. And so I was like, I got to make the switch. So I used 3.2, and I wrote it to Python 3. I didn't really have any users anyway back then. So I could do whatever I wanted.
21:28 Michael Kennedy: Yeah, breaking changes were allowed, it's fine, right?
21:31 Paulus Schoutsen: Yeah. And initially it was all using threads. So I used the thread pull and we had like, even if you would register for listening to an event, you would just register a function. And we would pass the function and the event object to a thread pool to execute in a thread. And be done. And this would actually cause crazy deadlocks some times because there would not be enough threads. Some threads would be waiting for work to be done, but that work would not be done because there were too many threads waiting for work to be done. It's a classic example.
22:04 Michael Kennedy: Those are hard, yeah. Those problems are hard to solve.
22:05 Paulus Schoutsen: Yeah.
22:07 Michael Kennedy: You know kind of where the problem is, but like, tracing down the details can be painful.
22:12 Paulus Schoutsen: Yeah, well, actually the solution came by switching to asyncio. So nowadays, and I think last year, we spent over a year converting to asyncio. I didn't know asyncio, but there was this engineer from Mozilla Ben Banghert, who was using asyncio at work. And he stopped by and he was using Home Assistant and was like. Our minimum requirement at that time was already Python 3.4. And he was like, if you're using Python 3.4, then asyncio is available. With asyncio it will be faster, there will be uhh, we can avoid deadlocks and it will be predictable. Because of all the locks in the threads was actually at a very unpredictable execution order.
22:56 Michael Kennedy: Sure, and at this point, it's waiting right at the network level. Right when it's talking to the device or waiting for a response or something like this, right?
23:02 Paulus Schoutsen: Yeah, with asyncio you can just, pause within the work until you get like the next piece of information and you can immediately execute.
23:11 Michael Kennedy: Yeah, it's really a nice way to add concurrency the concurrency you were looking for in the Python 3 world, or just the Python world. Because you kind of don't have to deal with the GIL very much at that point. You don't have this thread overhead. You don't have the deadlocks that come with it for the most part. It's really nice.
23:28 Paulus Schoutsen: Yeah, I think that once asyncio was introduced to me, initially when Ben stopped by and said you should use asyncio, I was like, yeah, okay, great. But I don't know. And so, either you come up with the PR, or it's done. And he actually delivered. He came up with the pull requests. So it's not fully done yet. But it was very exciting because I was learning something new in the Python world. Instead of like, you know, every time I do a pull request against Home Assistant or code reviewing it was kind of the same kind of technology. And so this was something new. And so we jumped on it and we managed to get the core converted to asyncio. And then over the course of eight months we've been converting more and more core pieces of Home Assistant to asyncio. To at which point, now, we're fully asyncio.
24:18 Michael Kennedy: Yeah, that's totally great. And did that lower the types of devices that could run on? So before maybe it required a PC, now it could run on a small device, or smaller devices than before?
24:32 Paulus Schoutsen: So we always ran well on Raspberry Pi, so even though we had tons of threats that did not... I mean Raspberry Pi 3 at least it was like running okay. But right now with asyncio we run blazing fast.
24:49 Michael Kennedy: Yeah, that is really cool.
24:50 Paulus Schoutsen: I think it allows us to even allow for lower power devices to use those for Home Assistant.
24:56 Michael Kennedy: Yeah I'm sure it really does make that more accessible, that's awesome. So that's the asyncio stuff is obviously how you talk to all the devices instead of threads, what web framework are you using?
25:08 Paulus Schoutsen: Let me just say what we used before. So initially we started out with something I built myself on top of the standard library HP server. Which is not a good idea, by the way. But for me it was kind of, you know, I'd never built a web framework, and I was like, we actually only have one route, which is serving up the single place applications, so you know, I'll just write something quickly myself, and then of course we needed more than one route, and then we needed SSL, and it became very fast...
25:36 Michael Kennedy: You're like, aww, what have I done?
25:39 Paulus Schoutsen: Yeah, well for a long time Home Assistant was my place where I was practicing writing sort of new technologies and et cetera, so this was kind of like, yeah, why not? But then somebody actually submitted a PR to migrate us to a Werkzeug, which is a WSGI framework.
25:59 Michael Kennedy: Yeah, part of Flask and all that, yeah?
26:00 Paulus Schoutsen: Yeah, it powers Flask, indeed. So that was great and we were using it on top of threads, so we used the Cherry Pi WSGI server to power all of this. And then we were migrating to asyncio and so when we migrated to asyncio it was like, well, we need a new web framework.
26:19 Michael Kennedy: Yeah, 'cause none of those do it.
26:20 Paulus Schoutsen: Yeah, so WSGI is not asyncio compatible. The way it's set up is it's all threading. So people have explored seeing if they can make it async compatible, and that's not possible. But then I stumbled upon AIOHTTP, and it's very difficult not to stumble upon it because they pretty much wrote the request for asyncio. So you cannot use requests either, so you have to use AIOHTTP, but it's a great, great, library. It's super fast, it's actually powered by cython under the hood.
26:52 Michael Kennedy: Oh, that's awesome.
26:53 Paulus Schoutsen: So a lot of execute it and see. And they did a great job, they wrote both the client and the server and they have a framework on top of that. So we were able to port our HP stack over to using AIOHTTP, and because we use asyncio it was actually very trivial for us to add web socket support, for example. Web socker support is an http standard that starts as a normal request to your HP server, but gets upgraded to become like a long-living connection that you can send messages back and forth.
27:26 Michael Kennedy: Right, and normally it's message to the web server, response from the web server, done.
27:30 Paulus Schoutsen: Yeah, this is very difficult in a threaded environment, because in a threaded environment if you have a worker for a request, then all of a sudden that bottom worker is going to be occupied forever. And with asyncio there's no such thing as the concept of worker, there's just tasks and there is pending work to be done. So you can scale it way higher.
27:51 Michael Kennedy: Yeah, absolutely, I mean that's kind of how NodeJS took off, like that type of programming model. It's really cool to see it here in Python.
28:01 Paulus Schoutsen: Yeah and it's working very well for us. Because of asyncio and we know there's no concurrency, we actually managed to get rid of a lot of locks in our code. And you know when you read from a dictionary, and then two... later you write to a dictionary, you know that no one else can mess with it. And also we're having less weird bugs, because these things can just no longer happen.
28:28 Michael Kennedy: Fewer heisen-bugs. Yeah so between the awaits, you have control over it, that's very cool. Alright, so that sounds like such a fun project to be working on. Are you looking for contributors and stuff like that?
28:42 Paulus Schoutsen: We have a lot of contributors right now, I think we do a release every two weeks and we have between 50 and 60 people contributing code very two weeks.
28:51 Michael Kennedy: Wow, that's really awesome.
28:53 Paulus Schoutsen: Yeah, so right now it's just like keeping up with the pull requests is the biggest challenge.
28:58 Michael Kennedy: Well maybe you could have some contributors who just try to review the pull requests, and just try to mediate the bugs and feedback, and things like that.
29:06 Paulus Schoutsen: Yeah, we have a few of those but they are... when you look at open source contributors, the people that will help and like, do code review and these kind of things, they're rare compared to the people who will just submit a pull request and get their feature in.
29:22 Michael Kennedy: Yeah, of course, of course. There's a lot of people who ask me, "Hey, I want to get started in open source, what's a project that I could just do something simple in?" And so things like that, maybe if people are listening, they could come volunteer to review some pull requests, or something.
29:34 Paulus Schoutsen: Well I think if you get into open source, coming in and doing some code review definitely helps, but I think the best first step into open source is usually just making some contributions to the documentation. Getting in because that way you also learn the workflow of a project, seeing who works there on it, and even if you hang out in the chat, you can talk to them, and once you know the project better, then start submitting some features, which makes it a lot easier, because in the end it's very difficult to get a pull request of somebody you've never spoken to, who has clearly put a lot of effort into a feature, and then having to tell that person, like, this feature is, we're not going to merge this because it's not going to work for these use cases that you did not think of. Which, if we would have just spoken before for a minute or two, I could have told you about this.
30:29 Michael Kennedy: Yeah, that's a really interesting point. It would be great if the whole, git flow PR story had like a preliminary step before the work was done, before the PR was submitted, like, I'm thinking of this. Prove my general idea, huh?
30:47 Paulus Schoutsen: Well yes and no, because sometimes you're also doing, not per se for the project, but just for yourself to learn, and then at the end you're like, oh, maybe this is valuable for someone else. But for me as a maintainer, it's important that when I accept the pull request, I also accept responsibility, right? Because when Home Assistant breaks, they don't come knocking on the door of that one person who submitted a pull request that contained the bug. No, they complain in our chatroom, in our forums, our own Twitter. And then we have to put out a hot fix.
31:24 Michael Kennedy: Which is, I'm sure, not fun.
31:25 Paulus Schoutsen: No, it's not. It's always hectic, because you're like okay, there's a lot of people angry, well we have a fix, okay, good enough. And then we have to have all the tests run, and then push it to PyPI, update the forums, update the release notes, and it takes more work than, we've automated a lot, but still, it takes a lot of work.
31:45 Michael Kennedy: Yeah, even if maybe rolling back documentation or something, right?
31:48 Paulus Schoutsen: Yeah, even reverts becomes a mess, because you have all these people who later want to revert the revert, and it's a mess.
31:57 Michael Kennedy: I hear ya, so let's talk about some of the devices you support. Let's go from the beginning, where you kind of created this for yourself, and you supported the devices you had, right? So where did it start?
32:08 Paulus Schoutsen: So it started with the Philips Hue light bulbs, so when they got released in the United States in, I guess, 2013. Or maybe, no, I think somewhere in 2012. End of 2012 they got introduced, and I bought them, and since then I was playing with it, but I was just writing simple scripts to turn them on, turn them off, later, and slowly that script just for turning it on and off later, slowly has grown into Home Assistant. Because then I wanted them to turn on when I came home, but then I didn't want them to turn on when I came home when the sun was up, so I have to keep track of more and more pieces of information. But the second thing I integrated was the Chromecast, and that's actually one of the best things I ever could have done, because the Chromecast has really taken off, and it's actually the Google Home is also a Chromecast audio.
33:04 Michael Kennedy: Oh wow, okay.
33:05 Paulus Schoutsen: So when you start Home Assistant, it automatically detects your TV if you a have a Chromecast there, it will detect your Google Home, any other guest audio devices you have, and then when you start playing music on your Google Home, it will actually immediately show the album art inside Home Assistant, you can pause and play it, and they have a great protocol where they push every state update to us, so the moment something happens on the Chromecast, we know about it in Home Assistant.
33:30 Michael Kennedy: Right, so there's not a latency of you pull in a thing, or whatever.
33:33 Paulus Schoutsen: Yeah.
33:33 Michael Kennedy: Okay, so that's really cool and those are some of the first two, and now you have more than a handful, right?
33:39 Paulus Schoutsen: Yeah, so right now we have over 740 or something devices, and it just keeps growing. There's a lot of devices, either very old ones that extend support, but there are also things like there was this South African contributor who had some South African startup IoT device in his house, and he wanted to integrated it. So he submitted a pool request, and these are the kind of cases, and this has been since the beginning, getting pool requests for devices that I cannot verify. It's always kind of, like at the beginning I was super scared, like what am I doing? How can I release to people code that I haven't even tested? I have to trust this random person on the internet.
34:22 Michael Kennedy: Not only have you not tested it, you might not have even really run it?
34:25 Paulus Schoutsen: Yeah.
34:25 Michael Kennedy: Crazy, right?
34:27 Paulus Schoutsen: Well the way Home Assistant works, I can verify the code the way things get called to make sure that they don't do anything that goes against the design of the system. And actually put in a lot of warnings in place, if the fetching of certain things takes too long, we'll print a warning. But the reasoning why I decided to actually accept these kind of pull requests is that for a lot of people it's way easier to fix a bug than to add support for something new. So this one person might still buy and contribute support for one version of a router, for example. And then, you know, we have support for this one version, and someone else stops by and actually sees a bug and for that other person it's super easy, you just come in and fix one bug. And then someone else stops by and it's like, oh, if you support this version of the router, well with very little work we can add this version of the router, and you know, it starts growing and growing. And we've seen this with various of our supported protocols and devices, where it just keeps growing. You almost get like many communities inside Home Assistant of a few people who are rattling about the Sonos component, or the Z-Wave component.
35:39 Michael Kennedy: Right, if their main passion is around the thing that they're integrating to Home Assistant, it can form little sub-communities around it, right?
35:47 Paulus Schoutsen: Yeah, and I think the nice thing about Home Assistant is that there's a lot of open-source software out there that you can't really use at home, right? Because there's some cool things like Kubernetes or certain web frameworks but who writes a website for their house? Not too many people, right? But if you're at home and you want to play with things, the nice thing about Home Assistant is that it controls your house, so there's immediate results. There's immediate satisfaction of your work.
36:18 Michael Kennedy: This portion of Talk Python to Me is brought to you by, us! As many of you know, I have a growing set of courses to help you go from Python beginner, to novice, to Python expert. And there are many more courses in the works. So please consider Talk Python training for you and your team's training needs. If you're just getting started, I've built a course to teach you Python the way professional developers learn, by building applications. Check out my Python start by building 10 apps at talkpython.fm/course. Are you looking to start adding services to your app? Try my brand new consuming HTTP services in Python. You'll learn to work with restful ATP services as well as SOAP, JSON, and XML data formats. Do you want to launch an online business? Well, Matt Makai and I have built an entrepreneurs' playbook with Python for entrepreneurs. This 16-hour course will teach you everything you need to launch your web-based business with Python. And finally, there's a couple of new course announcements coming really soon, so if you don't already have an account, be sure to create one at training.talkpython.fm to get notified. And for all of you who have bought my courses, thank you so much, it really really helps support the show. Yeah, and it's pretty low-risk, right, because you can keep it behind your firewall on your router if you want, and it's really sort of just for you, unlike a lot of the internet of things, they maybe have their own security issues.
37:39 Paulus Schoutsen: I mean yeah, there's a lot of internet of things companies like starting up all the time, and they put products out there and they have limited amount of time, limited amount of money, so they will try to get to market super fast, and security is sometimes an afterthought. Or after a couple of years, the company goes bankrupt, and either the cloud shuts down and your device doesn't work anymore, or if the device works locally, it doesn't receive any security updates anymore.
38:07 Michael Kennedy: Right, which is just a ticking time bomb. But it's hard to know, like my router at home has its little light, and it's green, usually, but if there's an update for it, it will start blinking yellow. I'm like, oh, maybe I should go look. But I don't know that it's not receiving updates if it weren't anymore. I just know when there is an update. I don't know that it didn't check anymore, you know what I mean? So it's really hard to kind of understand that, but Home Assistant itself can run internally in your house, in one of these Raspberry Pi's if you wanted, right? Are there devices you can get that's like a Home Assistant device? Or do I have to get a Raspberry Pi and set it up, do I have to 3D print a thing, or what's the story about that?
38:53 Paulus Schoutsen: Right now we're just an open source project. And so we don't have any devices, we kind of focus on the Raspberry Pi, from the statistics that we get, we see that actually 70 percent of our users are on the Raspberry Pi. But to make it easiest, like we're trying to slowly get to where we can either have Home Assistant devices, or have a better experience, so we're working on and we just released today, actually, hass.io, which is our new operating system for Raspberry Pi.
39:24 Michael Kennedy: Yeah, that's a huge announcement, this is like a brand new thing, it's not just like, here's some feature, there's a whole new operating system for it.
39:30 Paulus Schoutsen: Yeah, it's not a 100% operating system like we've built from the ground up, we based it on Raisin OS, which is an open source operating system to run Docker containers and embedded devices. So we use Docker and Raisin OS with hass.io to allow a user to Flash institute a Raspberry Pi and no longer have to worry about system updates, or updating Home Assistant, all these kinds of things can be done from the user interface.
39:58 Michael Kennedy: Yeah, that's awesome.
39:59 Paulus Schoutsen: Yeah, and so this is going to be our approach to get the next so many users, because right now our user base is very much, you know, do it yourself people, people that know programming, people that are comfortable enough to get a Raspberry Pi, open a terminal SSH into the Raspberry Pi, these kind of things.
40:19 Michael Kennedy: Previously the setup story was, SSH in there, create a virtual environment, pip install some requirements, it was that level, right? And now with this announcement of hass.io, it's like a web interface that controls and configures and extends Home Assistant, right?
40:36 Paulus Schoutsen: Yeah, so right now once you download hass.io, when you Flash it to an SD image and you write, you have to edit one text file in the SD image to have it connect to your wifi if you want, but you can of course plug in an ethernet cable into your Raspberry Pi, but the moment you start your Raspberry Pi it will download the latest version of Home Assistant, it will update the operating system to the latest version, and you're good to go. After 20 minutes, more or less, it will start up Home Assistant, Home Assistant will start automatically discovering which devices are in the network that it knows how to set up automatically for you, and you can start automating things.
41:13 Michael Kennedy: Yeah, that sounds really cool. So I'm sure that's going to increase adoption, and you had said in the early days you wanted to kind of keep it to this techy crowd and it feels like this is a major step to make it general consumer type stuff.
41:26 Paulus Schoutsen: Yeah, so we used to very much keep it focused on the techy crowd because you know, when something breaks they can also contribute back. But we've grown so big now, we have so much momentum that it's a pity if we keep this away from other people as well, so we want to have more people exposed to it, more people using it. And I think if you look at the amount of integrations, we support so many things that we want to get more people onboard, and even get more integrations, and become the ultimate home automation hub that you can run locally.
42:02 Michael Kennedy: Yeah, that's awesome. You have a lot more integrations than many of even the commercial ones, right?
42:08 Paulus Schoutsen: Yes, but that actually it kind of makes sense, the commercial ones probably have partnerships with other companies, they will test it every time one of those companies will update a new product, they'll have to make sure it's integrated and those APIs have to stay stable, and we have just an unlimited size army because we're open source, and so people will consume any API that's out there and integrate it into Home Assistant. And people are, you know, it's running in their house and they depend on it, so if they see it's breaking or an update coming out they will submit a patch to Home Assistant so that we are also, we're always on the latest APIs and always getting better.
42:50 Michael Kennedy: Yeah, it's really a testament to the power of open source, like all these people, they have one device and some technical skill. And they really want that one device to go in with the rest of their devices, and so, you can't do that at a commercial one. You can just send in like a support request, please support this device some time soon! I'd like it to, you know?
43:09 Paulus Schoutsen: Yeah, exactly. And one other strategy that we've had since the beginning is that we've always made sure that whenever anyone contributed support for a device, that the support itself was actually contained in a standalone Python library that was published to PyPI. That way, if you don't want to use Home Assistant but you still want to use that one device, you could still use its library and even end up maybe making improvements to that library or fixing bugs that we then also can consume inside Home Assistant.
43:38 Michael Kennedy: Right, and that's the way it's still set up now, like all of these integrations are separate?
43:40 Paulus Schoutsen: Yes, every, yeah. Because if you look at other home automation, even open source ones, they have all this protocol logic inside their own code base, and it's all coupled with their own way how they are doing things. So it makes it difficult to use that code for our project, for example.
43:58 Michael Kennedy: So what's the story of if I want to, suppose I get a device that's not supported and I want to add support through it. Take me through the steps of how do I add a new device to this.
44:08 Paulus Schoutsen: Well, first you start reading the documentation.
44:11 Michael Kennedy: For Home Assistant, right?
44:12 Paulus Schoutsen: For Home Assistant, yeah. I think the first step is to see if this device has an API.
44:16 Michael Kennedy: Okay.
44:18 Paulus Schoutsen: And this might either be a public API or if they have a mobile phone app, some people will use something like Wireshark or something to check out the protocol. For most things out there, there will be someone who has already done this. You can find, maybe there's a package for Node, maybe there's a package for Python already, so actually for most things that you'll want to integrate, you'll find a library ready to go. If you have to write one yourself, you'll have to use those API dumps or their public API to talk to the device. So once you have a library that can talk to the device, it will be very easy to integrate into Home Assistant. So anything that you want to integrate into Home Assistant is actually based on classes. So, we have for example, a switch class. So you would just extend the switch class, and then we use the Python properties, class properties so you have a property name, where you return the name of your device, you have a property, is on, that will return a bullion if the switch is on or off, and they have two functions, turn on and turn off. And if you implement those to fire the correct commands to your device, they're already in, that's it.
45:24 Michael Kennedy: Yeah, that's awesome.
45:27 Paulus Schoutsen: To integrate a device, you only have to integrate its class, and all of the things under the hood, how like the state machine has the representation and the event bus, all those things, you don't have to worry about. Those things are abstracted away.
45:39 Michael Kennedy: They work with your abstract base classes, and you don't care?
45:42 Paulus Schoutsen: Yeah.
45:44 Michael Kennedy: Nice, do you have multiple inheritance? Like, something is a switch and it's color-adjustable, or something like this?
45:50 Paulus Schoutsen: No, so we, the abstract class of the switch will have, color-adjustable will mean that it's actually a light. So the switch will have features that are not supported by every device and implemented abstract base class of the switch. So we have this property called supported features, which is using like integer bit flags to say which features are supported or not. And that's actually not being used inside Home Assistant Python, the core, because if you've go turn on, you can pass in the color, it's up to the implementation, they can just decide not to use the color. But the front end will, you know, we have a web interface to control everything. And the front end, for example, for a light it does not support color, so it will not show a color picker.
46:35 Michael Kennedy: Right, okay, cool. That makes a lot of sense. So that sounds pretty easy, if you can talk to the API, you can implement a couple of methods on an abstract base class, like it doesn't get a whole lot easier, right?
46:46 Paulus Schoutsen: Yeah, I think for a sensor, if you want to add a sensor which is a read-only piece of information that you want to put into Home Assistant, you can do that in like 20 lines.
46:56 Michael Kennedy: Sure, wow. Yeah, so hopefully that's encouraging people to go and integrate something, maybe that isn't in that 740 devices.
47:05 Paulus Schoutsen: Yeah, if people are interested in adding things to Home Assistant, I gave a talk at PyCon in Portland, last year, 2016. First it described how Home Assistant works, and then the last 20 minutes is how I'm writing a sensor and a switch from scratch, that actually interacts with the file system, as a demo.
47:25 Michael Kennedy: Okay, so check that out if you want to get started, that's really cool. So I saw that you have kiosk mode, what is that?
47:32 Paulus Schoutsen: Yeah, so actually recently, well not recently, this has been out for a while now, because people invest so heavily in Home Assistant, so they have Home Assistant controlling everything in their house. And they ended up installing Home Assistant on tablets, and having those tablets being mounted to walls at the entrance of their house, or somewhere else in their house. So people were, like, you know the normal Home Assistant user interface is not optimized for being on a tablet on a wall, where you would expect big buttons. Our user interface is optimized for mobile phones. So it looks like material design, you have small toggles, but you need a finger and you need to be close, you need some form of precision. And so for the wall mounts, people were like, we need a better approach. So we have support for custom panels in Home Assistant, so people were writing pages that would already show bigger buttons, or show a whole floor plan of the house, and show where in the house which lights are on, how much doors are open. But the kiosk mode now will actually take it one step further, we'll make sure the bar on the top is hidden and that the menu can no longer be opened. And that way, you will really have just a terminal that controls parts of your house, and people cannot go to the settings and start changing things.
48:53 Michael Kennedy: Yeah, that sounds really cool, actually. So that's your public one, and if you really want to change it, you pull it up on you laptop or something.
49:00 Paulus Schoutsen: Yeah, yeah.
49:01 Michael Kennedy: Okay, well that makes a ton of sense. So I was thinking of the things that I could automate in my house, when I was getting ready to talk to you. And to be honest, I don't really have many smart things, other than, like, my phone or my laptop, and my kids' phones and whatnot. We do have on our doors, we have a keypad entry for all the doors, so we don't use keys. But they don't have Bluetooth or anything, they're just literally like a keypad. But one thing that I am getting, is I'm getting an electric car, and it has one of those 240 volt charging units, and that thing has an app, and it has wireless connectivity, do you integrate with those types of things? Do you know? Or is that a PR I should be adding?
49:48 Paulus Schoutsen: That's probably a PR, I mean we have support for Volvo, so Volvo has an app for their cars, where you can see where the car is, and I think you can see the mileage, and how much gas you have left. And we actually integrate, Home Assistant has support for Volvo. So you can see everything on Volvo, so depending on the make and model of your car, there might be already.
50:07 Michael Kennedy: Yeah, so I'm thinking of getting a Bolt. I mean, I would love to get a Tesla, but you know. I'd also love a Ferrari, like I can want things.
50:14 Paulus Schoutsen: Of course. I don't think we have support for Bolt yet, but if they have an API that is either accessible or even public accessible, then it should be fairly straightforward to add support for it.
50:25 Michael Kennedy: Awesome, well maybe I'll do some kind of PR for you, because yeah, it's supposed to be like, I don't have it yet so I don't know, but it's supposed to, like, go on the wifi and talk to your app, and warm it up before you go outside, things like that.
50:36 Paulus Schoutsen: Wow, well I think that there's, there will be also nice things to do, like for example, reminders to charge it. Like hey, you were running low when you came home but you didn't plug it in.
50:47 Michael Kennedy: Yeah, yeah, for sure. I know it's on the wifi and it's low, so you better go pay it attention or something like that, right?
50:52 Paulus Schoutsen: Yeah.
50:54 Michael Kennedy: Awesome, that sounds really really cool. So let's talk about just a few other things, we're getting short on time here. You have just recently started a full-on Home Assistant podcast, why don't you tell people what that is, what it's about, how they can subscribe to you?
51:09 Paulus Schoutsen: Yeah, so three of our community members started this project, and it's all about Home Assistant and things around Home Assistant that have been happening in the IoT space. We're at episode four or five now, I think episode four just launched.
51:23 Michael Kennedy: Yeah, I think so.
51:24 Paulus Schoutsen: And it's all about, each episode they try to invite other members of the community that are either doing very cool stuff with Home Assistant, so I think last episode there was Carlo who has automated his whole house with Alexa, and Home Assistant to control everything, where even if he lives in Florida, so if he left his door open for too long, text to speech, the house would say to everybody inside, there's a door open so I'm going to turn off the air conditioning now, good luck. I was there on episode three, and they keep talking to other members that are developing the latest, greatest features for Home Assistant, and kind of like things how to use it, and it's getting quite some popularity. I think the first episode had like 2,500 listeners or something?
52:14 Michael Kennedy: That's amazing for a first episode.
52:17 Paulus Schoutsen: Yeah, it's really great. If people are interested, the URL is hasspodcast.io, but every episode is also promoted on our blog and Twitter.
52:26 Michael Kennedy: Right, and I subscribed to it, I just went to Overcast and I typed Home Assistant, and boom, Home Assistant podcast came straight up, so that always works as well. I'm sure it's on Google Play in addition to that. Cool, alright, so that's really awesome. And you guys have t-shirts, is that right?
52:42 Paulus Schoutsen: Yes!
52:42 Michael Kennedy: So if I want to automate my home and then get people to ask me questions about it?
52:46 Paulus Schoutsen: Yeah, so we've been selling t-shirts since, I guess somewhere in the beginning of the year, maybe a bit... around that time I think. And the t-shirts we have right now, 100% of the profits are being donated to the Electronic Frontier Foundation, that makes it easier for us to not have anything to do with income and taxes and all that stuff. And we also support the Electronic Frontier Foundation, which they do awesome work, among others they started the Let's Encrypt service, so that everybody can have an SSL certificate, which a lot of Home Assistant users are using.
53:20 Michael Kennedy: Yeah, with your hass.io, that's like a checkbox or something, right?
53:23 Paulus Schoutsen: Yeah, we make it super easy to get an SSL certificate to secure your connection to your house.
53:29 Michael Kennedy: That's cool, very cool. Alright, well it's super exciting, I definitely need to find a smart thing so I can start playing with Home Assistant. Maybe more than one.
53:42 Paulus Schoutsen: I would start with just a Chromecast for television.
53:43 Michael Kennedy: Okay.
53:45 Paulus Schoutsen: Because the nice thing about Chromecast is that it doesn't have a dedicated app that you need to control, the Chromecast is built in to the Netflix app, the Spotify app, to your browser, you open YouTube and it all of a sudden has a button, play on my TV. In your phone there will be like, show my screen on the TV, show my photos on the TV. It's a very powerful little device. And it's like dirt cheap, I think it's like 35 bucks.
54:13 Michael Kennedy: Yeah, it's incredibly cheap for what it is. Sounds very cool, alright, that's a great place for people to get started. Alright Paulus, before I let you out of here, let me ask you the two questions we always ask at the end of the show. One, if you're going to write some Python code, what editor do you open up?
54:27 Paulus Schoutsen: I use Sublime Text.
54:28 Michael Kennedy: Okay, Sublime, definitely a good choice.
54:30 Paulus Schoutsen: It's very fast, and it doesn't do too much, but it does enough.
54:36 Michael Kennedy: Yeah, absolutely, that's cool. And you've got quite a few lists to pick from, like maybe 741 at least, but how about a notable PyPI package?
54:46 Paulus Schoutsen: Well actually I was thinking about this question before the show started of what's one that I really like, and actually it's one that's now part of the, I really like asyncio, and I know it's now a part of the standard library, but I think that asynchronous programming sounds so magical but if you actually start looking at the source code, it's super super straightforward. It's just like, it has a list of tasks, that's every task, you have something to do, okay, do it. And then it just keeps, it's pretty much like a while true, and it just keeps going through this list of work, and the fact that it gets to the basics, it's so basic from the core, and it makes such a powerful paradigm possible is just amazing.
55:26 Michael Kennedy: Yeah, that's a really interesting point about how that simple paradigm really opens things up, and even like you said, simplifies, removes a whole class of bugs for you guys. Awesome, so final call to action, people are interested in this, what do they do?
55:42 Paulus Schoutsen: Get a Raspberry Pi, get hass.io, and just get up and running. And then there's some great documentation, we've got some great videos how to get started, and if you run into any trouble, make sure to stop by our Discord. It's a chat community, and it's where a lot people hang out, they're helping out a lot of people to get started, and discuss home automation.
56:04 Michael Kennedy: Alright, sounds great. Looking forward to playing around with it. Paulus, thanks for being on the show, it was great to chat with you and share your story.
56:11 Paulus Schoutsen: Yeah, it was awesome to be here, thank you.
56:14 Michael Kennedy: This has been another episode of Talk Python to Me. Today's guest was Paulus Schoutsen, and this episode has been brought to you by Rollbar and Talk Python Training. Rollbar takes the pain out of errors. They give you the context and insight you need to quickly locate and fix errors that might have gone unnoticed, until your users complain, of course. As Talk Python to Me listeners, track a ridiculous number of errors for free at Rollbar.com/Talk PythonToMe. Are you or a colleague trying to learn Python? Have you tried books and videos that just left you bored by covering topics point by point? Well check out my online course, Python Jumpstart by Building 10 Apps at talkpython.fm/course, to experience a more engaging way to learn Python. And if you're looking for something a little more advanced, try my Write Pythonic Code course at talkpython.fm/pythonic. Be sure to subscribe to the show. Open your favorite podcatcher and search for Python, we should be right at the top. You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct RSS feed at /rss on talkpython.fm. Our theme music is "Developers, Developers, Developers" by Corey Smith, who goes by Smixxx. Corey just recently started selling his tracks on iTunes, so I recommend you check it out at talkpython.fm/music. You can browse his tracks he has for sale on iTunes and listen to the full-length version of the theme song. This is your host, Michael Kennedy. Thanks so much for listening, I really appreciate it. Smixxx, let's get out of here.