Home
Softono
ha-kafka-net

ha-kafka-net

Open source C#
131
Stars
1
Forks
1
Issues
5
Watchers
6 months
Last Commit

About ha-kafka-net

HaKafkaNet is a .NET 9 library for building Home Assistant automations in C. It leverages the Home Assistant Kafka integration to receive state changes, ensuring automations are durable and able to restore state across restarts. Key features include tracking and retrieving states of all Home Assistant entities, responding to state changes, and calling Home Assistant RESTful services. Automations run asynchronously and in parallel for high performance. HaKafkaNet provides strongly typed access to entities, pre-built automations through factory methods, and a fluent automation builder for quick creation of custom logic. The extensible framework allows developers to create reusable automations and extend the automation factory. HaKafkaNet is designed for full unit testability of custom automations and supports integration testing for all registered automations. Observability is built in through system monitoring, tracing with log capturing, and Open Telemetry instrumentation. A web UI is included for managing au

Platforms

Web Self-hosted Windows

Languages

C#

Links

HaKafkaNet

A library for easily creating Home Assistant automations in .NET and C#.

Kafka ensures automations are durable and state is restored between restarts.


It has been a while since there have been any updates, but rest assured, the author has this framework running stable in his home and actively uses it :)

Version 11, is released and uses .NET 9. It also introduces integration testing. See special release statement for details.


Featured on an episode of On .NET Live !

HaKafkaNet featured On .NET Live


It was created with the following goals:

  • Create Home Assistant automations in .NET / C# with abilities to:
    • track/retrieve states of all entities in Home Assistant
    • respond to Home Assistant state changes
    • call Home Assistant RESTful services
  • Enable all automation code to be fully unit testable

Example

Example of multiple durable automations. See Tutorial for more examples.

registrar.TryRegister(
    _factory.SunRiseAutomation(
        cancelToken => _api.TurnOff("light.night_light", cancelToken)),
    _factory.SunSetAutomation(
        cancelToken => _api.TurnOn("light.night_light", cancelToken),
        TimeSpan.FromMinutes(-10))
);

Resources

Why ha-kafka-net ?

What others have said:

All in all, I'm really happy HaKafkaNet is a thing! It's a really ergonomic way of writing automations that make sense.

I converted the automations for 3 rooms from Home Assistant automations to C#, and those work good! So I'm really satisfied with the project.

Dashboard

Image of dashboard This is an image of the dashboard from the example app. See UI for additional details.

How it works

  • State changes are sent from Home Assistant to a Kafka topic
  • HaKafkaNet reads all state changes
  • States for every entity are cached allowing for faster retrieval later.
    • It also allows us to have some knowledge about which events were not handled between restarts and which ones were. The framework will tell your automation about such timings to allow you to handle messages appropriately.
  • It then looks for automations which want to be notified.
    • If the entity id of the state change matches any of the TriggerEntityIds exposed by your automation, and the timing of the event matches your specified timings, then the Execute method of your automation will be called with a new Task.
    • All of your automations will be called asynchronously and in parallel.

More examples

I have made my personal repository public so that users can see working examples of some moderately complex automations.

If you have some examples you would like to share, please start a discussion. I'd be happy to link it here.

Happy Automating!