一個動作集合[HMActionSet](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMActionSet_Class/index.html#//apple_ref/occ/cl/HMActionSet)和觸發器[HMTimerTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTimerTrigger_Class/index.html#//apple_ref/occ/cl/HMTimerTrigger)允許你同時控制多個智能電器。比如,一個動作集合可能會在用戶上床休息之前執行一組動作[HMAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMAction_Class/index.html#//apple_ref/occ/cl/HMAction)。一個寫動作向一個特性寫入了值。動作集合中的動作是以不確定的順序執行的。一個觸發器會在一個特定的時間出發一個動作集并可以重復執行。每一個動作集合在一個家庭中都有唯一的名稱并可被Siri識別。

**創建寫入動作**
寫入動作會向一個服務的特性寫入值并被加入到動作集合中去。[HMAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMAction_Class/index.html#//apple_ref/occ/cl/HMAction)類是[HMCharacteristicWriteAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/cl/HMCharacteristicWriteAction)具體類的抽象基類。一個動作有一個相關聯的特性對象,你可以通過Accessing Services and Characteristics中描述的來獲取相關的服務和特性,然后創建這個[HMCharacteristicWriteAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/cl/HMCharacteristicWriteAction)。
為了創建一個動作,我們使用[HMCharacteristicWriteAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/cl/HMCharacteristicWriteAction)類中的[initWithCharacteristic:targetValue:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/instm/HMCharacteristicWriteAction/initWithCharacteristic:targetValue:)方法。
~~~
HMCharacteristicWriteAction?*action?=?[[HMCharacteristicWriteAction?alloc]?initWithCharacteristic:characteristic?targetValue:value];
~~~
在你的代碼中,你使用對應的特性的期望來替換value參數,并使用對應的[HMCharacteristic](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristic_Class/index.html#//apple_ref/occ/cl/HMCharacteristic)對象來替換characteristic參數。
**創建并執行動作集**
一個動作集就是一個共同執行的動作的集合。比如一個夜間動作集合可能包含關閉電燈,調低恒溫水平和鎖上房門。
為了創建一個動作集我們使用[addActionSetWithName:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/addActionSetWithName:completionHandler:)異步方法。
~~~
[self.home addActionSetWithName:@"NightTime" completionHandler:^(HMActionSet *actionSet, NSError *error) {
if (error == nil) {
// 成功添加了一個動作集
} else {
// 添加一個動作集失敗
}
}];
~~~
為了添加一個動作到動作集,我們使用[addAction:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMActionSet_Class/index.html#//apple_ref/occ/instm/HMActionSet/addAction:completionHandler:)異步方法。
~~~
[actionSet addAction:action completionHandler:^(NSError *error) {
if (error == nil) {
// 成功添加了一個動作到動作集
} else {
// 添加一個動作到動作集失敗
}
}];
~~~
想要移除一個動作,可使用[removeAction:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMActionSet_Class/index.html#//apple_ref/occ/instm/HMActionSet/removeAction:completionHandler:)方法。
想要執行一個動作集,可使用[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)類的[executeActionSet:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/executeActionSet:completionHandler:)方法。比如,用戶希望控制所有的節日彩燈。我們就創建一個動作集來打開所有的節日彩燈,另外一個動作集來關閉所有的節日彩燈。為了打開所有的節日彩燈,發送executeActionSet:completionHandler:消息給home對象,并傳遞"打開節日彩燈"動作集。
**創建并開啟觸發器**
觸發器會執行一個或多個動作集。iOS會在后臺管理和運行你的觸發器。[HMTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/cl/HMTrigger)類是[HMTimerTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTimerTrigger_Class/index.html#//apple_ref/occ/cl/HMTimerTrigger)具體類的抽象類。當你創建一個定時觸發器時,你需要指定觸發時間和觸發的周期。創建并開啟一個定時觸發器需要多個步驟來完成。
遵循下面幾步來創建并啟動一個定時觸發器
創建一個定時觸發器
1.創建定時觸發器。
~~~
self.trigger = [[HMTimerTrigger alloc] initWithName:name
fireDate:fireDate
timeZone:niL
recurrence:nil
recurrenceCalendar:nil];
~~~
觸發時間必須設置在將來的某個時刻,第二個參數必須為0.如果你設置了一個周期,周期的最小值是5分鐘,最大值是5周。關于如何使用[NSDateComponents](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/index.html#//apple_ref/occ/cl/NSDateComponents)和[NSCalendar](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/index.html#//apple_ref/occ/cl/NSCalendar)來設置周期,請閱讀[Date and Time Programming Guide](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i)
2\. 添加一個動作集到觸發器。
使用[HMTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/cl/HMTrigger)基類方法[addActionSet:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/instm/HMTrigger/addActionSet:completionHandler:),來添加一個動作集到觸發器。
3\. 添加一個觸發器到家庭。
使用HMHome類中的[addTrigger:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/addTrigger:completionHandler:)方法來添加一個觸發器到家庭。
4\. 啟動觸發器。
新創建的觸發器默認是未啟動的。需要使用[enable:complationHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/instm/HMTrigger/enable:completionHandler:)方法啟動觸發器。
一個定時觸發器被啟動后,會周期性的運行它的動作集。