diff --git a/include/IRLight.h b/include/IRLight.h index 9a44245..bf72dbe 100644 --- a/include/IRLight.h +++ b/include/IRLight.h @@ -3,12 +3,13 @@ #include #include #include -#include +#include -class IRLight: public HALightBrightnessController { +class IRLight: public HALightColorTemperatureBrightnessController { private: bool state; short brightness; + byte color_temp; IRsend *irsend; @@ -28,7 +29,7 @@ private: 680, 470, 580, 1620, 630, 470, 630, 1570, 630, 520, 580, 520, 630, 1570, 680, 1520, 730, 1520, 680}; - const uint16_t RDCentralButton[67] = { + const uint16_t RDButtonCenter[67] = { 9030, 4320, 680, 470, 630, 470, 680, 420, 680, 470, 630, 470, 680, 420, 680, 470, 630, 1570, 680, 1520, 680, 1520, 680, 1520, 730, 1520, 680, 1520, 680, 1520, 680, 1570, 680, 420, 680, 1520, @@ -51,9 +52,13 @@ private: 630, 470, 630, 1570, 680, 470, 630, 470, 630, 470, 680, 470, 630, 1570, 630, 1570, 680, 1570, 630}; + const uint16_t RDButtonA[67] = {8980,4370, 630,470, 680,470, 630,470, 630,470, 630,520, 630,470, 630,470, 630,1620, 630,1570, 630,1620, 580,1570, 630,1620, 630,1570, 630,1570, 630,1570, 680,470, 630,1570, 630,1570, 630,1620, 630,470, 630,470, 630,470, 680,470, 630,470, 630,470, 680,470, 630,470, 630,1570, 630,1620, 630,1570, 630,1570, 630,1570, 630}; + + const uint16_t RDButtonB[67] = {9030,4320, 630,520, 580,520, 630,470, 630,470, 680,470, 580,520, 630,470, 680,1570, 630,1570, 630,1570, 630,1570, 630,1620, 580,1620, 630,1570, 630,1620, 580,520, 580,520, 630,470, 680,470, 630,1570, 630,470, 630,520, 580,520, 630,470, 630,1620, 630,1570, 630,1570, 630,470, 680,1570, 630,1570, 630,1570, 630,1570, 680}; + public: - IRLight(int pin); - IRLight(IRsend *irsend); + explicit IRLight(int pin); + explicit IRLight(IRsend *irsend); void setState(bool state); bool getState(); @@ -63,6 +68,9 @@ public: void setBrightness(int brightness); int getBrightness(); + void setColorTemperature(int color); + int getColorTemperature(); + void brightnessUp(); void brightnessDown(); int getBrightnessScale(); diff --git a/lib/HomeAssistantDevices/HALight.cpp b/lib/HomeAssistantDevices/HALight.cpp index 842b830..4bb2093 100644 --- a/lib/HomeAssistantDevices/HALight.cpp +++ b/lib/HomeAssistantDevices/HALight.cpp @@ -18,6 +18,12 @@ JSON_DOCUMENT_TYPE HALight::createConfigJSON() { doc["stat_t"] = "~/state"; doc["schema"] = "json"; + patchConfigJSON(doc); + + return doc; +} + +JSON_DOCUMENT_TYPE HALight::patchConfigJSON(JSON_DOCUMENT_TYPE &doc) { return doc; } @@ -30,10 +36,16 @@ JSON_DOCUMENT_TYPE HALight::createStateJSON() { doc["state"] = "OFF"; } + patchStateJSON(doc); + return doc; } -void HALight::innerHanlder(JSON_DOCUMENT_TYPE &doc) { +JSON_DOCUMENT_TYPE HALight::patchStateJSON(JSON_DOCUMENT_TYPE &doc) { + return doc; +} + +void HALight::innerHandler(const JSON_DOCUMENT_TYPE &doc) { if (strcmp(doc["state"], "ON") == 0) { light->setState(true); } else { @@ -65,7 +77,7 @@ void HALight::handle(char *topic, byte *payload, unsigned int length) { JSON_DOCUMENT_TYPE doc; deserializeJson(doc, (const byte *)payload, length); - innerHanlder(doc); + innerHandler(doc); sendState(); } \ No newline at end of file diff --git a/lib/HomeAssistantDevices/HALight.hpp b/lib/HomeAssistantDevices/HALight.hpp index fbc3b39..534ac83 100644 --- a/lib/HomeAssistantDevices/HALight.hpp +++ b/lib/HomeAssistantDevices/HALight.hpp @@ -16,13 +16,15 @@ private: protected: virtual JSON_DOCUMENT_TYPE createConfigJSON(); + virtual JSON_DOCUMENT_TYPE patchConfigJSON(JSON_DOCUMENT_TYPE &doc); virtual JSON_DOCUMENT_TYPE createStateJSON(); - virtual void innerHanlder(JSON_DOCUMENT_TYPE &doc); + virtual JSON_DOCUMENT_TYPE patchStateJSON(JSON_DOCUMENT_TYPE &doc); + virtual void innerHandler(const JSON_DOCUMENT_TYPE &doc); HALight(PubSubClient &client, const char *name, const char *unique_id); public: HALight(PubSubClient &client, const char *name, const char *unique_id, HALightController &baseLight); - void handle(char *topic, byte *payload, unsigned int length); - void sendConfig(); - void sendState(); + void handle(char *topic, byte *payload, unsigned int length) override; + void sendConfig() override; + void sendState() override; }; \ No newline at end of file diff --git a/lib/HomeAssistantDevices/HALightBrightness.cpp b/lib/HomeAssistantDevices/HALightBrightness.cpp index 864e491..dfb23b8 100644 --- a/lib/HomeAssistantDevices/HALightBrightness.cpp +++ b/lib/HomeAssistantDevices/HALightBrightness.cpp @@ -3,35 +3,28 @@ HALightBrightness::HALightBrightness(PubSubClient &client, const char *name, const char *unique_id, HALightBrightnessController &baseLight) - : HALight(client, "light", name, baseLight), - light(&baseLight) {} + : HALight(client, name, unique_id, baseLight), light(&baseLight) {} -JSON_DOCUMENT_TYPE HALightBrightness::createConfigJSON() { - JSON_DOCUMENT_TYPE doc = HALight::createConfigJSON(); - +JSON_DOCUMENT_TYPE HALightBrightness::patchConfigJSON(JSON_DOCUMENT_TYPE &doc) { doc["brightness"] = true; doc["brightness_scale"] = light->getBrightnessScale(); - // doc["color_mode"] = true; - // doc["supported_color_modes"] = - - // JSON_DOCUMENT_TYPE doc; return doc; } -JSON_DOCUMENT_TYPE HALightBrightness::createStateJSON() { - JSON_DOCUMENT_TYPE doc = HALight::createStateJSON(); - +JSON_DOCUMENT_TYPE HALightBrightness::patchStateJSON(JSON_DOCUMENT_TYPE &doc) { doc["brightness"] = light->getBrightness(); - - // JSON_DOCUMENT_TYPE doc; return doc; } -void HALightBrightness::innerHanlder(JSON_DOCUMENT_TYPE &doc) { +void HALightBrightness::handleBrightness(const JSON_DOCUMENT_TYPE &doc) { if (doc.containsKey("brightness")) { light->setBrightness(doc["brightness"]); } +} + +void HALightBrightness::innerHandler(const JSON_DOCUMENT_TYPE &doc) { + handleBrightness(doc); delay(50); - HALight::innerHanlder(doc); + HALight::innerHandler(doc); } \ No newline at end of file diff --git a/lib/HomeAssistantDevices/HALightBrightness.hpp b/lib/HomeAssistantDevices/HALightBrightness.hpp index 21ee0f0..e0f4120 100644 --- a/lib/HomeAssistantDevices/HALightBrightness.hpp +++ b/lib/HomeAssistantDevices/HALightBrightness.hpp @@ -2,23 +2,25 @@ #include -class HALightBrightnessController: public HALightController { +class HALightBrightnessController: public virtual HALightController { public: virtual void setBrightness(int value) = 0; virtual int getBrightness() = 0; - virtual int getBrightnessScale() = 0; }; -class HALightBrightness : public HALight { +class HALightBrightness : public virtual HALight { private: HALightBrightnessController *light; protected: - JSON_DOCUMENT_TYPE createConfigJSON() override; - JSON_DOCUMENT_TYPE createStateJSON() override; - void innerHanlder(JSON_DOCUMENT_TYPE &doc) override; + virtual JSON_DOCUMENT_TYPE patchConfigJSON(JSON_DOCUMENT_TYPE &doc) override; + virtual JSON_DOCUMENT_TYPE patchStateJSON(JSON_DOCUMENT_TYPE &doc) override; + virtual void innerHandler(const JSON_DOCUMENT_TYPE &doc) override; + + void handleBrightness(const JSON_DOCUMENT_TYPE &doc); + public: HALightBrightness(PubSubClient &client, const char *name, const char *unique_id, HALightBrightnessController &baseLight); diff --git a/lib/HomeAssistantDevices/HALightColorTemperature.cpp b/lib/HomeAssistantDevices/HALightColorTemperature.cpp new file mode 100644 index 0000000..66cb3ff --- /dev/null +++ b/lib/HomeAssistantDevices/HALightColorTemperature.cpp @@ -0,0 +1,28 @@ +#include + +HALightColorTemperature::HALightColorTemperature(PubSubClient &client, const char *name, const char *unique_id, + HALightColorTemperatureController &baseLight) + : HALight(client, "light", name, baseLight), + light(&baseLight) {} + +JSON_DOCUMENT_TYPE HALightColorTemperature::patchConfigJSON(JSON_DOCUMENT_TYPE &doc) { + doc["color_temp"] = true; + return doc; +} + +JSON_DOCUMENT_TYPE HALightColorTemperature::patchStateJSON(JSON_DOCUMENT_TYPE &doc) { + doc["color_temp"] = light->getColorTemperature(); + return doc; +} + +void HALightColorTemperature::handleColorTemperature(const JSON_DOCUMENT_TYPE &doc) { + if (doc.containsKey("color_temp")) { + light->setColorTemperature(doc["color_temp"]); + } +} + +void HALightColorTemperature::innerHandler(const JSON_DOCUMENT_TYPE &doc) { + handleColorTemperature(doc); + delay(50); + HALight::innerHandler(doc); +} \ No newline at end of file diff --git a/lib/HomeAssistantDevices/HALightColorTemperature.hpp b/lib/HomeAssistantDevices/HALightColorTemperature.hpp new file mode 100644 index 0000000..2de9fcb --- /dev/null +++ b/lib/HomeAssistantDevices/HALightColorTemperature.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include + +class HALightColorTemperatureController: public virtual HALightController { +public: + virtual void setColorTemperature(int value) = 0; + virtual int getColorTemperature() = 0; + + virtual int getBrightnessScale() = 0; +}; + +class HALightColorTemperature : public virtual HALight { +private: + HALightColorTemperatureController *light; + +protected: + virtual JSON_DOCUMENT_TYPE patchConfigJSON(JSON_DOCUMENT_TYPE &doc) override; + virtual JSON_DOCUMENT_TYPE patchStateJSON(JSON_DOCUMENT_TYPE &doc) override; + virtual void innerHandler(const JSON_DOCUMENT_TYPE &doc) override; + + void handleColorTemperature(const JSON_DOCUMENT_TYPE &doc); +public: + HALightColorTemperature(PubSubClient &client, const char *name, const char *unique_id, + HALightColorTemperatureController &baseLight); +}; \ No newline at end of file diff --git a/lib/HomeAssistantDevices/HALightColorTemperatureBrightness.cpp b/lib/HomeAssistantDevices/HALightColorTemperatureBrightness.cpp new file mode 100644 index 0000000..1f006c8 --- /dev/null +++ b/lib/HomeAssistantDevices/HALightColorTemperatureBrightness.cpp @@ -0,0 +1,34 @@ +#include + +HALightColorTemperatureBrightness::HALightColorTemperatureBrightness( + PubSubClient &client, + const char *name, + const char *unique_id, + HALightColorTemperatureBrightnessController &baseLight +) : + HALightBrightness(client, name, unique_id, baseLight), + HALightColorTemperature(client, name, unique_id, baseLight), + HALight(client, name, unique_id, baseLight), + light(&baseLight) +{ +} + +JSON_DOCUMENT_TYPE HALightColorTemperatureBrightness::patchConfigJSON(JSON_DOCUMENT_TYPE &doc) { + HALightBrightness::patchConfigJSON(doc); + HALightColorTemperature::patchConfigJSON(doc); + return doc; +} + +JSON_DOCUMENT_TYPE HALightColorTemperatureBrightness::patchStateJSON(JSON_DOCUMENT_TYPE &doc) { + HALightBrightness::patchStateJSON(doc); + HALightColorTemperature::patchStateJSON(doc); + return doc; +} + +void HALightColorTemperatureBrightness::innerHandler(const JSON_DOCUMENT_TYPE &doc) { + handleBrightness(doc); + delay(50); + handleColorTemperature(doc); + delay(50); + HALight::innerHandler(doc); +} \ No newline at end of file diff --git a/lib/HomeAssistantDevices/HALightColorTemperatureBrightness.hpp b/lib/HomeAssistantDevices/HALightColorTemperatureBrightness.hpp new file mode 100644 index 0000000..53527b0 --- /dev/null +++ b/lib/HomeAssistantDevices/HALightColorTemperatureBrightness.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +class HALightColorTemperatureBrightnessController: + public HALightBrightnessController, + public HALightColorTemperatureController +{ +}; + +class HALightColorTemperatureBrightness : + public HALightBrightness, + public HALightColorTemperature +{ +private: + HALightColorTemperatureBrightnessController *light; + +protected: + JSON_DOCUMENT_TYPE patchConfigJSON(JSON_DOCUMENT_TYPE &doc) override; + JSON_DOCUMENT_TYPE patchStateJSON(JSON_DOCUMENT_TYPE &doc) override; + virtual void innerHandler(const JSON_DOCUMENT_TYPE &doc) override; +public: + HALightColorTemperatureBrightness(PubSubClient &client, const char *name, const char *unique_id, + HALightColorTemperatureBrightnessController &baseLight); +}; \ No newline at end of file diff --git a/lib/HomeAssistantDevices/HomeAssistantDevices.hpp b/lib/HomeAssistantDevices/HomeAssistantDevices.hpp index 75533e5..c6c978c 100644 --- a/lib/HomeAssistantDevices/HomeAssistantDevices.hpp +++ b/lib/HomeAssistantDevices/HomeAssistantDevices.hpp @@ -1,4 +1,6 @@ #pragma once #include -#include \ No newline at end of file +#include +#include +#include \ No newline at end of file diff --git a/src/IRLight.cpp b/src/IRLight.cpp index 7158b90..952b5cf 100644 --- a/src/IRLight.cpp +++ b/src/IRLight.cpp @@ -42,6 +42,35 @@ void IRLight::setBrightness(int newBrightness) { brightness = newBrightness; } +void IRLight::setColorTemperature(int color) { + if (color < 208) { + color_temp = 2; + brightness = 14; + irsend->sendRaw(RDButtonB, 67, 38); + } else if (color < 294) { + color_temp = 1; + brightness = 14; + irsend->sendRaw(RDButtonCenter, 67, 38); + } else { + color_temp = 0; + brightness = 14; + irsend->sendRaw(RDButtonA, 67, 38); + } +} + +int IRLight::getColorTemperature() { + switch (color_temp) + { + case 0: + return 370; + case 1: + return 262; + case 2: + return 154; + } + return 0; +} + void IRLight::brightnessUp() { if (brightness < getBrightnessScale()) { brightness++; @@ -58,8 +87,9 @@ void IRLight::brightnessDown() { void IRLight::begin() { irsend->begin(); - irsend->sendRaw(RDCentralButton, 67, 38); brightness = 14; + color_temp = 1; + irsend->sendRaw(RDButtonCenter, 67, 38); delay(250); off(); } diff --git a/src/main.cpp b/src/main.cpp index 6eb9d8d..c4ab566 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,8 +55,9 @@ PubSubClient client(espClient); DHT dht(DHTPIN, DHTTYPE); IRLight irlight(4); -HALightBrightness halight(client, "light", "l-1", irlight); +HALightColorTemperatureBrightness halight(client, "light", "l-1", irlight); +void setup_devices(); void setup_wifi(); void setup_time(); void setup_mqtt(); @@ -76,9 +77,7 @@ void setup() Serial.begin(115200); delay(1000); - irlight.begin(); - dht.begin(); - + setup_devices(); setup_wifi(); setup_time(); setup_mqtt(); @@ -110,6 +109,12 @@ void loop() delay(100); } +void setup_devices() +{ + irlight.begin(); + dht.begin(); +} + void setup_wifi() { delay(10); @@ -192,8 +197,6 @@ void reconnect() } } -const int unused = 0; - void mqtt_callback(char *topic, byte *payload, unsigned int length) { Serial.print("Message arrived [");