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

Create action sets and triggers

May 22, 2021 HomeKit App Development guide

Table of contents

Create action sets and triggers

An action set of HMActionSet and trigger HMTimerTrigger allows you to control multiple smart appliances at the same time. F or example, a set of actions might perform a set of actions before the user d'on bed. A write action writes a value to a attribute. T he actions in the action collection are performed in an indeterminate order. A trigger sets an action set at a specific time and can be executed repeatedly. Each action collection has a unique name in a family and can be recognized by Siri.

Create action sets and triggers

Create a write action

The write action writes a value to the attribute of a service and is added to the action collection. T he HMAction class is an abstract base class of the HMCharacteristic WeriteAction concrete class. An action has an associated attribute object that you can obtain by using the descriptions in Accessing Services and Features to obtain related services and attributes, and then create this HMCharacteristic WhiteAction.

To create an action, we use the initWithCharacteristic:targetValue: method in the HMCharacteristic WeriteAction class.

HMCharacteristicWriteAction *action = [[HMCharacteristicWriteAction alloc] initWithCharacteristic:characteristic targetValue:value];

In your code, you replace the value parameter with the expectation of the corresponding attribute and the value parameter with the corresponding HMCharacteristic object.

Create and execute an action set

An action set is a collection of actions that are performed together. For example, a collection of nighttime actions might include turning off the lights, turning down the thermostat and locking the door.

To create an action set we use addActionSetWithName:completeHandler: Asynchronous methods.

[self.home addActionSetWithName:@"NightTime" completionHandler:^(HMActionSet *actionSet, NSError *error) {
    if (error == nil) {
        // 成功添加了一个动作集
    } else {
        // 添加一个动作集失败

To add an action-to-action set, we use addAction:completeHandler: Asynchronous methods.

[actionSet addAction:action completionHandler:^(NSError *error) {
    if (error == nil) {
        // 成功添加了一个动作到动作集
    } else {
    // 添加一个动作到动作集失败

To remove an action, use removeAction:contentHandler: Method.

To perform an action set, use the executeActionSet:endHandler: method of the HMHome class. F or example, users want to control all holiday lights. W e'll create an action set to turn on all the holiday lights, and another action set to turn off all the holiday lights. To turn on all the holiday lights, send the executeActionSet:endHandler: message to the home object and pass the "Turn on the holiday lights" action set.

Create and turn on the trigger

Triggers execute one or more action sets. i OS manages and runs your triggers in the background. T he HMTrigger class is an abstract class of the HMTimer Trigger concrete class. W hen you create a time trigger, you need to specify the trigger time and the trigger cycle. Creating and opening a timed trigger takes several steps to complete.

Follow these steps to create and start a timed trigger

Create a time trigger

1. Create a timed trigger.

self.trigger = [[HMTimerTrigger alloc] initWithName:name

The trigger time must be set at some point in the future, and the second argument must be 0. I f you set a period, the minimum value for the period is 5 minutes and the maximum value is 5 weeks. Read Date and Time Programming Guide on how to set cycles using NSDateComponents and NSCalendar

2. Add an action set to the trigger.

Use the HMTrigger base class method addActionSet:completeHandler:, to add an action set to the trigger.

3. Add a trigger to the home.

Use the add Trigger:endHandler method in the HMHome class: method to add a trigger to the home.

4. Start the trigger.

The newly created trigger is not started by default. You need to start the trigger using enable:complationHandler: Method.

When a timed trigger is started, it periodically runs its action set.