From 6549f3e132982ee8ff34ee74c50b9a92981dcd82 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 | 5 ++--- src/User/UserModHA.h | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/platformio.ini b/platformio.ini index 23f20985..782363a6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -50,10 +50,9 @@ lib_deps = [STARMOD_USERMOD_HA] build_flags = -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 [Speed_Flags] diff --git a/src/User/UserModHA.h b/src/User/UserModHA.h index b2733516..85800a0b 100644 --- a/src/User/UserModHA.h +++ b/src/User/UserModHA.h @@ -17,16 +17,18 @@ class UserModHA:public SysModule { public: - UserModHA() :SysModule("Home Assistant support - template") { + UserModHA() :SysModule("Home Assistant support") { 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"); } @@ -34,19 +36,31 @@ 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)); 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()); @@ -54,15 +68,22 @@ class UserModHA:public SysModule { } - 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); + HASensorNumber* testSensor = new HASensorNumber("uptime"); + bool started = false; }; extern UserModHA *hamod; \ No newline at end of file