Blog Logo
TAGS

Integration that uses Home Assistant Kafka integration for creating home automations in .NET

# ha-kafka-net Integration that uses Home Assistant Kafka integration for creating home automations in .NET It was created with the following goals: * Create Home Assistant automations in .NET * Expose a simple way to track states of all entities in Home Assistant * Expose a simple way to respond to Home Assistant state changes * Provide a means to call Home Assistant RESTful services * Enable all automation code to be fully unit testable Nuget package can be found [here](https://www.nuget.org/packages/HaKafkaNet/). Full documentation [here](https://github.com/leosperry/ha-kafka-net/wiki) ## Why ha-kafka-net ? * Kafka allows you to replay events. Therefore, when your application starts, it can quickly load the states of all your Home Assistant entities. * It gives you a easy-to-spin up infrastructure with management features out of the box. This includes docker images for both managing kafka and seeing the state of your consumers based on open source projects * You have an easy way to respond to events during start up which means you are guarenteed to see/handle all events at least once, even if your application has been down. * Full unit testability * MIT license ## How it works * Events are streamed from Home Assistant to the `home_assistant` topic. Unfortunately, the key is not utilizied by the provided home assistant kafka integration. Please upvote [this feature request](https://community.home-assistant.io/t/set-key-in-kafka-topic/671757/2) * The transformer reads the messages and then adds them to the `home_assistant_states` topic with the entity id set as a key. - This allows us to compact the topic and make some assurances about order. * A second consumer called the state handler reads from `home_assistant_states` topic and caches all state changes exposed by home assistant to Redis. - This allows for faster retrieval later and minimizes our application memory footprint. 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`. - It is up to the consumer to handle any errors. The framework prioritizes handling new messages speedily over tracking the state of individual automations. If your automation erros it will only write an ILogger message indicating the error. ## Current steps for Example App set up: 1. Follow instructions [here](https://github.com/leosperry/ha-kafka-net/wiki/Setup-Instructions) for setting up your environment. 2. In your HomeAssistant UI, create helper buttons named: - `Test Button` - `Test Button 2` - `Test Button 3` 2. Look through the provided examples for IDs of lights to set to match your environment. 3. Click your test buttons both while your application is up and while it is down to see different behaviors at starup. ## Coming soon * More pre-built automations. * More Documentation * More enhanced HA API functionality ## Tips * You can optionally add this repository as a submodule to your own instead of using the nuget package. * During start up, it can take a minute or two for it to churn though thousands of events. In the