public final class TraitReading
- Object
- TraitReading
One trait’s value at one moment, or the reason there isn’t one.
Three outcomes, not two
A reading can carry a value, carry an error, or carry neither. The third is the one that catches people: an accessory can legitimately have nothing to report. Matter marks a temperature it has not measured with a null sentinel, a light in colour-temperature mode has no meaningful hue, and an illuminance sensor in the dark reports “too dark to measure”. None of those is a failure and none of them is a value.
So hasValue() is asked first, and getValue() answers null when it
is false. There is no zero standing in for a missing measurement anywhere
in this API – a thermostat reading 0 degrees and a thermostat that has not
measured are different facts, and conflating them is how a UI comes to
display a freezing living room.
A batch read produces one of these per requested trait, so a partial success is the normal case: three readings with values and one unreachable accessory is a successful read, not a failed one.
Methods
Inherited methods
Method details
of
public static TraitReading of(String accessoryId, String serviceId, Trait trait, TraitValue value, long timestampMillis)Parameters
accessoryIdString- the accessory read
serviceIdString- the service on it
traitTrait- the trait read
valueTraitValue- the value
timestampMillislong- when the value was current, in milliseconds since the epoch; zero when the backend did not say
Returns
Throws
IllegalArgumentException- when
traitorvalueisnull, or when the value’s kind does not match the trait’s
absent
public static TraitReading absent(String accessoryId, String serviceId, Trait trait)Parameters
accessoryIdString- the accessory read
serviceIdString- the service on it
traitTrait- the trait read
Returns
Throws
IllegalArgumentException- when
traitisnull
failed
public static TraitReading failed(String accessoryId, String serviceId, Trait trait, HomeError error, String message)Parameters
accessoryIdString- the accessory read
serviceIdString- the service on it
traitTrait- the trait read
errorHomeError- why it failed;
nullbecomesHomeError.UNKNOWN messageString- the platform’s own text, or
null
Returns
Throws
IllegalArgumentException- when
traitisnull
getAccessoryId
public String getAccessoryId()Returns
nullgetServiceId
public String getServiceId()Returns
nullgetTrait
public Trait getTrait()Returns
nullhasValue
public boolean hasValue()Whether there is a value to read.
Ask this before getValue(). See the class note for why a missing
value is a normal outcome rather than an error.
Returns
getValue
public TraitValue getValue()Returns
null when the accessory had none or the read failedgetTimestampMillis
public long getTimestampMillis()When this value was current, in milliseconds since the epoch.
Zero when the backend did not say, which is common – most accessory reads are answered from a cache the platform keeps and do not carry a timestamp. Do not render an age from zero.
Returns
isFailed
public boolean isFailed()Returns
getError
public HomeError getError()Returns
null when the read succeededgetErrorMessage
public String getErrorMessage()Returns
nulltoString
public String toString()