Automation of Gixie Clock Mini

I purchased a “Gixie Clock Mini” from AliExpress and wanted to automate dimmer and settings changes via HA. The clock connects to WiFi (for NTP and LAN mobile app control). Some sniffing of the traffic to the clock from the mobile app reveals a websocket connection and text commands.

To get HA to open a websocket connection and send commands, I elected to us HACS Pyscript.

Here is the script:

from websockets.sync.client import connect
import json

@service
def clock_update(state=None, brightness=None, rgb=None, mode=None):
    """yaml
name: Update Clock Settings 
fields:
  mode:
    example: 0
    selector:
      number:
        min: 0
        max: 7
  state:
    example: true
    selector:
      boolean:
  brightness:
    example: 255
    selector:
      number:
        min: 0
        max: 255
  rgb:
    example: [255,255,255]
    selector:
      color_rgb:
"""
    if brightness is not None:
        state = True
    
    with connect("ws://192.168.0.218:81") as websocket:
        if mode is not None:
            websocket.send(json.dumps(
                {
                    "cmdType": 1,
                    "cmdNum": 211,
                    "cmdCtx": {
                        "value": mode
                    }
                }
            ))
        if state is not None:
            websocket.send(json.dumps(
                {
                    "cmdType": 1,
                    "cmdNum": 15,
                    "cmdCtx": {
                        "value": int(state == True)
                    }
                }
            ))
        if brightness is not None:
            websocket.send(json.dumps(
                {
                    "cmdType": 1,
                    "cmdNum": 14,
                    "cmdCtx": {
                        "value": brightness
                    }
                }
            ))
        if rgb is not None:
            websocket.send(json.dumps(
                {
                    "cmdType": 1,
                    "cmdNum": 9,
                    "cmdCtx": [
                        {"red": rgb[0], "green": rgb[1], "blue": rgb[2]},
                        {"red": rgb[0], "green": rgb[1], "blue": rgb[2]},
                        {"red": rgb[0], "green": rgb[1], "blue": rgb[2]},
                        {"red": rgb[0], "green": rgb[1], "blue": rgb[2]}
                    ]
                }
            ))

I added a DHCP reservation to my router to ensure the IP address does not change.