From e8962cf40b98c8dba86505a25034dec947ee3405 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Tue, 23 Apr 2024 21:45:10 +0100 Subject: [PATCH] Add sample sensor --- platformio.ini | 1 - src/User/UserModHA.h | 35 ++++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index 52ae60ad..471761de 100644 --- a/platformio.ini +++ b/platformio.ini @@ -61,7 +61,6 @@ build_flags = lib_deps = https://github.com/dawidchyrzynski/arduino-home-assistant.git#2.1.0 - [STARMOD_USERMOD_WLEDAUDIO] build_flags = -D STARMOD_USERMOD_WLEDAUDIO diff --git a/src/User/UserModHA.h b/src/User/UserModHA.h index 64339822..58ec6f4a 100644 --- a/src/User/UserModHA.h +++ b/src/User/UserModHA.h @@ -19,12 +19,14 @@ class UserModHA:public SysModule { isEnabled = false; }; - void setup() { + 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) { @@ -59,11 +61,12 @@ class UserModHA:public SysModule { 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[] = {0xF1, 0x10, 0xFA, 0x6E, 0x38, 0x4A}; // TODO + byte mac[6]; + WiFi.macAddress(mac); device.setUniqueId(mac, sizeof(mac)); // configure light (optional) @@ -94,11 +97,22 @@ class UserModHA:public SysModule { 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 = ""; - ppf("mqtt->begin(%s)\n", mqttAddr.c_str()); IPAddress ip; if(ip.fromString(mqttAddr)) { - mqtt->begin(ip, "", ""); + 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()); @@ -106,7 +120,7 @@ class UserModHA:public SysModule { } - void loop() { + void loop() override { // SysModule::loop(); mqtt->loop(); light->setCurrentBrightness(mdl->getValue("bri")); @@ -115,12 +129,19 @@ class UserModHA:public SysModule { fxSelect->setState(fx + 1); } + 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); HASelect* fxSelect = new HASelect("fx"); + HASensorNumber* testSensor = new HASensorNumber("uptime"); + bool started = false; }; extern UserModHA *hamod; \ No newline at end of file