This commit is contained in:
2025-08-29 19:17:09 +02:00
parent d630a7ea5f
commit 8a76e869cb
10 changed files with 70 additions and 44 deletions

View File

@@ -21,8 +21,7 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId)
lightType = LightType::rgb;
uint8_t bits = pinR->getLedResolutionBits();
maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u;
publishInitialState();
subscribeToMqttTopics();
notifyOnline();
}
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId)
@@ -32,7 +31,7 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::strin
uint8_t bits = pinR->getLedResolutionBits();
maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u;
publishInitialState();
subscribeToMqttTopics();
notifyOnline();
}
void Light::publishInitialState() {
@@ -42,7 +41,7 @@ void Light::publishInitialState() {
deviceInfo["name"] = this->deviceInfo.name;
deviceInfo["model"] = this->deviceInfo.model;
JsonArray identifiers = deviceInfo["identifiers"].to<JsonArray>();
identifiers.add(this->deviceInfo.identifier + this->lightInfo.uniqueId);
identifiers.add(this->lightInfo.uniqueId);
deviceInfo["sw_version"] = this->deviceInfo.swVersion;
deviceInfo["manufacturer"] = this->deviceInfo.manufacturer;
@@ -159,11 +158,21 @@ void Light::operatePin() {
if (pinWW) pinWW->setLedLevel(wwSetpoint);
}
void Light::subscribeToMqttTopics() {
void Light::notifyOnline() {
Mqtt::subscribe(lightInfo.commandTopic, [this](uint8_t* payload, int length) {
std::string command(reinterpret_cast<char*>(payload), length);
handleCommand(command);
});
publishInitialState();
}
void Light::notifyOffline() {
Mqtt::unsubscribe(lightInfo.commandTopic);
JsonDocument availabilityDoc;
availabilityDoc["availability"] = Availability.notAvailable;
std::string availabilityJson;
serializeJson(availabilityDoc, availabilityJson);
Mqtt::publish(lightInfo.availabilityTopic, availabilityJson);
}
void Light::handleCommand(const std::string& command) {