# Class TraitWrite

java.lang.Object
com.codename1.home.TraitWrite

public final class TraitWrite extends Object

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.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TraitWrite(Accessory accessory, AccessoryService service, Trait trait, TraitValue value)`
  Creates a write against a snapshot.
  `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.

- ## Method Summary

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

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### TraitWrite

    public TraitWrite(Accessory accessory, AccessoryService service, Trait trait, TraitValue value)

    Creates a write against a snapshot.

    ###### Parameters

    - `accessory`: the accessory to change

    - `service`: the service on it

    - `trait`: the trait to set

    - `value`: 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`: the accessory to change

    - `serviceId`: the service on it

    - `trait`: the trait to set

    - `value`: 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`: the credential, or `null` to clear it

    ###### Returns

    this write, for chaining

  - ### getAuthorizationData

    public String getAuthorizationData()

    The credential set by `setAuthorizationData(java.lang.String)`.

    ###### 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()
    Description copied from class: `Object`
    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())
    Overrides:
      `toString` in class `Object`