Skip to content

Commit

Permalink
Swap to UserModHA to basic template, suitable for StartMod(core)
Browse files Browse the repository at this point in the history
  • Loading branch information
netmindz committed Apr 23, 2024
1 parent 6aadd03 commit 1ac0699
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions src/User/UserModHA.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,47 @@

#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:

UserModHA() :SysModule("Home Assistant support") {
UserModHA() :SysModule("Home Assistant support - template") {
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() {
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");
}

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.setSoftwareVersion(_INIT(TOSTRING(VERSION)));
}

// configure light (optional)
light->setName("LEDs");
byte mac[] = {0xF1, 0x10, 0xFA, 0x6E, 0x38, 0x4A}; // TODO
device.setUniqueId(mac, sizeof(mac));

// Optionally you can set retain flag for the HA commands
// light.setRetain(true);
String mqttAddr = mdl->getValue("mqttAddr");

// 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
ppf("mqtt->begin(%s)\n", mqttAddr.c_str());
IPAddress ip;
if(ip.fromString(mqttAddr)) {
mqtt->begin(ip, "", "");
}
else {
ppf("Failed to parse %s to IP\n", mqttAddr.c_str());
}

mqtt->begin(BROKER_ADDR);
}

void loop() {
Expand All @@ -76,7 +63,6 @@ class UserModHA:public SysModule {
WiFiClient client;
HADevice device;
HAMqtt* mqtt = new HAMqtt(client, device);
HALight* light = new HALight(_INIT(TOSTRING(APP)), HALight::BrightnessFeature | HALight::RGBFeature);
};

extern UserModHA *hamod;

0 comments on commit 1ac0699

Please sign in to comment.