Leveling Up in Python

Leveling Up in Python

I started learning Python several years ago when I powered on my first Raspberry Pi. Between reading books from the library, wearing out the Google search bar and watching online courses, I got to where I could read and write Python with some fluency. Until recently, the extent of my Python programming was a script here or there to do a task with no regard for how it behaved if things weren’t perfect. I am not a programmer by trade, and, other than a brief flurry of Reddit karma for my CryptoRC project in 2017, all of my grand software ideas have never made it much further than myself and a friend or two. But as I poured hours into improving the integration between my thermostat and Home Assistant, I decided I should help others out along the way. Since this meant sharing my code with the world, it was time to level up my Python skills. To give me an incentive to up my game, I made it a goal to take my integration from custom to official.

As I worked to fix bugs that popped up and to make the integration more complete, each new commit was pushed to GitHub. I read through a lot of Python code during this time, and I began to get a better feel for the pythonic way of doing things. About the time I had decided I wanted to make this an official part of Home Assistant, I got a surprise from GitHub in the form of an email letting me know someone had opened an issue against my repository. Woohoo! Someone had found my code and cared enough to give feedback. The issue was that I had left an uncommented print statement in my code from some debugging I had been doing. I thanked him for bringing it to my attention and said I would find a more programmatic way of turning off and on debug messages. He suggested that I check out the built in Python logging module that HA uses. Cool, a specific item to level up on.

As I learned how the logging module worked, I started thinking about reporting more than just debug messages. What about passing back errors and warnings instead of just crashing? This sent me down a tangent, and I learned some more on how to properly handle exceptions in Python. Soon my code was handling non-critical errors by logging a warning, and critical errors by logging an error and preventing further execution so as not to cause cascading problems. My code was looking more and more like a proper Python module. HA requires that the code that interacts with outside services be packaged and available from PyPI, the Python Package Index. With exception handling and logging under my belt, I started down the road of rolling my module into a properly packaged Python project.

One of my high school teachers drilled in to me that if something is worth doing, it is worth doing well. So while this tutorial taught me the basics of how to build a project and deploy it to PyPI, I started reading other examples and directions until I understood more than just the first layer of this subject. Did you know that even if you delete a release or the entire project from PyPI, a file name can never be repeated? That bit of knowledge took trying to push a release with the same version number several times and thinking I was messing up a simple command before I found out what was wrong. Fortunately for the world, there is a test instance of PyPI where you can test deployments without chewing through the production namespace while you learn.

After deploying several versions of my code to test PyPI, installing it locally using PIP, and verifying I could import the module and reference it from other code, it was time to do it for real. I started getting nervous at this point. I was about to publish code I had written to the globally accessible index for anyone to see and use. Maybe I should test it one more time. 🙂 I finally got over my nerves, bumped the version to 0.2.0 and published myicomfort to PyPI! It is really cool to see your own code being installed the same way ‘real’ Python projects are installed.

With that big hurdle now past me, I turn my attention to making the HA component code an acceptable candidate for inclusion into the official Home Assistant code base. Unlike publishing a project to PyPI, getting code incorporated into someone else’s project comes with requirements for styling and behavior and tests to prove you met them. So, I am setting up the HA dev environment on my laptop and will begin working to check off each item.

I have never contributed to an open source software project before, and it certainly wasn’t something I envisioned I would be doing while I was in college pursuing an electrical engineering degree. But I am having fun on this journey and look forward to adding the virtual badge to my collection. Have you contributed to an OSS project? Do you have any tips for me? Let me know in the comments.

Jacob

2 thoughts on “Leveling Up in Python

  1. I have been looking to start on this project for the exact same reason. I have a iComfort tstat that I would love to integrate without using Smart Things as a gateway. How can I help?

    1. Jason – Having someone else run my code and provide feedback would be great. The version I am running in my house is on Github (https://github.com/thevoltagesource/LennoxiComfort). Let me know if you have any issues getting it running. I have a few more tweaks that I need to implement to make the code fit the HA model. Once those are in place it is just a matter of me working thru the process of a pull request with Home Assistant. Given that this is my first attempt I am sure it will take a few iterations.

Leave a Reply

Your email address will not be published. Required fields are marked *