• Stars
    star
    1,741
  • Rank 25,660 (Top 0.6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

hc is a lightweight framework to develop HomeKit accessories in Go.

Not Maintained Anymore

This library is not maintained anymore. Please switch to the new hap library.


GoDoc Widget

hc is a lightweight framework to develop HomeKit accessories in Go. It abstracts the HomeKit Accessory Protocol (HAP) and makes it easy to work with services and characteristics.

hc handles the underlying communication between HomeKit accessories and clients. You can focus on implementing the business logic for your accessory, without having to worry about the protocol.

Here are some projects which use hc.

What is HomeKit?

HomeKit is a set of protocols and libraries from Apple. It is used by Apple's platforms to communicate with smart home appliances. A non-commercial version of the documentation is now available on the HomeKit developer website.

HomeKit is fully integrated into iOS since iOS 8. Developers can use HomeKit.framework to communicate with accessories using high-level APIs.

Home+.app

I've developed the Home+ app to control HomeKit accessories from iPhone, iPad, and Apple Watch. If you want to support hc, please purchase Home from the App Store. That would be awesome. ❤️

Checkout the official website.

Features

Getting Started

  1. Install and set up Go

  2. Create your own HomeKit accessory or clone an existing one (e.g. hklight)

     cd $GOPATH/src
    
     # Clone project
     git clone https://github.com/brutella/hklight && cd hklight
    
     # Run the project
     make run
    
  3. Pair with your HomeKit App of choice (e.g. Home)

Go Modules

hc supports Go module since v1.0.0. Make sure to set the environment variable GO111MODULE=on.

Example

See _example for a variety of examples.

Basic switch accessory

Create a simple on/off switch, which is accessible via IP and secured using the pin 00102003.

package main

import (
    "log"
    "github.com/brutella/hc"
    "github.com/brutella/hc/accessory"
)

func main() {
    // create an accessory
    info := accessory.Info{Name: "Lamp"}
    ac := accessory.NewSwitch(info)

    // configure the ip transport
    config := hc.Config{Pin: "00102003"}
    t, err := hc.NewIPTransport(config, ac.Accessory)
    if err != nil {
        log.Panic(err)
    }

    hc.OnTermination(func(){
        <-t.Stop()
    })

    t.Start()
}

You can define more specific accessory info, if you want.

info := accessory.Info{
    Name: "Lamp",
    SerialNumber: "051AC-23AAM1",
    Manufacturer: "Apple",
    Model: "AB",
    FirmwareRevision: "1.0.1",
}

Events

The library provides callback functions, which let you know when a clients updates a characteristic value. The following example shows how to get notified when the On characteristic value changes.

ac.Switch.On.OnValueRemoteUpdate(func(on bool) {
    if on == true {
        log.Println("Switch is on")
    } else {
        log.Println("Switch is off")
    }
})

When the switch is turned on "the analog way", you should set the state of the accessory.

ac.Switch.On.SetValue(true)

Multiple Accessories

When you create an IP transport, you can specify more than one accessory like this

bridge := accessory.NewBridge(...)
outlet := accessory.NewOutlet(...)
lightbulb := accessory.NewColoredLightbulb(...)

hc.NewIPTransport(config, bridge.Accessory, outlet.Accessory, lightbulb.Accessory)

By doing so, the bridge accessory will become a HomeKit bridge. The outlet and lightbulb are the bridged accessories.

When adding the accessories to HomeKit, iOS only shows the bridge accessory. Once the bridge was added, the other accessories appear automatically.

HomeKit requires that every accessory has a unique id, which must not change between system restarts. hc automatically assigns the ids for you based on the order in which the accessories are added to the bridge.

But I recommend that you specify the accessory id yourself, via the accessory.Config.ID field, like this.

bridge := accessory.NewBridge(accessory.Info{Name: "Bridge", ID: 1})
outlet := accessory.NewOutlet(accessory.Info{Name: "Outlet", ID: 2})
lightbulb := accessory.NewColoredLightbulb(accessory.Info{Name: "Light", ID: 3})

Accessory Architecture

HomeKit uses a hierarchical architecture to define accessories, services and characeristics. At the root level there is an accessory. Every accessory contains services. And every service contains characteristics.

For example a lightbulb accessory contains a lightbulb service. This service contains characteristics like on and brightness.

There are predefined accessories, services and characteristics available in HomeKit. Those types are defined in the packages accessory, service, characteristic.

Contact

Matthias Hochgatterer

Website: https://hochgatterer.me

Github: https://github.com/brutella

Twitter: https://twitter.com/brutella

License

hc is available under the Apache License 2.0 license. See the LICENSE file for more info.

More Repositories

1

chatheads

An implementation of Facebook's ChatHeads on iOS.
Objective-C
883
star
2

hkcam

Open-Source HomeKit Surveillance Camera
Go
881
star
3

hap

The HomeKit Accessory Protocol (hap) implemented in Go
Go
289
star
4

can

Connect to a CAN bus in Go
Go
198
star
5

dnssd

This library implements Multicast DNS (mDNS) and DNS-Based Service Discovery (DNS-SD) for Zero Configuration Networking in Go.
Go
174
star
6

hklifx

LIFX HomeKit bridge
Go
106
star
7

hkknx-public

hkknx is a HomeKit KNX bridge for KNX.
89
star
8

swift-csv

Fast and memory-efficient CSV library in Swift.
Swift
80
star
9

hklight

Example project of a HomeKit light bulb using HomeControl
Go
39
star
10

canopen

Communicate with other CANopen nodes in Go
Go
26
star
11

TextViewLineNumbers

Implementation of NSTextView with line numbers
Swift
25
star
12

simplediff-swift

simplediff is a simple diff algorithm implementation in Swift.
Swift
16
star
13

nsuserdefaults-macros

Some handy NSUserDefaults macros/functions
Objective-C
15
star
14

NSTreePopUpButton

An NSPopUpButton subclass which supports binding to NSTreeController
Swift
15
star
15

uvr

Communicate with an UVR1611 over the CAN bus
Go
10
star
16

hkuvr1611

UVR1611 HomeKit bridge
Go
9
star
17

Axt

A forgiving HTML SAX Parser for iOS
Objective-C
9
star
18

log-swift

Logging to console and file
Swift
9
star
19

gouvr

Library to decode the data bus of UVR 1611 and similar devices
Go
7
star
20

hkuvr

HomeKit bridge for UVR1611
Go
5
star
21

hksymo

HomeKit bridge for Fronius Symo inverter
Go
3
star
22

Journal

Easy file logging in Swift
Swift
3
star
23

XCollectionData

Swift
2
star
24

comment-generator

Ruby classes to generate comment templates from source code
Ruby
2
star
25

git-diff

Compare git branches, tags and commits in your favourite diff tool.
Shell
1
star
26

geizhals

Fetch prices from Geizhals
Go
1
star
27

C3Presentation

Slides about C++ programming built with deck.js and deckem
JavaScript
1
star