Class TraitConstraint
What one particular accessory will actually accept for one particular
Trait: whether it can be read or written, the range it works over, the
step it moves in, and for an enum trait, which constants it can produce.
Trait documents the nominal range -- brightness is a percentage, so it
is 0 to 100. This carries the real one, from HomeKit's
HMCharacteristicMetadata or the Matter attribute's own min and max
attributes. A dimmer whose floor is 10 percent and that steps in fives says
so here, and a slider built from these numbers will not offer the user a
value the accessory is going to reject.
Writes are refused, not clamped
A write outside getMinimum() to getMaximum() fails with
HomeError.VALUE_OUT_OF_RANGE. Clamping was the obvious alternative and it
is worse: an app that asked for 40 degrees and silently got 38 never learns
it was wrong, so the bug reaches the user as a thermostat that "does not go
high enough" rather than as a failure at the call site where it can be
fixed.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanaccepts(TraitValue value) Whether a value falls inside what this accessory will accept.static TraitConstraintA constraint for an enum trait whose accessory declared which values it can produce.doubleThe largest value this accessory accepts, inTrait.getUnit().doubleThe smallest value this accessory accepts, inTrait.getUnit().doublegetStep()The increment this accessory moves in, inTrait.getUnit().getTrait()The trait this describes.The ordinals of the domain-enum constants this accessory can produce or accept.booleanhasRange()booleanWhether this accessory will answer a read of this trait.booleanWhether this accessory will accept a write of this trait.booleanWhether this accessory pushes changes to this trait.static TraitConstraintA constraint for a trait whose accessory declared no range -- a boolean, or a numeric trait the platform did not describe.static TraitConstraintranged(Trait trait, boolean readable, boolean writable, boolean notifies, double minimum, double maximum, double step) A constraint for a numeric trait whose accessory declared its range.toString()Returns a string representation of the object.
-
Method Details
-
of
A constraint for a trait whose accessory declared no range -- a boolean, or a numeric trait the platform did not describe.
Parameters
-
trait: the trait this describes -
readable: whether the accessory will answer a read -
writable: whether the accessory will accept a write -
notifies: whether the accessory pushes changes, so a subscription on it can do better than polling
Returns
the constraint
Throws
IllegalArgumentException: whentraitisnull
-
-
ranged
public static TraitConstraint ranged(Trait trait, boolean readable, boolean writable, boolean notifies, double minimum, double maximum, double step) A constraint for a numeric trait whose accessory declared its range.
Parameters
-
trait: the trait this describes -
readable: whether the accessory will answer a read -
writable: whether the accessory will accept a write -
notifies: whether the accessory pushes changes -
minimum: the smallest accepted value, inTrait.getUnit() -
maximum: the largest accepted value, inTrait.getUnit() -
step: the increment the accessory moves in, or zero when it did not say
Returns
the constraint
Throws
IllegalArgumentException: whentraitisnull, ormaximumis belowminimum
-
-
choices
public static TraitConstraint choices(Trait trait, boolean readable, boolean writable, boolean notifies, int[] validOrdinals) A constraint for an enum trait whose accessory declared which values it can produce.
Parameters
-
trait: the trait this describes -
readable: whether the accessory will answer a read -
writable: whether the accessory will accept a write -
notifies: whether the accessory pushes changes -
validOrdinals: the ordinals of the constants this accessory can produce or accept, in the domain enum for this trait;nullor empty means it did not say, which is not the same as "none"
Returns
the constraint
Throws
IllegalArgumentException: whentraitisnull
-
-
getTrait
The trait this describes.
Returns
the trait, never
null -
isReadable
public boolean isReadable()Whether this accessory will answer a read of this trait.
Returns
truewhen the trait is readable here -
isWritable
public boolean isWritable()Whether this accessory will accept a write of this trait.
Narrower than
Trait.isReadOnly(), which says whether writing the trait could ever mean anything at all. A writable trait can still be read-only on a particular accessory -- a thermostat you have been given view-only access to, a covering with its motor disabled.Returns
truewhen the trait is writable here -
notifiesOnChange
public boolean notifiesOnChange()Whether this accessory pushes changes to this trait.
Where this is
false, a subscription still works but has nothing better than polling behind it, so changes arrive late or only when you callSmartHome.drainChanges(). Independent ofTraitSubscription.isPushDelivery(), which is about whether the platform can deliver at all; this is about whether the accessory bothers to say.Returns
truewhen the accessory reports changes on its own -
hasRange
public boolean hasRange()Whether
getMinimum(),getMaximum()andgetStep()mean anything.Returns
truewhen the accessory declared a range -
getMinimum
public double getMinimum()The smallest value this accessory accepts, in
Trait.getUnit().Returns
the minimum, or zero when
hasRange()isfalse -
getMaximum
public double getMaximum()The largest value this accessory accepts, in
Trait.getUnit().Returns
the maximum, or zero when
hasRange()isfalse -
getStep
public double getStep()The increment this accessory moves in, in
Trait.getUnit().Zero means it did not say, which is not the same as continuous -- treat it as unknown rather than as a step of nothing.
Returns
the step, or zero
-
getValidOrdinals
-
accepts
Whether a value falls inside what this accessory will accept.
Checks the range for a numeric trait and the ordinal list for an enum one, and answers
truefor anything the accessory did not constrain -- this is what the write path tests, and refusing values on the strength of information an accessory never gave would fail writes that work.The step is deliberately not enforced. Accessories declare steps they then round to happily, and refusing 33 percent on a dimmer that declares fives would reject a value the hardware accepts.
Parameters
value: the value to test, ornull
Returns
truewhen the value is acceptable, or when the accessory declared no constraint to test it against;falsefor anullvalue or one whose kind does not match the trait -
toString
Description copied from class:ObjectReturns 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())
-