star_icon
blog-img

Author: Sayali Gujrathi

Posted On Sep 26, 2014   |   6 Mins Read

With the launch of iOS 8, Apple has set its sight on the home automation market by introducing the HomeKit framework. HomeKit in simple terms can be described as a framework for communicating and controlling connected smart devices present in a user’s home. Apps developed on iOS 8 HomeKit framework can enable homeowners/users to discover or map devices in their home and configure them for controlling via mobile devices. Users can also group a set of actions and invoke them using Siri- the famous intelligent personal assistant from Apple.

HomeKit can unify and simplify the control of various appliances, devices and accessories of a smart home, unlike HealthKit, where all the activities are to be controlled by a dedicated app. In the HomeKit, Apple allows developers to build their own third party apps to control a smart home, which can interact with Siri to enable a unified control.

HomeKit is unified and consistent. How? Let’s see. All the HomeKit data is maintained in a common database and can be accessed by all the apps. All the information created for home irrespective of the app is maintained in this common database. The common database is the core enabler to maintain the consistency of Home Automation.

HomeKit has defined some common accessories like light bulb, door lock etc. However, users can define many more accessories to control, thus providing the motivation for developers to create innovative apps. Apple provides end to end encryption between iOS devices and the accessories to ensure security and privacy of the connection between the controlling device and these accessories, thereby providing a more reliable experience. Also HomeKit APIs would function only when the application is in the foreground, so the user is aware of the response and behavior of the accessories. Thus, no background processing is allowed as of now for HomeKit APIs.

Let us discuss some core HomeKit concepts with their features:

Core Concepts

To get started with the HomeKit, one has to check out the HomeManager. The HomeManager provides the gateway to the common database, allowing to manage homes and notify changes (if any) to be added to homes. It is represented by HMHomeManager class and we can create its instance in the manner mentioned below:

self.homeManager = [[HMHomeManager alloc] init];
[self.homeManager setDelegate:self]

To use HomeManager, we need to implement the following delegate method

– (void)homeManagerDidUpdateHomes:(HMHomeManager *)manager {
self.homes = manager.homes;
self.primaryHome = manager.primaryHome;
}

When this callback HomeManager has prepared all the data and loaded the data it is ready to be used further. Initially there will no Homes in DB. So let’s add a home.

[self.homeManager addHomeWithName:@”My Home” completionHandler:^(HMHome *home, NSError *error) {
if (error == nil) {
// Success. Home with name ‘My Home’ is added
}
else {
// adding home failed. Error can be something liked “Home with same name already exists”
}
}];

Homes must be uniquely named so that it can be recognized by Siri.. It contains the Rooms, Accessories, etc. We can access Rooms and Accessories as follows:

NSArray *allRooms = self.home.rooms;
NSArray *allAccessories = self.home.accessories;

Initially there will be no rooms so let’s add room to our Home.

[self.home addRoomWithName:@”Drawing Room” completionHandler:^(HMRoom *room, NSError *error) {
if(error == nil) {
// Room is added successfully
}
else {
// Unable to add rooms. Possible error can be
}
}];

As seen above, Room is represented by HMRoom class. Room contains accessories it must have a unique name within one Home. and like Homes, they too are recognized by Siri andcan perform actions through commands from Siri.

Accessory corresponds to an actual physical device the user wants to control. It is assigned to a single room. Whenever there is a change to an Accessory like state change or rename, the app gets notified. Like Home or a Room, an Accessory also requires a unique name within Home to be recognized by Siri.

For using Accessory, it must be accessible. The app gets to know about the accessibility by implementing the following callback

-(void)accessoryDidUpdateReachability:(HMAccessory *)accessory {
if(accessory.isReachable == YES) {
// app can communicate or interact with it
}
else {
// accessory is out of range, or may be off, etc
}
}

Now that we have established the components, let us see some features of the HomeKit

Every accessory is assigned a particular service which represents some functionality associated with respect to its characteristics. A good example, let’s say for an accessory light bulb, it’s power state is the service characteristic with whom an app can interact with. Other examples of service characteristics can be range, units, stepper values, etc.

Like other objects, services may or may not have names, however, they must be unique to a room. Named services are exposed and are accessible outside the app but unnamed services are not exposed generally. Siri recognizes only named services along with those that are exposed and defined by Apple.

Services are represented by HMService class. It has name, characteristics, Service Type and accessory. Characteristics are recognized by Siri although they are not defined by the app. Variety of characteristics can be only read like current temperature, or read-write like target temperature for thermostat, or write-only like writing data to some device in order to act based on some conditions like opening the garage door on a beep on my device etc.

Characteristics are represented by HMCharacteristic class. One can read and write characteristics as follows:

// Read Characteristic
[self.characteristic readValueWithCompletionHandler:^(NSError *error) {
if(error != nil) {
id value = self.characteristic.value;
}
}];
// Write Value
[self.characteristic writeValue:@”newValue” completionHandler:^(NSError *error) {
if(error != nil) {
// unable to write
}
}];

We now spoke about the physical things and real world objects. But we need to find and setup those accessories as well. With the “Accessory Browser” one can look for new accessory or browse for accessories.

self.accessoryBrowser = [[HMAccessoryBrowser alloc] init];
self.accessoryBrowser.delegate = self;
// Start looking for devices
[self.accessoryBrowser startSearchingForNewAccessories];// Once done with Searching
[self.accessoryBrowser stopSearchingForNewAccessories];

While the app is searching for new Accessories, we get a new Accessory callback as follows:

-(void)accessoryBrowser:(HMAccessoryBrowser *)browser didFindNewAccessory:(HMAccessory *)accessory {
// a new accessory has been found & can be added to home
[self.home addAccessory:accessory completionHandler:^(NSError *error) {
if(error != nil) {
// adding accessory failed
}
else {
// added successfully. Now you can assign this newly added accessory to a room & name it
}
}];
}

Once the Accessory is added, the app receives the following callback.

-(void)accessoryBrowser:(HMAccessoryBrowser *)browser didRemoveNewAccessory:(HMAccessory *)accessory {
// A new accessory is added to a home
//or removed as it may have gone out of range or has been turned off
}

In this blog we have covered the basic features of iOS 8 HomeKit. Apple has surely turned up the heat in the Internet of Things space. In the next blog, we will discuss on how to test HomeKit applications and dwell on some advanced HomeKit topics as well.