diff --git a/platformio.ini b/platformio.ini index d938bd80..00029787 100644 --- a/platformio.ini +++ b/platformio.ini @@ -36,10 +36,10 @@ lib_deps = [STARBASE_USERMOD_HA] build_flags = - -D STARBASE_USERMOD_HA + -D STARMOD_USERMOD_HA + -D ARDUINOHA_DEBUG 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] diff --git a/src/User/UserModHA.h b/src/User/UserModHA.h index b62b07b9..2bfffb9e 100644 --- a/src/User/UserModHA.h +++ b/src/User/UserModHA.h @@ -11,8 +11,8 @@ #include -#define BROKER_ADDR IPAddress(192,168,178,42) //ewowi: could we scan that instead of hard coded? - +// Basic example of MQTT connectivity to Home Assistant. +// Add HALight, HASelect etc as required class UserModHA:public SysModule { public: @@ -21,62 +21,69 @@ class UserModHA:public SysModule { isEnabled = false; }; - void onStateCommand(bool state, HALight* sender) { - ppf("State: %s\n", state?"true":"false"); - - sender->setState(state); // report state back to the Home Assistant - } + void setup() override { + SysModule::setup(); - void onBrightnessCommand(unsigned8 brightness, HALight* sender) { - ppf("Brightness: %s\n", brightness); + parentVar = ui->initUserMod(parentVar, name, 6300); - sender->setBrightness(brightness); // report brightness back to the Home Assistant + ui->initText(parentVar, "mqttAddr"); + ui->initText(parentVar, "mqttUser"); + ui->initText(parentVar, "mqttPass"); } - void onRGBColorCommand(HALight::RGBColor color, HALight* sender) { - ppf("Red: %d Green: %d blue: %d\n", color.red, color.green, color.blue); - - 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)); + + 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()); } - // configure light (optional) - light->setName("LEDs"); - - // Optionally you can set retain flag for the HA commands - // light.setRetain(true); - - // Maximum brightness level can be changed as follows: - // light.setBrightnessScale(50); - - // Optionally you can enable optimistic mode for the HALight. - // In this mode you won't need to report state back to the HA when commands are executed. - // light.setOptimistic(true); - - // handle light states - light->onStateCommand(onStateCommand); - light->onBrightnessCommand(onBrightnessCommand); // optional - light->onRGBColorCommand(onRGBColorCommand); // optional - - mqtt->begin(BROKER_ADDR); } - void loop() { + void loop() override { // SysModule::loop(); mqtt->loop(); } + void loop10s() override { + if(!started) return; + testSensor->setValue((uint32_t) (millis() / 1000)); + } + private: WiFiClient client; HADevice device; HAMqtt* mqtt = new HAMqtt(client, device); - HALight* light = new HALight(_INIT(TOSTRING(APP)), HALight::BrightnessFeature | HALight::RGBFeature); + HASensorNumber* testSensor = new HASensorNumber("uptime"); + bool started = false; }; extern UserModHA *hamod; \ No newline at end of file