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) {

View File

@@ -7,35 +7,11 @@ struct LightInfo {
std::string uniqueId;
const std::string name = "Smart RGB Light";
const std::string discoveryTopic = "homeassistant/light/smart_rgb_light/light/config";
const std::string baseTopic = "studiotj/smart-rgb/light";
const std::string availabilityTopic = "studiotj/smart-rgb/light/status";
const std::string stateTopic = "studiotj/smart-rgb/light/state";
const std::string jsonAttributesTopic = "studiotj/smart-rgb/light/attributes";
const std::string stateValueTemplate = "{{ value_json.state }}";
const std::string commandTopic = "studiotj/smart-rgb/light/state/set";
const std::string brightnessCommandTopic = "studiotj/smart-rgb/light/brightness/set";
const std::string brightnessValueTemplate = "{{ value_json.brightness }}";
const std::string colorTempCommandTopic = "studiotj/smart-rgb/light/color_temp/set";
const std::string colorTempKelvinTopic = "studiotj/smart-rgb/light/color_temp_kelvin/set";
const std::string colorTempStateTopic = "studiotj/smart-rgb/light/color_temp/state";
const std::string colorTempValueTemplate = "{{ value_json.color_temp }}";
const std::string hsCommandTopic = "studiotj/smart-rgb/light/hs/set";
const std::string hsCommandTemplate = "{{ value_json.hs_cmd }}";
const std::string hsStateTopic = "studiotj/smart-rgb/light/hs/state";
const std::string hsValueTemplate = " {{ value_json.hs_value }}";
const std::string rgbCommandTopic = "studiotj/smart-rgb/light/rgb/set";
const std::string rgbCommandTemplate = "{{ {'rgb': [red, green, blue]} | to_json }}";
const std::string rgbStateTopic = "studiotj/smart-rgb/light/rgb/state";
const std::string rgbValueTemplate = "{{ value_json.rgb | join(',') }}";
const std::string rgbwCommandTopic = "studiotj/smart-rgb/light/rgbw/set";
const std::string rgbwCommandTemplate = "{{ value_json.rgbw_cmd }}";
const std::string rgbwStateTopic = "studiotj/smart-rgb/light/rgbw/state";
const std::string rgbwValueTemplate = "{{ value_json.rgbw_value }}";
const std::string rgbwwCommandTopic = "studiotj/smart-rgb/light/rgbww/set";
const std::string rgbwwCommandTemplate = "{{ value_json.rgbww_cmd }}";
const std::string rgbwwStateTopic = "studiotj/smart-rgb/light/rgbww/state";
const std::string rgbwwValueTemplate = "{{ value_json.rgbww_value }}";
const std::string supportedColorModesTopic = "studiotj/smart-rgb/light/supported_color_modes";
const std::string supportedColorModesValue = "['rgb', 'brightness']";
const std::string availabilityTemplate = "{{ value_json.availability }}";
};
@@ -67,7 +43,8 @@ public:
Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId);
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, std::string uniqueId);
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId);
void subscribeToMqttTopics();
void notifyOnline();
void notifyOffline();
void publishInitialState();
void publishCurrentState();
void setHsl(uint8_t h, uint8_t s, uint8_t l);

View File