DEV Community

Atsushi Suzuki
Atsushi Suzuki

Posted on

I Automated My Air Conditioner with Kubernetes (kind + CronJob + SwitchBot)

Even though I’m an AWS Community Builder in the Containers category, I had honestly never touched Kubernetes before. I felt a bit guilty about that, so I recently decided to properly start learning it.

After studying for about a month for the CKA (Certified Kubernetes Administrator) exam, I became familiar with the basic Kubernetes resources and kubectl commands. At that point, I wanted to actually try using Kubernetes resources end-to-end in a real project.

Around the same time, I realized that the SwitchBot Hub Mini I had bought during Black Friday might be perfect for a small hands-on experiment, so I decided to play around with it.

SwitchBot Hub Mini

IMG\_9424 (2).jpg

As a first step, I built a Kubernetes cluster locally on my Mac using kind, and created a CronJob that turns on the air conditioner in my bedroom every day at 7:00 PM(JST).

The total implementation time was about 30 minutes.

SwitchBot Hub Mini Setup

Prerequisites

  • The SwitchBot app is installed
  • A SwitchBot account has been created
  • The air conditioner remote has already been registered in the app

Getting the Token / Secret Key / Device ID

Open Profile > Preferences > About in the SwitchBot app.

IMG\_9425 (2).png

Tap App Version repeatedly, and Developer Options will appear.

image.png

Make a note of your Token and Secret Key.

IMG\_9427 (2).png

Using the obtained Token, call the SwitchBot API to retrieve the list of registered devices, and make a note of the Device ID (deviceId) for your air conditioner.

curl "https://api.switch-bot.com/v1.0/devices" -H "Authorization: ${token}" | jq .

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   350  100   350    0     0   1523      0 --:--:-- --:--:-- --:--:--  1528
{
  "statusCode": 100,
  "body": {
    "deviceList": [
      {
        "deviceId": "xxxxxxxxxx",
        "deviceName": "Hub Mini E7",
        "deviceType": "Hub Mini",
        "enableCloudService": true,
        "hubDeviceId": "000000000000"
      }
    ],
    "infraredRemoteList": [
      {
        "deviceId": "xx-xxxxxxxx-xxxx",
        "deviceName": "Air Conditioner",
        "remoteType": "Air Conditioner",
        "hubDeviceId": "xxxxxxxxxx"
      }
    ]
  },
  "message": "success"
}
Enter fullscreen mode Exit fullscreen mode

Setting Up the Kubernetes Environment (kind)

This time, I used kind to create a local Kubernetes cluster on my Mac.
As long as you have Docker Desktop installed, you’re good to go.

kind

The cluster can be created with just two commands.

brew install kind
kind create cluster
Enter fullscreen mode Exit fullscreen mode

If the node status is Ready, everything is set.

kubectl get nodes

NAME                 STATUS   ROLES           AGE    VERSION
kind-control-plane   Ready    control-plane   3m9s   v1.35.0
Enter fullscreen mode Exit fullscreen mode

Application Structure

Repository

Since the codebase is quite small, it’s probably fastest to just look at the repository directly.

GitHub

Directory Structure

switchbot-k8s-cronjob/
├── README.md
├── Makefile
├── docker/
│   └── Dockerfile
├── app/
│   ├── switchbot.py # Simply calls the SwitchBot API
│   └── requirements.txt
├── k8s/
│   ├── cronjob.yaml
│   ├── secret.example.yaml # Tokens are managed via Kubernetes Secrets
│   └── namespace.yaml
└── .gitignore
Enter fullscreen mode Exit fullscreen mode

What I Got Stuck On

At first, the CronJob kept failing with ImagePullBackOff.

In a kind cluster, Docker images built locally cannot be pulled automatically.
Because of this, it was necessary to set imagePullPolicy: Never.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: switchbot-cron
  namespace: switchbot
spec:
  schedule: '0 19 * * *' # every day at 19:00
  timeZone: 'Asia/Tokyo' # JST
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: switchbot
              image: switchbot-job
              imagePullPolicy: Never # NOTE: use local image
              envFrom:
                - secretRef:
                    name: switchbot-secret
          restartPolicy: Never
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Even though CronJob itself is outside the CKA exam scope, it was great to see that with just the knowledge gained from CKA study, I could build something that actually works in about 30 minutes.

There are plenty of other things I could try with this setup, so I’m planning to experiment a bit more during the New Year holidays.

Top comments (0)