Swiftpack.co - Package - Bouke/HAP

Homekit Accessory Protocol, implemented in Swift

The goal of this package is to provide a complete implementation of the Homekit Accessory Protocol, enabling you to build your DIY accessories, and connect your non-HAP devices.

Build Status Maintainability Rating


  • Persistent configuration across reboots.
  • Homekit pairing by scanning QR code (shipped in iOS 11).
  • Speakers and Remote Control accessories (shipped in iOS 12 / macOS Mojave).
  • Television accessories (shipped in iOS 12.2 / macOS 10.14.3).
  • Extensibility through custom services and characteristics.
  • Runs on Linux / Raspbian (Raspberry Pi).


Remember that this is not a commercial product, but the result of free labor.

  • If you need help using this library, open an issue here on GitHub. The more detail the better!
  • If you found a bug, open an issue here on GitHub. The more detail the better!
  • If you want to contribute, submit a pull request.

Getting Started


Install libsodium (used for Curve25519 and Ed25519):

brew install libsodium

And then build and run the project itself, specifying a release build for good performance:

swift run -c release hap-server


Install dependencies:

sudo apt install openssl libssl-dev libsodium-dev libcurl4-openssl-dev libavahi-compat-libdnssd-dev

Make sure you have libsodium 1.0.9 or above. Ubuntu 16.10 or later suffices. Otherwise you have to compile and install libsodium from source:

wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.12.tar.gz
tar xzf libsodium-1.0.12.tar.gz
cd libsodium-1.0.12
make && make check
sudo make install
sudo ldconfig

And then build the project itself, specifying a release build for good performance:

swift run -c release hap-server

Raspberry Pi (Raspbian Stretch)

There are currently no official binaries from swift.org targetting ARM / Raspbian, however there's an active community working on Swift on ARM. You can install binaries from their repository:

curl -s https://packagecloud.io/install/repositories/swift-arm/release/script.deb.sh | sudo bash
sudo apt install swift5


Modify Sources/hap-server/main.swift to include your own accessories, or import the HAP library into your own project.

On Mac OS, you can debug using XCode by running the command swift package generate-xcodeproj and the opening the resulting HAP.xcodeproj project. Select and run the hap-server target.


The following code snippet how you would model a fictious accessory representing a mobile power bank.

class PowerBankAccessory: Accessory {
    let service = PowerBankService()
    init(info: Service.Info) {
        super.init(info: info, type: .outlet, services: [service])
class PowerBankService: Service {
    public let on = GenericCharacteristic<Bool>(
        type: .on,
        value: false)
    public let inUse = GenericCharacteristic<Bool>(
        type: .outletInUse,
        value: true,
        permissions: [.read, .events])
    public let batteryLevel = GenericCharacteristic<Double>(
        type: .batteryLevel,
        value: 100,
        permissions: [.read, .events])

    init() {
        super.init(type: .outlet, characteristics: [

Object-Oriented Design

A high-level overview of the objects involved are shown in the diagram below. The terminology of HAP (Device, Accessory, Service, Characteristic) is followed for ease of understanding.

                      | NetService |
                             | delegate
   +--------+ 1     0…1 +--------+ *   * +---------------------+
   | Device |-----------| Server |-------| Controller (iPhone) |
   +--------+           +--------+       +---------------------+
        | 1                           * /
        | *                           /
  +-----------+                     /
  | Accessory |                   /
  +-----------+                 /
        | 1                   / > read, events
        | *                 / < write, subscribe
   +---------+            /
   | Service |          /
   +---------+        /
        | 1         /
        | *     * /
| Characteristic |


Running tests

Certain tests involve crypto, which can be a bit slow in debug builds. Best to run the tests with a release build, like this:

swift test -c release -Xswiftc -enable-testing


This library was written by Bouke Haarsma and contributors.


Stars: 286


- 2020-01-27 21:38:17


  • HAP definitions from macOS Catalina.


  • Swift 5 required.
  • SwiftNIO upgraded to v2.
  • Logging through SwiftLog instead of Evergreen.

- 2019-08-31 09:10:18


  • Additional accessory and service types.
  • Support for Raspberry Pi / Raspbian Stretch.
  • Services / Characteristics are now generated from Apple's Homekit framework on macOS. This means that those definitions are more easily kept up-to-date. Contribution by Guy Brooker.
  • Television accessory support. Contribution by Guy Brooker.
  • Swift 5 support.


  • Network stack rewritten with SwiftNIO.
  • Naming of some service / characteristic / enum types.
  • Characteristics with read permission (Paired Read) require a value, no value is no longer valid.
  • Characteristic bounds no longer trap when out of bound values are set programmatically, but will clip the value instead. This resolves an issue where setting a value to the minimum value would trap in some situations.


  • Allow bridges with spaces in name.


  • Swift 4 support.

Swift 4 and more - 2017-11-19 19:07:11

  • Swift 4
  • Improved HAP specification conformity (notification coalescence, multiple status return codes)
  • Additional builtin accessory and service types
  • Resolved issue with pairings not being removed