Hi, I’ve published an experimental release of an .NET library that hopefully makes it easier to connect .NET apps to Home Assistant via MQTT. It is not another MQTT transport layer, but a Home Assistant centric fluent configuration interface to map an application’s models to Home Assistant entities.
If this is of interest to anybody I would love feedback. Currently experimental/alpha (pre-1.0.0).
Now has support for Even entity (thanks to another who did most of the work).
It feels like it will go 1.0.0 (first stable) in September.
Example code for implementing event in application model:
public enum VolumeEventTypes
{
Mute,
LowerVolume,
RaiseVolume
}
public event EventHandler<HassEventArgs>? VolumeEvent;
// Example model method firing event
public void OnKeyPressed(char keyChar)
{
if (keyChar == '+')
{
EfEvent?.Invoke(this, HassEventArgs.From(VolumeEventTypes.RaiseVolume));
}
if (keyChar == '-')
{
EfEvent?.Invoke(this, HassEventArgs.From(VolumeEventTypes.LowerVolume));
}
if (keyChar == 'm')
{
EfEvent?.Invoke(this, HassEventArgs.From(VolumeEventTypes.Mute));
}
}
The Net2HassMqtt configuration will look something like:
device.HasEvent(config => config.OnModel(model)
.WithEvent(nameof(MyModel.VolumeEvent))
.WithEventTypes<VolumeEventTypes>()
.WithEventTypeToSendAfterEachEvent("Clear")
.WithFriendlyName("Volume control event")
.WithNodeId("volume_event"));
WithEventTypeToSendAfterEachEvent adds an event type to send immediately after each other event. Work around to prevent HASS resending events on device reconnect and HASS start-up.