HomeKit 允许用户创建一个或者多个Home布局。每个Home([HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome))代表一个有网络设备的住所。用户拥有Home的数据并可通过自己的任何一台iOS设备进行访问。用户也可以和客户共享一个Home,但是客户的权限会有更多限制。被指定为primary home的home默认是Siri指令的对象,并且不能指定home。
每个Home一般有多个room,并且每个room一般会有多个智能配件。在home([HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMRoom_Class/index.html#//apple_ref/occ/cl/HMRoom)) 中,每个房间是独立的room,并具有一个有意义的名字,例如“卧室”或者“厨房”,这些名字可以在Siri 命令中使用。一个accessory([HMAccessory](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMAccessory_Class/index.html#//apple_ref/occ/cl/HMAccessory))代表实际家庭中的自动化设备,例如车库开门器。一个sevice([HMService](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/cl/HMService))是accessory提供的?种实际服务,例如打开或者关闭车库,或者车库上的灯。
![homes_2x.png](https://box.kancloud.cn/2015-08-08_55c5769654b59.png "1427188200927392.png")
如果你的app 缓存了home布局的信息,那么当其布局发声改变的时候,app就需要更新这些信息。使用[HMHomeManager](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManager_Class/index.html#//apple_ref/occ/cl/HMHomeManager)对象可以从HomeKit数据库获取[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)和其他相关的对象。本章描述的API获取对象后,你应该通过代理回调函数保持获取对象和HomeKit数据库同步,具体描述请参看“[Observing HomeKit Database Changes](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/RespondingtoHomeKitDatabaseChanges/RespondingtoHomeKitDatabaseChanges.html#//apple_ref/doc/uid/TP40015050-CH5-SW2)".
**创建 Home Manager对象**
使用Home Manager—一个[HMHomeManager](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManager_Class/index.html#//apple_ref/occ/cl/HMHomeManager)对象的访问home、room、配件、服务以及其他HomeKit对象。在创建家庭对象管理器(home manager)之后,直接设置它的代理,以便获取到这些对象之后及时的通知到你。
~~~
self.homeManager = [[HMHomeManager alloc] init];
self.homeManager.delegate = self;
~~~
当你创建一个home manager对象时,HomeKit就开始从HomeKit数据库获取这些homes和相关对象,例如room和accessory对象。当HomeKit正在获取那些对象时,home manager 的[primaryHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManager_Class/index.html#//apple_ref/occ/instp/HMHomeManager/primaryHome)属性是nil,并且[homes](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManager_Class/index.html#//apple_ref/occ/instp/HMHomeManager/homes)属性是个空数组。你的app应该处理用户还没有完成创建home的情况,但是app应该等待直到HomeKit完成初始化。当获取对象完成之后,HomeKit 会发送[homeManagerDidUpdateHomes:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManagerDelegate_Protocol/index.html#//apple_ref/occ/intfm/HMHomeManagerDelegate/homeManagerDidUpdateHomes:)消息给home manager的代理。
注意:当app进入前台或者在后台Home manager属性发生改变时,这个[homeManagerDidUpdateHomes:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManagerDelegate_Protocol/index.html#//apple_ref/occ/intfm/HMHomeManagerDelegate/homeManagerDidUpdateHomes:)方法就会被调用,详情请参阅“[Observing Changes to the Collection of Homes](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/RespondingtoHomeKitDatabaseChanges/RespondingtoHomeKitDatabaseChanges.html#//apple_ref/doc/uid/TP40015050-CH5-SW3)”。
**获取Primary Home和 Homes集合**
通过home manager的[primaryHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManager_Class/index.html#//apple_ref/occ/instp/HMHomeManager/primaryHome)属性,可以得到primary home,代码如下:
~~~
HMHome *home = self.homeManager.primaryHome;
~~~
使用home manager的[homes](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeManager_Class/index.html#//apple_ref/occ/instp/HMHomeManager/homes)属性可以得到用户的所有home的集合;例如自家主要居所、度假别墅以及办公室。每个home都对应一个独立的home对象。
~~~
HMHome *home;
for(home in self.homeManager.homes ){
…}
~~~
**获取 Home中的所有room**
在一个home中,rooms属性定义accessories的物理位置。用home的[rooms](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instp/HMHome/rooms)属性可以枚举home中的所room。
~~~
HMHome *home = self.homeManager.primaryHome;
HMRome *room;
for(room in home.rooms){
…
}
~~~
**获取Room 中的Accessories**
Accessories 数组属于home,但是被指定给了home中的room。假如用户没有给一个accessory指定room,那么这个accessories被指定一个默认的room ,这个room是[roomForEntireHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/roomForEntireHome)方法的返回值。用room的[accessories](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMRoom_Class/index.html#//apple_ref/occ/instp/HMRoom/accessories)属性可以枚举room中所有的accessory。代码如下:
~~~
HMAccessory *accessory;
for(accessory in room.accessories){
…
}
~~~
如果你要展示一个个accessory的相关信息或者允许用户控制它,可设置accessory的代理方法并实现这个代理方法,详情请见“[Observing Changes to Accessories](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/RespondingtoHomeKitDatabaseChanges/RespondingtoHomeKitDatabaseChanges.html#//apple_ref/doc/uid/TP40015050-CH5-SW1)”.
一旦你获取到一个accessory对象,你就可以访问它的服务和对象,详情请参阅“[Accessing Services and Characteristics](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/AccessingServicesandTheirCharacteristics/AccessingServicesandTheirCharacteristics.html#//apple_ref/doc/uid/TP40015050-CH6-SW1)”。
**获取Home中的Accessories属性**
使用[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)类中的[accessories](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instp/HMHome/accessories)的方法,可以直接从Home对象中获取所有的accessory对象,而不用枚举home中的所有room对象(详情请见“[Getting the Accessories in a Room](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/FindingandAddingAccessories/FindingandAddingAccessories.html#//apple_ref/doc/uid/TP40015050-CH3-SW5)”。