Skip to content

🏠 Home Assistant MQTT format support for embedded systems

Notifications You must be signed in to change notification settings

MyrtIO/home-assistant-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Home Assistant JSON

Library for (de)serializing Home Assistant MQTT JSON format.

The library provides a set of classes that can be used to implement MQTT control support with Home Assistant without additional configuration. The classes are able to parse and serialize data, as well as automatically generate topic names.

Usage

#include <PubSubServer.h>
#include <HomeAssistantJSON/light_entity.h>
// Your internal platforms
#include <platforms/platforms.h>

HomeAssistant::Device device = HomeAssistant::Device({
    .name = "My Device",
    .id = "my_device",
    .mqttNamespace = "my/device"
});
HomeAssistant::LightEntity lightEntity({
    .name = "Light",
	.identifier = "light",
	.icon = "mdi:led-strip-variant",
    .effects = nullptr,
    .effectCount = 0,
	.writable = true,
	.maxMireds = 400,
	.minMireds = 120
}, device);

ILightPlatform* light;
HomeAssistant::LightState state;

void reportLightConfig(PubSubClient* client) {
	lightEntity.serializeConfig(responseBuffer);
	client->publish(lightEntity.configTopic(), responseBuffer);
}

void reportLightState(PubSubClient* client) {
	state.enabled = light->getPower();
	// ...
	lightEntity.serializeValue(responseBuffer, state);
	client->publish(lightEntity.stateTopic(), responseBuffer);
}

void updateLightState(PubSubClient* client, byte* payload, uint length) {
	lightEntity.parseValue(state, payload);
	// Update platform with new state
	light->setPower(state.enabled);
	// ...
}

void registerLightTopics(PubSubServer* server) {
	light = IO_INJECT(ILightPlatform);
	lightEntity.setEffects(light->effects(), light->effectCount());
	server
		->report(reportLightConfig, MQTT_CONFIG_REPORT_INTERVAL)
		->report(reportLightState, MQTT_STATE_REPORT_INTERVAL)
		->on(lightEntity.commandTopic(), updateLightState);
}

About

🏠 Home Assistant MQTT format support for embedded systems

Topics

Resources

Stars

Watchers

Forks

Languages