public final class AccessoryService

  1. Object
  2. AccessoryService

One functional endpoint of an Accessory: HomeKit’s HMService, one Matter endpoint.

The distinction matters more than it looks. A two-gang wall switch is one accessory with two services, and writing Trait.ON_OFF to the accessory without saying which service is ambiguous. Everything in this API that reads or writes a trait names a service.

An immutable snapshot. Nothing here calls into the platform.

Constructors

public AccessoryService(String id, String name, ServiceType type, boolean primary, List<TraitConstraint> constraints)Creates a service snapshot.

Methods

public String getId()The identifier this service is addressed by, unique within its accessory.
public String getName()The user-visible name, empty when the accessory gave none.
public ServiceType getType()What this service is.
public boolean isPrimary()Whether this is the accessory’s main service – the one a UI showing a single control for the whole device should use.
public List<TraitConstraint> getConstraints()Every trait this service exposes, with what each will accept.
public TraitConstraint getConstraint(Trait trait)What this service will accept for one trait.
public boolean supports(Trait trait)Whether this service exposes a trait at all.
public List<Trait> getTraits()Every trait this service exposes.
public String toString()Returns a string representation of the object.

Inherited methods

Constructor details

AccessoryService

public AccessoryService(String id, String name, ServiceType type, boolean primary, List<TraitConstraint> constraints)
Creates a service snapshot. Called by the ports and by the local home; application code receives these rather than building them.

Parameters

id String
the service identifier, unique within its accessory
name String
the user-visible name, or null for none
type ServiceType
what the service is; null becomes ServiceType.OTHER
primary boolean
whether this is the accessory’s main service
constraints List<TraitConstraint>
what each trait on this service will accept; null becomes empty

Throws

IllegalArgumentException
when id is null or empty

Method details

getId

public String getId()
The identifier this service is addressed by, unique within its accessory.

Returns

the identifier, never null

getName

public String getName()
The user-visible name, empty when the accessory gave none.

Returns

the name, never null

getType

public ServiceType getType()
What this service is.

Returns

the type, never null

isPrimary

public boolean isPrimary()
Whether this is the accessory’s main service – the one a UI showing a single control for the whole device should use.

Returns

true for the primary service

getConstraints

public List<TraitConstraint> getConstraints()
Every trait this service exposes, with what each will accept.

Returns

an immutable list, possibly empty

getConstraint

public TraitConstraint getConstraint(Trait trait)
What this service will accept for one trait.

Parameters

trait Trait
the trait to look up, or null

Returns

the constraint, or null when this service does not expose the trait

supports

public boolean supports(Trait trait)

Whether this service exposes a trait at all.

This is the question to ask instead of switching on getType() or on SmartHome.getBackend(). A service exposes what it exposes, and two accessories of the same type routinely differ.

Parameters

trait Trait
the trait to test, or null

Returns

true when the trait is present here

getTraits

public List<Trait> getTraits()
Every trait this service exposes.

Returns

an immutable list, possibly empty

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())