diff --git a/src/User/UserModHA.h b/src/User/UserModHA.h index 60d94394..b2733516 100644 --- a/src/User/UserModHA.h +++ b/src/User/UserModHA.h @@ -11,60 +11,47 @@ #include -#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() { @@ -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; \ No newline at end of file