public final class TraitWrite

  1. Object
  2. TraitWrite

One change to make: set one Trait on one AccessoryService to one value.

TraitWrite w = new TraitWrite(lamp, lamp.getPrimaryService(),
        Trait.BRIGHTNESS, TraitValue.of(40, TraitUnit.PERCENT));
SmartHome.getInstance().write(w).onResult((results, err) -> { ... });

Writes name a service, not just an accessory, because an accessory can have several – the two halves of a two-gang switch both expose Trait.ON_OFF and writing to “the switch” would be ambiguous.

Constructors

public TraitWrite(Accessory accessory, AccessoryService service, Trait trait, TraitValue value)Creates a write against a snapshot.
public TraitWrite(String accessoryId, String serviceId, Trait trait, TraitValue value)Creates a write against identifiers, for code working from persisted ids that has not re-fetched the graph.

Methods

public TraitWrite setAuthorizationData(String authorizationData)A credential the accessory needs before it will obey.
public String getAuthorizationData()The credential set by setAuthorizationData(java.lang.String).
public String getAccessoryId()The accessory this write changes.
public String getServiceId()The service on that accessory.
public Trait getTrait()The trait this write sets.
public TraitValue getValue()The value it sets the trait to.
public SceneAction toSceneAction()This write as a scene action, for building a Scene out of the changes a user just made.
public String toString()Returns a string representation of the object.

Inherited methods

Constructor details

TraitWrite

public TraitWrite(Accessory accessory, AccessoryService service, Trait trait, TraitValue value)
Creates a write against a snapshot.

Parameters

accessory Accessory
the accessory to change
service AccessoryService
the service on it
trait Trait
the trait to set
value TraitValue
the value to set it to

Throws

IllegalArgumentException
when any argument is null, when the value’s kind does not match the trait’s, or when the trait can never be written

TraitWrite

public TraitWrite(String accessoryId, String serviceId, Trait trait, TraitValue value)
Creates a write against identifiers, for code working from persisted ids that has not re-fetched the graph.

Parameters

accessoryId String
the accessory to change
serviceId String
the service on it
trait Trait
the trait to set
value TraitValue
the value to set it to

Throws

IllegalArgumentException
when any argument is null or empty, when the value’s kind does not match the trait’s, or when the trait can never be written

Method details

setAuthorizationData

public TraitWrite setAuthorizationData(String authorizationData)

A credential the accessory needs before it will obey.

The only thing that uses this today is a Matter door lock configured with RequirePINforRemoteOperation, which refuses Trait.TARGET_LOCK_STATE without a user PIN and fails the write with HomeError.PIN_REQUIRED. HomeKit never takes a PIN and ignores this.

Treat the value the way you would any credential: do not log it, do not persist it in the clear, and prefer prompting for it over storing it. It is passed straight to the platform and is not retained here beyond the life of this object.

Parameters

authorizationData String
the credential, or null to clear it

Returns

this write, for chaining

getAuthorizationData

public String getAuthorizationData()

Returns

the credential, or null

getAccessoryId

public String getAccessoryId()
The accessory this write changes.

Returns

the accessory identifier, never null

getServiceId

public String getServiceId()
The service on that accessory.

Returns

the service identifier, never null

getTrait

public Trait getTrait()
The trait this write sets.

Returns

the trait, never null

getValue

public TraitValue getValue()
The value it sets the trait to.

Returns

the value, never null

toSceneAction

public SceneAction toSceneAction()

This write as a scene action, for building a Scene out of the changes a user just made.

Drops any credential: a scene is replayed by the platform later, when this app is not running, and there is nowhere to put one.

Returns

an equivalent scene action

toString

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())