HackGPT - Creating a Home Assistant Integration Using ChatGPT
About 10 or so years ago I switched my energy supplier and, as part of the new deal, I got given a smart heating thermostat called a Cosy made by a company called Geo (https://cosy.support.geotogether.com/en/support/solutions/articles/7000011187-introducing-cosy). It's a decent little bit of kit that can be retrofitted in series with your existing, old-school dumb thermostat. It simplifies things by giving you three pre-sets: Slumber (for a relatively cool environment to sleep in), Comfy (for when you just want to take the chill off) and Cosy (For full blown get a sweat on do nothing on the sofa type days). You can set a target temperature for each mode, along with schedules for you want each mode to automatically activate. It has a nice little app, a web interface if you'd prefer that, and can be controlled to some extent via Alexa.
But I've been a Home Assistant user for quite a few years now, and it's quite nice to be able to control things like heating using triggers other than just time, and it using it via HA would give it a bit more flexibility. For instance, the Cosy monitors temperature using neat a little portable control device that you can move to different places in your home. But, in my house at least, the temperature is not always the same in different places it could be cold downstairs but toasty in the bedrooms and , if that's where the Cosy device is, it'll stay cold downstairs. Sure, I could turn the heating on manually but where would be the fun in that? By sticking some cheap £2 AliExpress temperature sensors around the house, it would be possible to turn the heating on when any area gets cold, not just where the Cosy happens to be. Pair that up with some smart radiator thermostats and we can even heat just the area / room that is cold rather than the entire house. But there was a problem, there was no Home Assistant integration available for Cosy. Sad times.
I've built myself a few smart home devices in the past that have used MQTT to let Home Assistant to control them and it's worked pretty well, so a few years ago I created a 'Cosy Server' within a Docker container that combined an MQTT server that could receive commands from HA and send back status updates, with a NodeJS implementation of Puppeteer / headless browser which automated the actions of logging on to the Cosy web front end to scrape the information and switch modes etc. It was a bit of a clunky approach, but it worked OK for a while. It was also pretty limited: you could change modes and get the current mode but that was about it, and you couldn't set it to Hibernate. The HA front end controls were also a bit lacklustre, a few switches to change the mode was about the extent of it, but you could automate based on other triggers, so it sort of did the job. But it wasn't very robust. It didn't always work, sometimes MQTT messages didn't get sent or received or whatever, the server sometimes stopped working and I never got around to debugging it and then, eventually I moved from a HA installation on top of a full-blown Linux install to a HASS image and so stopped using my little server. I'd posted about my project on the HA forums, and shared the Github repository just in case it might be useful for anyone else. It turned out it was. About a year later someone who had seen my project got inspired to create something similar, rather than use MQTT, they wanted to use If This Then That (IFTT) to allow them to control their heating using outside triggers like emails etc. They went for a NuGet package for C# .NET instead of the Docker / Node route I had taken, but more importantly, they had done it using Cosy's non-public API which is what Cosy's web front end uses, rather than going down the headless browser / scraping route that I'd used. You can see their project here: https://github.com/dan-agilexp/cosyrest/blob/main/README.md
Using the API seemed like a much better idea and I'd always thought about creating a proper Home Assistant integration so that I'd have full control of everything (modes, target temperatures, hibernate on / off, etc.) and also have decent and pretty controls on the Home Assistant front end. I never got around to it until, three years later, which happens to be a few days ago, I did.
I don't code much these days, I don't really have the time, but I recently came across a YouTube video where a fella let ChatGPT take control of his smart home through HA's built in voice assistant capability and it looked pretty cool. I thought about maybe doing something similar, but felt like heating would be a key thing I'd like it to be able to control a bit more dynamically. I remembered the post from the guy I'd inspired, and in turn his work with the non-public Cosy API inspired me to have a look and see if I could create a Home Assistant integration.
The first step was to figure out how the API worked. To do this, I used Chrome's built in developer tools. You can access them by clicking the 3 dot menu button to the right of the address bar, hover over 'More Tools' then click developer tools. This opens a new section within your browser window, and gives you a bunch of tools that allow you to see how a website has been created. Along the top there are some tabs such as Elements, Console, etc. that let you inspect different components of the site. What I was interested in was the messages being sent to and from the site to control the Cosy system. Luckily, there's a tab for that too. It's called Network. I navigated the the login page for the Cosy web front-end, then pressed the record button in the toolbar just below the tabs. I then logged into Cosy, navigated around and changed settings etc. Once I'd done the main actions I wanted to include, I pressed the stop button next to the record button. Here's how things looked:
Ok we seem to just be going around in circles. Lets try a new approach. The file CosyLogin.py is a Python scriptthat I wrote which allows you to control a heating thermostat using their web api. You are a skilled python programmer, I would like to use this script to create a Home Assistant integration. Please can you write all of the filesI need to do this.
The requirements are as follows: All settings to install the integration must be accessible from the UI (ie. not using configuration.yaml) It must have a unique ID so that it can be managed in the UI once installed It must be able to get both the current temperature and the current preset mode It must be able to set the current preset mode, these are slumber, comfy and cosy It must be able to set the target temperature for each of the preset modes It must allow us to switch hiberate mode on and off If hibernate mode is on and the user tries to set one of the preset modes, it must first switch hibernate mode off It must be controllable via a lovelace UI card which shows the current temperature, the current preset mode, and
allows
you to set the target temperature for the current preset mode.
Sure, let's create a Home Assistant integration based on your CosyLogin.py script. We'll break this down into
several files:
manifest.json - Metadata about the integration.
config_flow.py - Handles the configuration flow for the integration. const.py - Constants used in the integration. api.py - Handles the communication with the Cosy API. climate.py - Defines the climate entity for Home Assistant. __init__.py - Initializes the integration.
Have you used ChatGPT to help with your coding? Has it allowed you to build something you would have had the skill or time to build otherwise? Let me know in the comments.