服務([HMService](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/cl/HMService))代表了一個配件(accessory)的某個功能和一些具有可讀寫的特性([HMCharacteristic](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristic_Class/index.html#//apple_ref/occ/cl/HMCharacteristic))。一個配件可以擁有多項服務,一個服務也可以有很多特性。比如一個車庫開門器可能擁有一個照明和開關的服務。照明服務可能擁有打開/關閉和調節亮度的特性。用戶不能制造智能家電配件和它們的服務-配件制造商會制造配件和它們的服務-但是用戶可以改變服務的特性。一些擁有可讀寫屬性的特性代表著某種物理狀態,比如,一個恒溫器中的當前溫度就是一個只可讀的值,但是目標溫度又是可讀寫的。蘋果預先定義了一些服務和特性的名稱,以便讓Siri能夠識別它們。
**獲得配件的服務和屬性**
在依照[Getting the Accessroties in a Room](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/FindingandAddingAccessories/FindingandAddingAccessories.html#//apple_ref/doc/uid/TP40015050-CH3-SW5)中描述,你創建了一個配件對象之后,你可以獲得配件的服務和特性。當然你也可以直接從home中按照類型獲得不同的服務。
重要:不要暴露匿名服務-比如固件升級服務-給用戶
通過[HMAccessory](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMAccessory_Class/index.html#//apple_ref/occ/cl/HMAccessory)類對象的[services](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMAccessory_Class/index.html#//apple_ref/occ/instp/HMAccessory/services)屬性,我們可以獲得一個配件的服務。
~~~
NSArray?*services?=?accessroy.services;
~~~
要獲得一個home當中配件提供的特定服務,使用[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)類對象的[servicesWithTypes:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/servicesWithTypes:)方法。
~~~
//?Get?all?lights?and?thermostats?in?a?home
NSArray?*lightServices?=?[home?servicesWithTypes:[HMServicesTypeLightbulb]];
NSArray?*thermostatServices?=?[home?servicesWithTypes:[HMServicesTypeThermostat]];
~~~
使用[HMServices](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/cl/HMService)類對象的[name](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/instp/HMService/name)屬性來獲得服務的名稱
~~~
NSString?*name?=?services.name;
~~~
要獲得一個服務的特性,請使用[characteristics](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/instp/HMService/characteristics)屬性。
~~~
NSArray?*characteristics?=?service.characteristics
~~~
使用[servicesType](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/instp/HMService/serviceType)屬性來獲得服務的類型
NSString *serviceType = service.serviceType;
蘋果定義了一些服務類型,并能被Siri識別:
* 門鎖(Door locks)
* 車庫開門器(Garage door openers)
* 燈光(Lights)
* 插座(Outlets)
* 恒溫器(Thermostats)
**改變服務名稱**
使用[updateName:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/instm/HMService/updateName:completionHandler:)異步方法來改變服務名稱。傳入此方法的服務名稱參數必須在一個home當中是唯一的,并且服務名可被Siri識別。
~~~
[service updateName:@"Garage 1 Opener" completionHandler:^(NSError *error) {
if (error) {
// Failed to change the name
} else {
// Successfully changed the name
}
}];
~~~
**訪問特性的值**
特性代表了一個服務的一個參數,它要么是只讀、可讀寫或者只寫。它提供了這個參數可能的值的信息,比如,一個布爾或者一個范圍值。恒溫器中的溫度就是只讀的,而目標溫度又是可讀寫的。一個執行某個任務的命令且不要求任何返回-比如播放一段聲音或者閃爍一下燈光來確認某個配件-可能就是只寫的。
蘋果定義了一些特性的類型,并能被Siri識別:
* 亮度(Brightness)
* 最近溫度(Current temperature)
* 鎖的狀態(Lock state)
* 電源的狀態(Power state)
* 目標狀態(Target state)
* 目標溫度(Target temperature)
比如,對于一個車庫開門器來說,目標狀態就是打開或者關閉。對于一個鎖來說,目標狀態又是上鎖和未上鎖。
在你獲得了一個[HMService](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/cl/HMService)對象之后,如?[Getting Services and Their Properties](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/AccessingServicesandTheirCharacteristics/AccessingServicesandTheirCharacteristics.html#//apple_ref/doc/uid/TP40015050-CH6-SW2)所描述的,你可以獲得每個服務的特性的值。因為這些值是從配件中獲得的,這些讀寫的方法都是異步的,并可以傳入一個完成回調的block。
使[用readValueWithCompletionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristic_Class/index.html#//apple_ref/occ/instm/HMCharacteristic/readValueWithCompletionHandler:)異步方法來讀取一個特性的值。
~~~
[characteristic readValueWithCompletionHandler:^(NSError *error) {
if (error == nil) {
// Successfully read the value
id value = characteristic.value;
}
else {
// Unable to read the value
} }];
~~~
在if語句塊中,加入你的代碼以更新app的視圖。
使用[writeValue:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristic_Class/index.html#//apple_ref/occ/instm/HMCharacteristic/writeValue:completionHandler:)異步方法來向一個特性寫入值。
~~~
[self.characteristic writeValue:@42 withCompletionHandler:^(NSError *error) {
if (error == nil) {
// Successfully wrote the value
}
else {
// Unable to write the value
} }];
~~~
不要以為函數調用完成就意味著寫入成功,實際上只有在當完成回調執行并沒有錯誤產生時才表示寫入成功。比如,直到一個開關的特性改變之前都不要改變這個開關的狀態。在if語句塊中,加入你的代碼,以更新app的視圖。
另外,在別的app更新了特性的值時也需要更新視圖,在[Observing Changes to Accessories](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/RespondingtoHomeKitDatabaseChanges/RespondingtoHomeKitDatabaseChanges.html#//apple_ref/doc/uid/TP40015050-CH5-SW1)中有描述。
**創建服務組**
一個服務組([HMServiceGroup](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMServiceGroup_Class/index.html#//apple_ref/occ/cl/HMServiceGroup))提供了控制不同配件的任意數量服務的快捷方式-比如,當用戶離開家之后控制家中的某些燈。

在你創建了一個[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)對象之后,如[Getting the Primary Home and Collection of Homes](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/FindingandAddingAccessories/FindingandAddingAccessories.html#//apple_ref/doc/uid/TP40015050-CH3-SW3)中描述,你也就在這個家中創建一個服務組。
為了創建一個服務組,我們使用[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)類對象的[addServiceGroupWithName:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/addServiceGroupWithName:completionHandler:)方法。方法中參數服務組的名稱必須在此家中唯一,并可以被Siri識別。
~~~
[self.home addServiceGroupWithName:@"Away Lights" completionHandler:^(HMServiceGroup *serviceGroup, NSError *error) {
if (error == nil) {
// Successfully created the service group
} else {
// Unable to create the service group
}];
~~~
我們使用[HMServiceGroup](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMServiceGroup_Class/index.html#//apple_ref/occ/cl/HMServiceGroup)類對象的[addService:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMServiceGroup_Class/index.html#//apple_ref/occ/instm/HMServiceGroup/addService:completionHandler:)方法來向服務組中添加一個服務。服務可以在一個或多個服務組中。
~~~
[serviceGroup addService:service completionHandler:^(NSError *error) {
if (error == nil) {
// Successfully added service to service group
}
// Unable to add the service to the service group
}];
~~~
通過[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)類對象的[serviceGroups](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instp/HMHome/serviceGroups)屬性,來獲得這個家的所有服務組。
~~~
NSArray?*serviceGroups?=?self.home.serviceGroups;
~~~
通過[HMServiceGroup](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMServiceGroup_Class/index.html#//apple_ref/occ/cl/HMServiceGroup)類對象的[accessory](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMService_Class/index.html#//apple_ref/occ/instp/HMService/accessory)屬性,我們獲得服務所對應的智能電器。
~~~
HMAccessory?*accessory?=?service.accessory;
~~~
和配件類似,代理方法在別的app改變服務組時也會被調用。如果你的app使用了服務組,請閱讀HMHomeDelegate Protocol Reference文檔,獲悉你應該實現哪些方法以觀察這些變化。