Skip to article frontmatterSkip to article content

Behaviors in Home Assistant Matter Hub

Overview

Behaviors are modular components that implement Matter clusters and map Home Assistant entity state/actions to Matter endpoints. They are used to compose endpoints and define their functionality.

How Behaviors Work

const LightEndpointType = LightDevice.with(
  BasicInformationServer,
  IdentifyServer,
  HomeAssistantEntityBehavior,
  LightOnOffServer,
  LightLevelControlServer,
);

Configuring Behaviors

const InputButtonOnOffServer = OnOffServer({
  isOn: () => false,
  turnOn: () => ({ action: "input_button.press" }),
  turnOff: null,
}).with(["Lighting"]);

Extending Behaviors

Example: Implementing a Custom Behavior

Below is a short example of how to implement a custom OnOffServer behavior, following the actual pattern used in the codebase:

import { OnOffServer } from "@matter/main/behaviors";

// Custom implementation for the OnOffServer behavior
export class CustomOnOffServer extends OnOffServer {
  
  override initialize() {
    // Init the behavior
    this.state.onOff = true;
  }

  override on() {
    // device was turned on via matter
  }
  
  override off() {
    // device was turned off via matter
  }
}

// Usage with a device type
const MockSwitchEndpointType = SwitchDevice.with(
  BasicInformationServer,
  IdentifyServer,
  CustomOnOffServer.with(["Lighting"]),
);

This pattern can be adapted for any cluster or device type.