Skip to content
/ hc Public
forked from brutella/hc

HomeControl is an implementation of the HomeKit Accessory Protocol (HAP) in Go.

License

Notifications You must be signed in to change notification settings

JJCinAZ/hc

 
 

Repository files navigation

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.

About

HomeControl is an implementation of the HomeKit Accessory Protocol (HAP) in Go.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%