GithubHelp home page GithubHelp logo

hc's Introduction

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.

hc's People

Contributors

brutella avatar chris-rock avatar cloudkucooland avatar comawill avatar dgageot avatar eaigner avatar fernferret avatar gonzolino avatar hairyhenderson avatar hubertat avatar jjcinaz avatar josh avatar mxlje avatar pmaene avatar simonwaldherr avatar timoschilling avatar tomtaylor avatar yakutozcan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hc's Issues

Multiple devices

How would one add multiple devices at once?

When I run hap.NewIPTransport("00102003", sw.Accessory) for every device, I get:

2015/07/15 21:49:18 [ERR] bonjour: Failed to bind to udp6 port: listen udp6 :5353: bind: address already in use

How to build own accessory?

I am trying to extend the thermometer to support additional sensors such as a carbon dioxide sensor and an humidity sensor. It is not really clear to me how to integrate those additional services. Might be due to my very limited golang experience. Could you provide some example on how to add an additional service to an accessory?

Q: Is it possible to query the state of other accessories through hc?

If I have other HomeKit enabled devices, would it be possible to query the state of those, and build new aggregated services from those devices?

Say I want to create a thermostat using the the already HomeKit connected temperature sensor in Elgato Eve Room, would that be possible via HomeKit or would I have to get data from the Bluetooth transfer directly?

High latency client can crash the bridge

If the client is under enough packet loss or experiencing high enough latency, the following happens to me:

2015/07/18 19:28:44 [ERRO] Decryption failed: read tcp 192.168.1.104:65302: operation timed out
2015/07/18 19:28:44 [ERRO] Decryption failed: use of closed network connection
2015/07/18 19:28:44 [INFO] Close connection and remove session

The server is then dead and must be halted and resumed. If this is a fatal error, it should be handled as such and the program should terminate. If not, the connection should be retried. I haven't gotten deep enough into netio to see where to handle this better, and this is my first real foray into reading someone else's go.

Thermostat example?

Hi - this looks like a great project, thanks for working on it so far. I'd really like to expose my Raspberry Pi driven thermostat using HomeKit, and I'm not quite sure how to get started.

I can see the example with the switch, but the thermostat structs don't seem to have the same callback functions - I can't see how to bind up something that will trigger when the user changes the target temperature.

Am I missing something, or has this not been implemented yet?

Fix invalid http host header field value

iOS uses the mDNS service name as host header field value – whitespaces are encoded as \032.
Go1.6 validates the host header field value as reported in #48 and will fail on any backslash character ("").

There is now a radar for Apple to fix that.

Until then we should remove whitespaces from mDNS services names.

As a developer I want to identify my accessory

The current implementation of POST /identify does nothing other than responding with HTTP status code 204 (no content).

Things to do

  • Respond with HTTP status code 400 (bad request) when accessory is already paired with a device
  • Respond with HTTP status code 204 (no content) when accessory is not paired. Provide method for developers to let the accessory identify itself.

iPhone on iOS 9 can't reconnect to bridge

iOS 9 connects and findes products in home app, after connection is lost (wifi | flight mode toggle on/off) and products remains disconnected..

Philips hue bridge connects right away (so no ios9/ iPhone problem)

How to update hc?

Could you provide some documentation on how to update an existing hc installation?
I am a bit puzzled, since the installation guide only clones the hklight repo. How does the hc code get updated to the latest version of the repo?
Thanks!

bonjour errors

Hi,
I built this for OSX but when I run it I get..
[Maelstrom 16:45] ~/projects/go/src/hklight >./hklightd
2016/12/16 16:45:26 [INFO] Accessory IP is 192.168.1.29
2016/12/16 16:45:26 [ERR] bonjour: Failed to unpack packet: dns: failed to unpack truncated message
2016/12/16 16:45:26 [ERR] bonjour: Failed to handle query: dns: failed to unpack truncated message
2016/12/16 16:45:26 [ERR] bonjour: Failed to unpack packet: dns: failed to unpack truncated message
2016/12/16 16:45:26 [ERR] bonjour: Failed to handle query: dns: failed to unpack truncated message
etc..

