#include HALight::HALight(PubSubClient &client, const char *name, const char *unique_id) : HAControllableDevice(client, "light", name, unique_id) {} HALight::HALight(PubSubClient &client, const char *name, const char *unique_id, HALightController &baseLight) : HAControllableDevice(client, "light", name, unique_id), light(&baseLight) {} JSON_DOCUMENT_TYPE HALight::createConfigJSON() { JSON_DOCUMENT_TYPE doc; doc["~"] = device_topic; doc["name"] = name; doc["unique_id"] = unique_id; doc["cmd_t"] = "~/cmd"; doc["stat_t"] = "~/state"; doc["schema"] = "json"; return doc; } JSON_DOCUMENT_TYPE HALight::createStateJSON() { JSON_DOCUMENT_TYPE doc; if (light->getState()) { doc["state"] = "ON"; } else { doc["state"] = "OFF"; } return doc; } void HALight::innerHanlder(JSON_DOCUMENT_TYPE &doc) { if (strcmp(doc["state"], "ON") == 0) { light->setState(true); } else { light->setState(false); } } void HALight::sendState() { JSON_DOCUMENT_TYPE doc = createStateJSON(); buffer[0] = '\0'; serializeJson(doc, buffer, buffer_size); Serial.println(state_topic); client.publish(state_topic, buffer, true); } void HALight::sendConfig() { JSON_DOCUMENT_TYPE doc = createConfigJSON(); buffer[0] = '\0'; serializeJson(doc, buffer, buffer_size); client.publish(config_topic, buffer, true); } void HALight::handle(char *topic, byte *payload, unsigned int length) { if (strcmp(topic, command_topic) != 0) return; JSON_DOCUMENT_TYPE doc; deserializeJson(doc, (const byte *)payload, length); innerHanlder(doc); sendState(); }