diff --git a/lib/light/light.cpp b/lib/light/light.cpp index 4434844..650a8ac 100644 --- a/lib/light/light.cpp +++ b/lib/light/light.cpp @@ -16,8 +16,8 @@ const struct { Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId) - : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr) { - lightInfo.uniqueId = uniqueId; + : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr), lightInfo(uniqueId) { + lightType = LightType::rgb; uint8_t bits = pinR->getLedResolutionBits(); maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u; @@ -25,8 +25,7 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId) } Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId) - : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW) { - lightInfo.uniqueId = uniqueId; + : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), lightInfo(uniqueId) { lightType = LightType::rgbww; uint8_t bits = pinR->getLedResolutionBits(); maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u; diff --git a/lib/light/light.h b/lib/light/light.h index 96ce2b6..06ea36d 100644 --- a/lib/light/light.h +++ b/lib/light/light.h @@ -4,14 +4,32 @@ #include "pin.h" struct LightInfo { + LightInfo() = default; + LightInfo(const std::string& id) { + uniqueId = id; + updateTopics(); + } + void setUniqueId(const std::string& id) { + uniqueId = id; + updateTopics(); + } + void updateTopics() { + discoveryTopic = discoveryTopicBase + uniqueId + "/config"; + availabilityTopic = topicBase + uniqueId + "/availability"; + stateTopic = topicBase + uniqueId + "/state"; + jsonAttributesTopic = topicBase + uniqueId + "/attributes"; + commandTopic = topicBase + uniqueId + "/state/set"; + } std::string uniqueId; const std::string name = "Smart RGB Light"; - const std::string discoveryTopic = "homeassistant/light/smart_rgb_light/light/config"; - 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 discoveryTopicBase = "homeassistant/light/"; + std::string discoveryTopic = discoveryTopicBase + uniqueId + "/config"; + const std::string topicBase = "studiotj/"; + std::string availabilityTopic = topicBase + uniqueId + "/availability"; + std::string stateTopic = topicBase + uniqueId + "/state"; + std::string jsonAttributesTopic = topicBase + uniqueId + "/attributes"; + std::string stateValueTemplate = "{{ value_json.state }}"; + std::string commandTopic = topicBase + uniqueId + "/state/set"; const std::string supportedColorModesValue = "['rgb', 'brightness']"; const std::string availabilityTemplate = "{{ value_json.availability }}"; };