Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/HomeAssistant'
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoudwijma committed Jun 7, 2024
2 parents dcb3e66 + 6549f3e commit bf9b7cb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
6 changes: 3 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
81 changes: 44 additions & 37 deletions src/User/UserModHA.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include <ArduinoHA.h>

#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:
Expand All @@ -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;

0 comments on commit bf9b7cb

Please sign in to comment.