Skip to content

Commit

Permalink
Add sample sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
netmindz committed Apr 23, 2024
1 parent 29d86ff commit e8962cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
1 change: 0 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 28 additions & 7 deletions src/User/UserModHA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -94,19 +97,30 @@ 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());
}

}

void loop() {
void loop() override {
// SysModule::loop();
mqtt->loop();
light->setCurrentBrightness(mdl->getValue("bri"));
Expand All @@ -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;

0 comments on commit e8962cf

Please sign in to comment.