HomeControl is an implementation of the HomeKit Accessory Protocol (HAP) in Go.
Go
Clone or download
brutella Merge pull request #120 from JJCinAZ/master
accessory.Switch was using wrong type?
Latest commit 5b6df48 May 7, 2018
Permalink
Failed to load latest commit information.
_example Update example and readme on how to stop an ip transport #95 Oct 23, 2017
accessory Fixed error where accessory.Switch was creating a accessory of type T… May 6, 2018
characteristic Import HomeKit types from Accessory Simulator 3.0 (131) Feb 6, 2018
cmd Generate markdown tables to summarize HomeKit services and characteri… Feb 9, 2018
crypto Switched to Go’s chacha20poly1305 implementation Feb 28, 2017
db Add method to get all entities from the database Oct 12, 2015
event Send event when device is paired/unpaired and update mDNS service rea… Oct 12, 2015
gen Generate markdown tables to summarize HomeKit services and characteri… Feb 9, 2018
hap Add missing file Feb 10, 2018
log Remove dependency to github.com/brutella/log Oct 6, 2016
service Generate markdown tables to summarize HomeKit services and characteri… Feb 9, 2018
util Move files from netio into hap package and some files from hap into r… Apr 28, 2016
vendor/github.com Merge commit '9b327fcebf3d9a1da507de6aed80453bed3dd95a' Nov 30, 2017
.gitignore Add missing data package Feb 25, 2015
.travis.yml Squashed 'vendor/github.com/brutella/dnssd/' content from commit ca9b207 Oct 6, 2017
CREDITS Remove reference to bonjour library Oct 9, 2017
LICENSE Change license from non-commercial to Apache License 2.0 Oct 6, 2017
README.md Generate markdown tables to summarize HomeKit services and characteri… Feb 9, 2018
config.go Fix accessory category identifier in mDNS txt records Feb 6, 2018
config_test.go Fix accessory category identifier in mDNS txt records Feb 6, 2018
doc.go Update go doc Apr 28, 2016
ip_transport.go Use Go's context cancelation API in favor or custom stop method Sep 29, 2017
mdns.go Use github.com/brutella/dnssd for publishing mDNS service Sep 27, 2017
password.go Adds check for invalid pins Jun 8, 2017
password_test.go Adds check for invalid pins Jun 8, 2017
termination.go Move files from netio into hap package and some files from hap into r… Apr 28, 2016
transport.go Use Go's context cancelation API in favor or custom stop method Sep 29, 2017

README.md

HomeControl

Build Status

HomeControl is an implementation of the HomeKit Accessory Protocol (HAP) to create your own HomeKit accessory in Go. HomeKit is a set of protocols and libraries to access devices for Home Automation. The actual protocol documentation is only available to MFi members. A non-commercial version of the documentation is now available on the HomeKit developer website.

You can use this library to make existing Home Automation devices HomeKit compatible. I've already developed the following HomeKit bridges with in:

HomeKit on iOS

HomeKit is fully integrated since iOS 8. Developers can use the HomeKit framework to communicate with HomeKit using high-level APIs. I've developed the Home app (for iPhone, iPad, Apple Watch) to control HomeKit accessories. If you purchase Home on the App Store, you not only support my work but also get an awesome iOS app. Thank you.

Once you've setup HomeKit, you can use Siri to interact with your accessories using voice command (Hey Siri, turn off the lights in the living room).

Features

Getting Started

  1. Install Go

  2. Setup Go workspace

  3. Create your own HomeKit bridge 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
     go run hklightd.go
    
  4. Pair with your HomeKit App of choice (e.g. Home)

API Example

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() {
	info := accessory.Info{
		Name: "Lamp",
	}
	acc := accessory.NewSwitch(info)
    
    config := hc.Config{Pin: "00102003"}
	t, err := hc.NewIPTransport(config, acc.Accessory)
	if err != nil {
		log.Panic(err)
	}
    
    hc.OnTermination(func(){
        <-t.Stop()
    })
    
	t.Start()
}

You should change some default values for your own needs

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

Callbacks

You get a callback when the power state of a switch changed by a client.

acc.Switch.On.OnValueRemoteUpdate(func(on bool) {
	if on == true {
		log.Println("Client changed switch to on")
	} else {
		log.Println("Client changed switch to off")
	}
})

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

acc.Switch.On.SetValue(true)

A complete example is available in _example/example.go.

Model

The HomeKit model hierarchy looks like this:

Accessory
|-- Accessory Info Service
|   |-- Identify Characteristic
|   |-- Manufacturer Characteristic
|   |-- Model Characteristic
|   |-- Name Characteristic
|   |-- Serial Characteristic
|   
|-- * Service
|   |-- * Characteristic

HomeKit accessories are container for services. Every accessory must provide the Accessory Information Service. Every service provides one or more characteristics (a characteristic might be the power state of an outlet). HomeKit has predefined service and characteristic types, which are supported by iOS. You can define your own service and characteristic types, but it's recommended to use predefined ones.

Dependencies

HomeControl uses vendor directories (vendor/) to integrate the following libraries

  • github.com/tadglines/go-pkgs/crypto/srp for SRP algorithm
  • github.com/agl/ed25519 for ed25519 signature
  • github.com/gosexy/to for type conversion
  • github.com/brutella/dnssd for DNS service discovery

Contact

Matthias Hochgatterer

Website: http://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.