One of my 2020 lockdown projects I’m extremely proud of is HA-meural – a custom integration for the Home Assistant home automation software that lets you control a NETGEAR Meural Canvas digital frame. It’s the result of a few months of work, lots of trial and error, and many hours lost to bad indents. But it’s also a journey that has been very fulfilling personally and has renewed my love for programming. And one that shows that building your own Home Assistant integration is much more doable than it might appear at first!
This year, during the first few months of lockdown, I impulse-bought a Meural Canvas digital art frame. Photography is my main creative outlet and it’s always frustrated me that I couldn’t display more of my photos in my own home, limited by available wall-space and my inability to choose which photos to print. The Canvas can display any image I want – any of my photos I’ve taken, but also famous art from a museum or cool digital art I’ve found online. It’s amazing and my favorite purchase of the year, easy. I tweeted about my new gadget, and kinda complained that I couldn’t control the frame with Home Assistant, the system that runs my smart home. One of my smart home dreams is inspired by this scene from Antritrust, where the house knows who is present and changes the art on the walls automatically to fit their tastes.
Home Assistant integration
So what you might not know about me – once upon a time I studied computer science. Not for very long, and I ultimately switched my major to marketing, but I really enjoyed programming at the time. After graduating I did not have many opportunities to practice this skill in my daily life as a marketer, and I ended up forgetting most of what I had learned.
Home Assistant’s founder, Paulus, is a friend of mine from those student days. When he saw my tweet he replied that it was actually really simple to build your own Home Assistant integration – especially as there was a public API available! Color me intrigued. After sending him the link to Meural’s REST API documentation and giving him my login and password to test with, he had a proof of concept version of the integration up and running within half an hour. From his home in America, he could now change which playlist was displayed on the Canvas in my home in the Netherlands, all using Home Assistant. Even more impressive – he did this without owning a Canvas, or even having seen one in real life. Awesome!
And that’s when he threw the ball back to my court. The groundwork was done, now I should start getting my own hands dirty and extend the integration with all the functionality I actually wanted. That seemed a bit of a challenge, as it had been 15 years since I had last coded and Python was not a language that I was familiar with. But hey… what else are you going to do during lockdown? Might as well try my hand at this.
That was half a year ago, back in May. Now, in November, I just released HA-meural version v0.2.0 which is available to all Home Assistant users via HACS (the Home Assistant Community Store, for easy installation of unofficial add-ons), and has a whole bunch of happy users who are controlling their Canvas frames using the integration. It lets you control your Meural directly from the Home Assistant interface, which means you can easily change the displayed playlist, go to the next or previous image, pause or resume the playback and turn the Canvas on and off. The interface also shows the title, artist and year along with a thumbnail of the currently displayed artwork.
You can also automate the Canvas using the full capabilities that Home Assistant offers. For example, Meural already offers a simple scheduler, but it’s limited to setting up specific times for wake, sleep and playlist events. In our Home Assistant setup we use the far more powerful Node-RED visual programming language from IBM to create automations. A simple flow turns our Canvas on at 8 in the morning and off at midnight, but it also takes into account if my girlfriend and I are actually home to appreciate the art. If we’re not at home, the Canvas remains off. If we come home between 8 in the morning and midnight, the Canvas turns on. And if we leave the house, the Canvas turns off again. Pretty nice – and this is still a very simple automation, considering what Home Assistant can do.
If you want to copy the above flow into your own Node-RED installation, you can import the following gist:
Integrating the Canvas into Home Assistant has also unlocked a functionality that is often requested by Meural users. The Canvas only supports Amazon Alexa out-of-the-box, so you’re shit out of luck if you have a Google Home. But as Home Assistant can expose devices to Google for voice control, you can control your Meural Canvas using Google Home through HA-meural. An easy fix for something that has been frustrating Canvas owners all over the world.
Learning to code – again
Making this has been a shitload of fun. I had forgotten just how enjoyable it is to program something and then see it affect the world around me. For the past few months I’ve been spending my evenings, here and there, just messing with the integration. It’s awesome to spend a few hours figuring out these little logical puzzles in the code and then get rewarded by seeing the Canvas act just a little bit smarter. And it’s been worthwhile to pick this skill up outside of this project too – much has changed over the years and some ability to code in Python has become a valuable skill inside marketing too.
And the crazy thing is – it wasn’t nearly as hard as I expected it to be. I do realize I had some unfair advantages – the first version of the code wasn’t written by me, after all, I extended an integration that could already log into the Meural server and retrieve the basic information required to operate. And I do have some prior programming experience, even if I don’t remember much from my time as a student. Still, having gone through a few months of figuring things out, I’m convinced that this is not as hard as it looks from the outside.
After all, you’re not starting from scratch. There’s official Home Assistant developer documentation. The code of every other add-on, official and unofficial, is out there on Github for you to learn from. There’s tons of general Python tutorials and guides online. There’s Stack Overflow if you need help with a specific problem. And of course there are the Home Assistant forums. I started discussing the integration there and mentioned that I was having trouble coding the upload of images to the Canvas. A key functionality we needed to really extend the use of the Canvas for smart homes. Within days @thomasvs contributed the code to upload an image to the Canvas using the preview functionality, allowing us to display any arbitrary image on the device!
Python is not a language I’m very familiar with. I don’t have the experience of proper code design patterns an experienced dev would have. So all in all, HA-meural‘s code probably reads like a freshman ‘intro to programming’ homework assignment. But at the end of the day – it works. Various iterations of this code have been running in my home for the past half year now and it’s been good at doing what it’s supposed to do. And when you get down to it, that’s good enough for me. If that’s good enough for you, believe me: if I can do this, so can you.