Apple HomeKit — Your home at your command

Hetal Patel
Simform Engineering
7 min readMay 18, 2021

HomeKit is a library that allows you to communicate and control your smart home accessories with the application.

It provides a central repository for configuring these accessories in your home. It is available on iPad, iPhone, iWatch, and iMac.

What’s great about HomeKit

You can control your smart-home device even if you are away from your home.

When you are in your home, the kit directly connects with your home device and smart home hubs let you control devices when you are out of the house.

Getting started

HomeKit has a defined structure to enable developer communications with the compatible devices.

The root of HomeKit is Homes. Each home can have one or more rooms, and each room can have one or more accessories. An accessory could be anything just like a fan or lights.

Let’s implement this amazing kit into an application

You may not have a HomeKit device available to you to test your application. You don’t need to buy the HomeKit device instead you can make use of the simulator provided by Apple to create HomeKit accessories.

Apple provides a feature to test our application virtually so the developer need not spend on different devices.

Let’s create virtual simulators and accessories for further use.

Setup HomeKit simulator

Select the project and add the HomeKit capability. If you don’t have this capability then you can also download it.

Add HomeKit capability
Download simulator option

On clicking the download button, it will redirect you to the apple developer account. You will see a window with downloading different tools. Select Hardware IO Tools for Xcode 7.3.

Xcode tools option at Apple developer account

Copy the HomeKit Accessory Simulator app to your application’s folder. Now, launch HomeKit Accessory Simulator, from here you can add and configure an accessory.

HomeKit Accessory Simulator
Add new accessory
Add service
Fan accessory with its configuration information

Your first HomeKit accessory is ready now!!

Let’s go to Home: Our starter project

Before you code, you need to take permission from user as you are using user’s private data.

Add permission in .plist file

To start interacting with HomeKit, the HMHomeManager object will give you access to HomeKit as shown below.

You can also set the delegate for applying updates of home accessories to the home manager.

Not every user will already have a Home defined in HomeKit, so you’ll add the ability to create your own home and give it a Room by adding this code:

What does this code do?

  1. To add home we call this method addHome(withName:completionHandler:) by passing the name of the home in the argument.
  2. Verify the error while adding home and manage it accordingly.
  3. After you successfully created HMHome, add a room to the home.
  4. If you successfully added a room, you can write a code according to your requirement.

Add Accessories

Adding accessories is not the same as adding homes. They need to be discovered before they can be added to a specified room.

Structure

How to discover accessories?

Using HMAccessoryBrowser, you can discover accessories.

You can get the details of discovered accessory using HMAccessoryBrowserDelegate like this:

Once you get the available accessory and add them to your accessories object, you will get the following screens for pairing the accessories.

You can scan accessory code

If you don’t have code, you can add an accessory by adding code manually.

Add accessory code option
Add code manually

You have added your accessory successfully. You can also add an accessory that is associated with a specific room.
Now you need to add the ability to control your accessory. You can try to find an accessory with a service that matches your desire service type. In our case, the service is HMServiceTypeFan .

Set the delegate for the accessory and enable notification on that characteristic. This way, you will achieve a callback when the value of the characteristic changes.

How to control accessory from the application?

You can update the value of your selected accessory from your application. In our case, we have HMServiceTypeFanservice and to change the value of rotation speed of the fan we can achieve by this

Here, speed is my variable which is responsible to set the speed of the fan and you can set any value here according to your necessities and

Set the value of characteristic on the basis of maximum value.

You can have an HMAccessory that contains HMService and then HMService contains HMCharacteristic.

What’s not great about HomeKit?

  • An additional development time is required to build in Apple’s proprietary hardware. This slows the developer’s time to market, which has the potential to hurt revenue and customer satisfaction.
  • The development’s cost of the HomeKit device and chip is not cheap.
  • HomeKit allows “away from home support” if you have an Apple TV or an iPad at home that you can use as a hub. This feature only works with fourth-generation or newer Apple TVs.

Tips 🖊

Solving HomeKit connectivity problems

  1. Make sure the HomeKit device has power and it’s turned on and it’s in range of your router if it’s a Wi-Fi device.
  2. Check the Wi-Fi connection and reset your router. Make sure your iOS device is up to date, connected to the internet, and that you’re signed in to ‌iCloud‌.
  3. Turn the HomeKit device off, wait 10 seconds, and turn it back on. Do the same thing with your ‌iPhone‌ or other devices you’re attempting to use with HomeKit.
  4. Make sure your HomeKit device is on the right Wi-Fi band. There are a lot of HomeKit devices that are 2.4GHz while most devices connect to 5GHz networks, and that can sometimes cause problems. If you have a 2.4GHz accessory, make sure it’s on the 2.4GHz network. Steps for this will vary based on your setup.
  5. Remove the device from HomeKit in the app and then re-add it by scanning it. For some HomeKit products, it eliminates scenes and automation.
  6. Remove the device from HomeKit and reset it. This is a step that’s necessary when removing some HomeKit devices from a HomeKit setup. You need to consult your device’s manual because resetting is different on every product.

If none of these steps work, you need to contact the support staff for whichever product you’re having problems with for troubleshooting purposes.

Conclusion

You can provide a way to configure accessories and create actions to control them using the HomeKit framework. It allows users to communicate and control connected accessories in their homes using your app.

Hope these helped you out and you learned something new today, if you enjoyed reading this article be sure to throw me a couple of claps.👏

--

--

Hetal Patel
Simform Engineering

Senior Software Engineer @simform | #iOSDeveloper | Explorer | Learner