View Source HAP (HAP v0.4.9)

HAP is an implementation of the HomeKit Accessory Protocol specification. It allows for the creation of Elixir powered HomeKit accessories which can be controlled from a user's iOS device in a similar manner to commercially available HomeKit accessories such as light bulbs, window coverings and other smart home accessories.

The HomeKit Data Model

The data model of the HomeKit Accessory Protocol is represented as a tree structure. At the top level, a single HAP instance represents an Accessory Server. An accessory server hosts one or more Accessory Objects. Each accessory object represents a single, discrete physical accessory. In the case of directly connected devices, an accessory server typically hosts a single accessory object which represents device itself, whereas bridges will have one accessory object for each discrete physical object which they bridge to. Within HAP, an accessory server is represented by a HAP.AccessoryServer struct, and an accessory by the HAP.Accessory struct.

Each accessory object contains exposes a set of Services, each of which represents a unit of functionality. As an example, a HomeKit accessory server which represented a ceiling fan with a light would contain one accessory object called 'Ceiling Fan', which would contain two services each representing the light and the fan. In addition to user-visible services, each accessory exposes an Accessory Information Service which contains information about the service's name, manufacturer, serial number and other properties. Within HAP, a service is represented by a HAP.Service struct.

A service is made up of one or more Characteristics, each of which represents a specific aspect of the given service. For example, a light bulb service exposes an On Characteristic, which is a boolean value reflecting the current on or off state of the light. If it is a dimmable light, it may also expose a Brightness Characteristic. If it is a color changing light, it may also expose a Hue Characteristic. Within HAP, a characteristic is represented by a tuple of a HAP.CharacteristicDefinition and a value source.

Using HAP

HAP provides a high-level interface to the HomeKit Accessory Protocol, allowing an application to present any number of accessories to an iOS HomeKit controller. HAP is intended to be embedded within a host application which is responsible for providing the actual backing implementations of the various characteristics exposed via HomeKit. These are provided to HAP in the form of HAP.ValueStore implementations. For example, consider a Nerves application which exposes itself to HomeKit as a light bulb. Assume that the actual physical control of the light is controlled by GPIO pin 23. A typical configuration of HAP would look something like this:

accessory_server =
  %HAP.AccessoryServer{
    name: "My HAP Demo Device",
    model: "HAP Demo Device",
    identifier: "11:22:33:44:12:66",
    accessory_type: 5,
    accessories: [
      %HAP.Accessory{
        name: "My HAP Lightbulb",
        services: [
          %HAP.Services.LightBulb{on: {MyApp.Lightbulb, gpio_pin: 23}}
        ]
      }
    ]
  }

children = [{HAP, accessory_server}]

Supervisor.start_link(children, opts)

...

In this example, the application developer is responsible for creating a MyApp.Lightbulb module which implements the HAP.ValueStore behaviour. This module would be called by HAP whenever it needs to change or query the current state of the light. The extra options (gpio_pin: 23 in the above example) are conveyed to this module on every call, allowing a single value store implementation to service any number of characteristics or services.

HAP provides structs to represent the most common services, such as light bulbs, switches, and other common device types. HAP compiles these structs into generic HAP.Service structs when starting up, based on each source struct's implementation of the HAP.ServiceSource protocol. This allows for expressive definition of services by the application developer, while providing for less boilerplate within HAP itself. For users who wish to create additional device types not defined in HAP, users may define their accessories in terms of low-level HAP.Service and HAP.CharacteristicDefinition structs. For more information, consult the type definitions for HAP.AccessoryServer.t/0, HAP.Accessory.t/0, HAP.Service.t/0, HAP.Characteristic.t/0, and the HAP.CharacteristicDefinition behaviour.

Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for Supervisor.init/1.

Starts a HAP instance based on the passed config

Called by user applications whenever a characteristic value has changed. The change token is passed to HAP.ValueStore instances via the HAP.ValueStore.set_change_token/2 callback.

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Callback implementation for Supervisor.init/1.

@spec start_link(HAP.AccessoryServer.t()) :: Supervisor.on_start()

Starts a HAP instance based on the passed config

Link to this function

value_changed(change_token)

View Source
@spec value_changed(HAP.ValueStore.change_token()) :: :ok

Called by user applications whenever a characteristic value has changed. The change token is passed to HAP.ValueStore instances via the HAP.ValueStore.set_change_token/2 callback.