/posts/touchbarproject/

Domoticz Touchbar Light Control

Apple Home and Domoticz

For this project I set out to control my existing HomeKit enabled accessories using the TouchBar of my MacBook…

By using another Home Automation controller with a HTTP API, I could build a Cocoa application that utilises the TouchBar to control the lights around my home.

This resulted in a very convenient and visually pleasing way of playing with my lights, and also helped highlight how simple the TouchBar libraries are to use, due to their many analogues to the existing Cocoa framework.

Initial Problems

For what I assume to be a variety of security concerns, the HomeKit database is only accessible to tvOS, iOS and the HomePod. This means Apple does not provide a ‘Home’ application for OSX, nor does the OSX Siri client allow Home control. Because of this, one cannot use their Mac to control their HomeKit enabled accessories.

Enter Domoticz

Domoticz is an open source home automation server with hundreds of available device integrations. Most HomeKit enabled devices (such as the Ikea Tradfri smart bulbs) can be controlled via their server which exposes the state using a HTTP API.

Provided the HomeKit accessories relay broadcast their state to the HomeKit server and the Domoticz Server when it is changed by either platform, the two platforms should run seamlessly side by side.

Enter NSTouchBar

The TouchBar provides extensions to a number of familiar OSX interface components such as sliders, buttons and text.

As expected Apple has strict interface guidelines for the TouchBar which are enforced through SDK limitations and what would appear to be some runtime validation running conccurrent with the application (more on this later).

Combining the two

The Domoticz API exposes the ‘Devices’ added to the system through a list of JSON objects, and allows the state of these devices to be changed through dedicated URL paths for each device type (standard bulbs, RGB bulbs etc, momentary switches etc).

This information is easily unmarshalled into Swift dictionaries and can then be used to populate scrolling lists of home accessories in a TouchBar object.

Through the use of a PopOver, each device can be tapped, opening up a TouchBar analog of a popup which contains UI widgets representing and allowing the mutation of the smart devices state via the Domoticz HTTP API.

The above image shows the controls for a simple light which has a brightness and toggle switch.

The above image shows the control options for an RGB bulb, with additional brightness control.

Limitations

Due to TouchBar limitations, the pop up menus have a maximum depth of 1. This creates some unfortunate side effects when trying to use some Apple provided widgets. For example, a colour One example of such is that when I attempted to add a colour picker to a devices pop up menu, it would open and then immediately close, as the widget itself uses a PopOver to display the colour choices. This means I could not use the widget in my application.

Furthermore, Apple requires the application to be in focus for its TouchBar to be presented, and hence I had to add shortcuts to bring my application to the foreground. Some privileged applications, such as Spotify, are not subject to this restriction.