Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Create a Home layout


May 22, 2021 HomeKit App Development guide


Table of contents


Create a Home layout

HomeKit allows users to create one or more Home layouts. E ach Home (HMHome) represents a home with internet access. U sers have Home's data and can access it from any of their iOS devices. U sers can also share a Home with the customer, but the customer's permissions are more restrictive. Home, which is designated as primary home, defaults to the object of the Siri instruction and cannot specify Home.

Each Home typically has more than one room, and each room typically has more than one smart accessory. I n Home, each room is a separate room and has a meaningful name, such as "bedroom" or "kitchen," which can be used in Siri commands. A n accessory represents an automated device in a real home, such as a garage door opener. I s an HMService provided by accessory? practical services, such as turning the garage on or off, or the lights on the garage.

Create a Home layout

If your app caches information about the Home layout, the app needs to update that information when its layout changes. can be used to get HMHome and other related objects from the HomeKit database. After you get the object through the API described in this chapter, you should keep the get object and homeKit database in sync through the proxy callback function, see "Observing HomeKit Database Changes" for specific descriptions.

Create a Home Manager object

Access home, room, accessories, services, and other HomeKit objects using Home Manager, an HMHomeManager object. After you create the Home Manager, set up its proxy directly to get timely notifications of these objects.

self.homeManager = [[HMHomeManager alloc] init];
self.homeManager.delegate = self;

When you create a home manager object, HomeKit starts getting those homes and related objects from the HomeKit database, such as room and accessory objects. W hen HomeKit is getting those objects, home manager's primaryHome property is nil, and the homes property is an empty array. Y our app should handle situations where the user hasn't finished creating home yet, but the app should wait until HomeKit completes initialization. When the acquisition object is complete, HomeKit sends homeManager DidUpdateHomes: a message to the home manager's agent.

Note: This homeManagerDidUpdateHomes: Method is called when the app enters the fore desk or changes the Home Manager property in the background, see "Observing Changes to The Collection of Homes" for details.

Get the Primary Home and Homes collections

With the primaryHome property of home manager, you can get primary home with the following code:

HMHome *home = self.homeManager.primaryHome;

Use home manager's homes property to get a collection of all home from the user, such as your home's primary residence, vacation home, and office. Each home corresponds to a separate home object.

HMHome *home;
 for(home in self.homeManager.homes ){
 …}

Get all rooms in Home

In a home, the rooms property defines the physical location of accessories. You can enumere the room in home with the rooms property of home.

HMHome *home = self.homeManager.primaryHome;
 HMRome *room;
 for(room in home.rooms){
 …
 }

Get Accessories in room

TheAccessories array belongs to home, but is assigned to room in home. I f the user does not specify a room for accessory, the accessories is assigned a default room, which is the return value of the roomForEntireHome method. U se the room's accessories property to enumerge all the accessory in the room. The code is as follows:

HMAccessory *accessory;
 for(accessory in room.accessories){
 …
 }

If you want to show information about an accessory or allow users to control it, you can set up accessory's proxy method and implement it, see "Observing Changes to Accessories" for details.

Once you get an accessory object, you can access its services and objects, see Accessing Services and Features for more information.