I ran wireshark looking for traffic but couldn't find anything (so.. maybe I looked in the wrong place :)

Help: How to integrate temperature sensor

I'm the guy on twitter who asked for the netamo integration :)
I'm trying to integrate a temperature sensor. I can access the temperature via simple http requests. Can I adapt the thermometer class for this?
https://github.com/brutella/hc/blob/master/model/accessory/thermometer.go

I tried adding the temperature sensor to my devices like follows: 
thermometer_info := model.Info{
    Name:         "Netatmo",
    SerialNumber: "001",
    Manufacturer: "Netatmo",
    Model:        "Thermostaty",
    }

    model_thermometer := accessory.NewTemperatureSensor(thermometer_info, 10, 0, 100, 1)

But this will obviously give me 10°C as the temperature. Is there a func called when I access the sensor device via homekit? I would put the http call there. Thanks!

Additions/modifications

Hi Matthias,

First, this is not an issue as such :-)

Second, great work you have done here. Now then, I'm working on an "HomeKit to MQTT Server" that allow bidirectional communication between HomeKit (using your HomeControl) and devices communicating via the MQTT protocol. So far I've made a new transport based on
'ip_transport.go' and a few additions/modifications here and there (including adding new accessories).

So the question is: are you willing to accept PRs (if the code is ok of course)?

If I commit/sync to github you can have a look - any interest?

/michael

Prevent invalid characteristic values

Characteristics have a specific type (string, float, ...) and may have a min and max. We should prevent invalid values; e.g. 24 for values which have a max of 20.

Does hklight work on OSX?

Hi,

I just wanted to dip my toes into this project by playing with hklight. But after starting it my iPhone does not see the accessory. Before I investigate further, is this known?

regards

MFi Compatibility

How can you enable the MFi compatibility? Is there stub functions to fill?

Control of the IP Address that HC binds to

It would be nice to be able to control which ip address HC binds to instead of it just choosing one as it does now. I run HC on my linux gateway/firewall system and it was grabbing the outside interface address when I prefer it to keep its traffic on my internal net.

I've got a diff for the GetFirstLocalIPAddress func in mdns.go that first attempts to use the hostname ip address before falling back to just blindly grabbing the first available IP address.

An even better solution would be to be able to specify the address to bind to, perhaps as an additional variable in hap.Config? I don't "know" go but have figured it out enough for this diff. If hap.Config would be an approved method, I could work on implementing that.

Resetting State Information

Is there a simple way to reset the state information. For example reset all bulbs to off. Or would just restarting the program be enough to do this?

bonjour fail... help

Hi, folks,

i try to start the example from the tutorial of brutella but i receive an error:

antonkluev.pi ~/go/src/hklight $ go run hklightd.go
2015/10/26 21:18:07 [INFO] Accessory IP is 192.168.1.8
2015/10/26 21:18:07 [ERR] bonjour: Failed to bind to udp6 port: listen udp6 :5353: address family not supported by protocol

the homekit apps don't see my virtual accessoire..
can anyone help me?

and i have one more question: can i use my raspberry pi to control multiple accessoires?
thx guys!

Flatten project structure

Most sub-packages (if not all) can be consolidated into one package (preferably at the top level). For instance packages like util are not really required and can just be private package methods.

Bonjour: Failed to handle query

When I start up my app that runs two accessories, I get the following right at startup:

2016/08/16 22:26:09 [ERR] bonjour: Failed to handle query:
  write udp4 0.0.0.0:5353->192.168.0.16:5353:
    sendto: operation not permitted

I am running on Ubuntu 16.04 and I have stopped avahi-daemon to make sure port 5353 is available.

Is the problem here that it is trying to send a UDP packet from 0.0.0.0 ?

New accessories

Thanks for your work!
You plan to add new accessories?
Very interested in the camera and doorbell.

Start hc after boot on Debian

Is there an easy way to have hc started after a reboot? I can use the following script to start hc from the command line:

export GOPATH=~/go/
cd ~/go/src/hklight/
go run ~/go/src/hklight/home.go

But I can't execute it via cron after a reboot. Any suggestions on how to start hc after a reboot?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.