Skip to content

Configuration

Leonard Sperry edited this page Sep 28, 2024 · 13 revisions

You have two options for configuring HaKafkaNet in your program.cs. The example app shows both methods.

appsettings.json

The first option is to copy the appsettings.json file from the example app into your project. When you do that, registration in your 'proram.cs' should look as follows:

HaKafkaNetConfig config = new HaKafkaNetConfig();
builder.Configuration.GetSection("HaKafkaNet").Bind(config);
services.AddHaKafkaNet(config);

Below is an example of what your appsetting.json file might look like. NLog is included for log tracing.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "MyHome": "Debug"
    }
  },
  "NLog":{
    "rules":[
      {
        "logger": "Microsoft.*",
        "minLevel": "Warn",
        "finalMinLevel":"Warn"
      },
      {
        "logger": "System.Net.*",
        "minLevel": "Warn",
        "finalMinLevel":"Warn"
      },
      {
        "logger": "HaKafkaNet.*",
        "minLevel": "Debug",
        "finalMinLevel": "Debug"
      },
      {
        "logger": "MyHome.*",
        "minLevel": "Debug",
        "finalMinLevel":"Debug"
      },
    ]
  },
  "AllowedHosts": "*",
  "HaKafkaNet": {
    "KafkaBrokerAddresses": ["<ip of your kafka instance>:9094"],
    "KafkaTopic": "home_assistant_states",
    "ExposeKafkaFlowDashboard" : true,
    "UseDashboard": true,
    "StateHandler":{
      "GroupId": "hakafkanet-consumer-example",
      "BufferSize" : 5,
      "WorkerCount": 5
    },
    "HaConnectionInfo":{
      "BaseUri": "http://<ip of your HA instance>:8123",
      "AccessToken": "YOUR_LONG_LIVED_HA_ACCESSTOKEN"
    }
  }
}

Programatic

Alternatively, you can configure your instance programmatically. When you choose this option you must provide your own configuration.

services.AddHaKafkaNet(options =>{
    //minimum amount of config
    options.KafkaBrokerAddresses = ["your kafka instance"];
    options.HaConnectionInfo.AccessToken = "your access token";
    options.HaConnectionInfo.BaseUri = "your Home Assistant location";
    //set additional options
});

The above snippet demonstrates the minimum amount of configuration needed. You can override any defaults listed below.

Configuration options

Below are the definitions of the HaKafaNet config object

KafkaBrokerAddresses

string[] - required - defaults to ["localhost:9094"]

An array of addresses in your cluster. Only one is needed as demonstrated in the example.

KafkaTopic

string - required

This is the topic that HaKafkaNet will read from. It should be the same as configured in the Home Assistant Kafka integration.

ExposeKafkaFlowDashboard

bool - defaults to true

When set to true, the Kafkaflow endpoint is set up at ~/kafkaflow

UseDashboard

bool - defaults to false

When set to true, the HaKafakaNet dashboard is set up at ~/dasboard.html

StateHandler

object

Settings used by the State Handler

GroupId

string - defaults to "hakafkanet-consumer"

Change this value if you want to have multiple environments running against the same cluster

BufferSize

int - defaults to 5

WorkerCount

int - defaults to 5

See KafkaFlow documentation for details.

HaConnectionInfo

object - required

Note: this used to be labled API. Please use HaConnectionInfo instead. Future versions will not support the old name.

BaseUri

string - required

The URI for your Home Assistant instance. e.g. http://homeassistant.local:8123

AccessToken

string - required

A long lived access token for connecting to Home Assistant.

Clone this wiki locally