diff --git a/platformio.ini b/platformio.ini index e2bfedaa..0bc22701 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,9 +38,7 @@ lib_deps = build_flags = -D STARBASE_USERMOD_HA lib_deps = - https://github.com/dawidchyrzynski/arduino-home-assistant.git#2.0.0 - https://github.com/knolleary/pubsubclient.git#v2.8 - + https://github.com/dawidchyrzynski/arduino-home-assistant.git#2.1.0 [STARBASE] build_flags = diff --git a/src/User/UserModHA.h b/src/User/UserModHA.h index b62b07b9..c999f782 100644 --- a/src/User/UserModHA.h +++ b/src/User/UserModHA.h @@ -11,8 +11,6 @@ #include -#define BROKER_ADDR IPAddress(192,168,178,42) //ewowi: could we scan that instead of hard coded? - class UserModHA:public SysModule { public: @@ -21,34 +19,68 @@ class UserModHA:public SysModule { isEnabled = false; }; - void onStateCommand(bool state, HALight* sender) { + void setup() override { + SysModule::setup(); + + parentVar = ui->initUserMod(parentVar, name, 6300); + + ui->initText(parentVar, "mqttAddr"); + ui->initText(parentVar, "mqttUser"); + ui->initText(parentVar, "mqttPass"); + } + + static void onStateCommand(bool state, HALight* sender) { ppf("State: %s\n", state?"true":"false"); + mdl->setValue("on", state); + sender->setState(state); // report state back to the Home Assistant } - void onBrightnessCommand(unsigned8 brightness, HALight* sender) { - ppf("Brightness: %s\n", brightness); + static void onBrightnessCommand(unsigned8 brightness, HALight* sender) { + ppf("Brightness: %d\n", brightness); + + mdl->setValue("bri", brightness); sender->setBrightness(brightness); // report brightness back to the Home Assistant } - void onRGBColorCommand(HALight::RGBColor color, HALight* sender) { + static void onRGBColorCommand(HALight::RGBColor color, HALight* sender) { ppf("Red: %d Green: %d blue: %d\n", color.red, color.green, color.blue); + mdl->setValue("Red", color.red); + mdl->setValue("Green", color.green); + mdl->setValue("Blue", color.blue); + + mdl->setValue("fx", 0); // Solid + sender->setRGBColor(color); // report color back to the Home Assistant } void connectedChanged() { + ppf("connectedChanged\n"); if (mdls->isConnected) { // set device's details (optional) - device.setName(_INIT(TOSTRING(APP))); + device.setName(mdl->getValue("instance")); device.setSoftwareVersion(_INIT(TOSTRING(VERSION))); - } + } + + byte mac[6]; + WiFi.macAddress(mac); + device.setUniqueId(mac, sizeof(mac)); // configure light (optional) light->setName("LEDs"); + String options = "Solid"; + // for(int i = 0; i < eff->effects.effects.size(); i++) { + // options += ";"; + // options += eff->effects.effects.at(i)->name(); + // } + fxSelect->setOptions(options.c_str()); + + ppf("Options: %s\n", options.c_str()); + // Optionally you can set retain flag for the HA commands // light.setRetain(true); @@ -61,15 +93,45 @@ class UserModHA:public SysModule { // handle light states light->onStateCommand(onStateCommand); - light->onBrightnessCommand(onBrightnessCommand); // optional - light->onRGBColorCommand(onRGBColorCommand); // optional + light->onBrightnessCommand(onBrightnessCommand); + light->onRGBColorCommand(onRGBColorCommand); + + String mqttAddr = mdl->getValue("mqttAddr"); + String mqttUser = mdl->getValue("mqttUser"); + if(mqttUser == "null" || mqttUser == nullptr) mqttUser = ""; + String mqttPass = mdl->getValue("mqttPass"); + if(mqttPass == "null" || mqttPass == nullptr) mqttPass = ""; + + IPAddress ip; + if(ip.fromString(mqttAddr)) { + if(mqttUser == "") { + ppf("mqtt->begin('%s')\n", mqttAddr.c_str()); + mqtt->begin(ip); + } + else { + ppf("WARNING - untested mqtt->begin('%s', '%s', pass)\n", mqttAddr.c_str(), mqttUser.c_str()); + mqtt->begin(ip, mqttUser.c_str(), mqttPass.c_str()); + } + started = true; + } + else { + ppf("Failed to parse %s to IP\n", mqttAddr.c_str()); + } - mqtt->begin(BROKER_ADDR); } - void loop() { + void loop() override { // SysModule::loop(); mqtt->loop(); + light->setCurrentBrightness(mdl->getValue("bri")); + light->setCurrentState(mdl->getValue("on")); + int8_t fx = mdl->getValue("fx"); + fxSelect->setState(fx + 1); + } + + void loop10s() override { + if(!started) return; + testSensor->setValue((uint32_t) (millis() / 1000)); } private: @@ -77,6 +139,9 @@ class UserModHA:public SysModule { HADevice device; HAMqtt* mqtt = new HAMqtt(client, device); HALight* light = new HALight(_INIT(TOSTRING(APP)), HALight::BrightnessFeature | HALight::RGBFeature); + HASelect* fxSelect = new HASelect("fx"); + HASensorNumber* testSensor = new HASensorNumber("uptime"); + bool started = false; }; extern UserModHA *hamod; \ No newline at end of file