Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home Assistant #52

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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;
Loading