Complaining is not the solution!

How do you feel when someone unloads all of his problems and complaints on you? Not every uplifting and energizing, is it? The truth is, nobody likes to be around a complainer-except, perhaps, other…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




AppDaemon

Taking Home Assistant to the next level with Python — part 2

AppDaemon is a great way to take your smart home automations to the next level. In this series I will guide you through AppDaemon and how to use it to take your home automation to the next level.

In this part I’ll show you an app that turns on and off a workstation and how you can use unit tests to verify correct behaviour. I will not go into unit testing or framework specifics too much and not all unit tests will be shown here.

I have a sit-stand desk with a power strip which powers my laptop, monitor and a few other electronic devices. When not in use, the whole setup uses around 10 watts which isn’t that much but watts add up so it’s nice to reduce standby usage if possible. I had a working automation.yaml just for this, but there were some situations where it would trigger so I decided to move it to AppDaemon.

For this project I had these requirements:

I also wanted to unit test the custom methods I needed for this AppDaemon app. Why unit test? Because I want to make sure my code works, but also I want to be able to iterate on my apps without worrying about breaking things and not knowing about it.

Especially apps that control ‘critical’ automations can have a lot of impact when they stop working.

This article will focus more on unit testing than actual AppDaemon functionality so I want to share my project structure with you, which will help you set up your project with unit tests too.

Next to the default apps directory I’ve created a tests directory, inside there are a few files that are important for running the unit tests:

As you can see, the initialize() method only calls other methods and splits the logic into smaller units. I consider it good practice to split your code into smaller chunks (units) of code. This will not only make it easier to read, it will also make it a lot easier to test.

I’ve created two partials for the startup and shutdown. By creating partials, I can ‘freeze’ the function call with arguments and reuse it. So whenever I want to refactor this code, I only have to modify the function call in one place instead of multiples places.

For some apps this might be overkill, but for other apps (like making sure your coffee machine is ready in the morning) it might not be. The most important thing is to know that it is possible to unit test your AppDaemon apps and I hope this article helped you to get an understanding on how to do it. In the final part, I’ll show you how to combine AppDaemon with an API client

Add a comment

Related posts:

Setidaknya

Langit bukan sekadar warna. “Setidaknya” is published by Karin Farinella.

How to find longest substring without repeating characters in a given string ?

This problem follows a sliding window mechanism and can be easily solved by looping through the characters using indexes sliding over them /** * @param {string} s * @return {number} */ var…

Election Composites

You may have noticed an “election composite” mentioned in the Data Selector or below the ratings diagram in the Analytics view . For example: This post explains what an election composite is and why…