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

HA: сенсори денної і нічної яскравості #250

Merged
merged 1 commit into from
Dec 21, 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
49 changes: 41 additions & 8 deletions firmware/src/JaamFirmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ void buttonUpdate(ButtonState &button, uint8_t pin, int mode, int modeLong) {
button.lastState = button.currentState;
}

void distributeBrightnessLevels() {
distributeBrightnessLevelsFor(settings.brightness_day, settings.brightness_night, ledsBrightnessLevels, "Leds");
}

bool saveBrightness(int newBrightness) {
if (settings.brightness == newBrightness) return false;
settings.brightness = newBrightness;
Expand All @@ -1109,12 +1113,42 @@ bool saveBrightness(int newBrightness) {
preferences.end();
reportSettingsChange("brightness", settings.brightness);
Serial.print("brightness commited to preferences");
Serial.println(settings.ha_light_brightness);
Serial.println(settings.brightness);
ha.setBrightness(newBrightness);
autoBrightnessUpdate();
return true;
}

bool saveDayBrightness(int newBrightness) {
if (settings.brightness_day == newBrightness) return false;
settings.brightness_day = newBrightness;
preferences.begin("storage", false);
preferences.putInt("brightness_day", settings.brightness_day);
preferences.end();
reportSettingsChange("brightness_day", settings.brightness_day);
Serial.print("brightness_day commited to preferences");
Serial.println(settings.brightness_day);
ha.setDayBrightness(newBrightness);
autoBrightnessUpdate();
distributeBrightnessLevels();
return true;
}

bool saveNightBrightness(int newBrightness) {
if (settings.brightness_night == newBrightness) return false;
settings.brightness_night = newBrightness;
preferences.begin("storage", false);
preferences.putInt("brightness_night", settings.brightness_night);
preferences.end();
reportSettingsChange("brightness_night", settings.brightness_night);
Serial.print("brightness_night commited to preferences");
Serial.println(settings.brightness_night);
ha.setNightBrightness(newBrightness);
autoBrightnessUpdate();
distributeBrightnessLevels();
return true;
}

bool saveAutoBrightnessMode(int autoBrightnessMode) {
if (settings.brightness_mode == autoBrightnessMode) return false;
settings.brightness_mode = autoBrightnessMode;
Expand Down Expand Up @@ -1492,10 +1526,6 @@ void serviceMessageUpdate() {
}
//--Display end

void distributeBrightnessLevels() {
distributeBrightnessLevelsFor(settings.brightness_day, settings.brightness_night, ledsBrightnessLevels, "Leds");
}

void updateHaTempSensors() {
if (climate.isTemperatureAvailable()) {
ha.setLocalTemperature(climate.getTemperature(settings.temp_correction));
Expand Down Expand Up @@ -2331,8 +2361,8 @@ void handleUpdate(AsyncWebServerRequest* request) {
void handleSaveBrightness(AsyncWebServerRequest *request) {
bool saved = false;
saved = saveInt(request->getParam("brightness", true), &settings.brightness, "brightness", saveBrightness) || saved;
saved = saveInt(request->getParam("brightness_day", true), &settings.brightness_day, "brd", NULL, distributeBrightnessLevels) || saved;
saved = saveInt(request->getParam("brightness_night", true), &settings.brightness_night, "brn", NULL, distributeBrightnessLevels) || saved;
saved = saveInt(request->getParam("brightness_day", true), &settings.brightness_day, "brd", saveDayBrightness) || saved;
saved = saveInt(request->getParam("brightness_night", true), &settings.brightness_night, "brn", saveNightBrightness) || saved;
saved = saveInt(request->getParam("day_start", true), &settings.day_start, "ds") || saved;
saved = saveInt(request->getParam("night_start", true), &settings.night_start, "ns") || saved;
saved = saveInt(request->getParam("brightness_auto", true), &settings.brightness_mode, "bra", saveAutoBrightnessMode) || saved;
Expand Down Expand Up @@ -3498,7 +3528,8 @@ void initUpdates() {

void initHA() {
if (shouldWifiReconnect) return;
Serial.println("Init Home assistant API");

Serial.println("Init Home assistant API");

if (!ha.initDevice(settings.ha_brokeraddress, settings.devicename, currentFwVersion, settings.devicedescription, chipID)) {
Serial.println("Home Assistant is not available!");
Expand All @@ -3511,6 +3542,8 @@ void initHA() {
ha.initUsedMemorySensor();
ha.initCpuTempSensor(temperatureRead());
ha.initBrightnessSensor(settings.brightness, saveBrightness);
ha.initDayBrightnessSensor(settings.brightness_day, saveDayBrightness);
ha.initNightBrightnessSensor(settings.brightness_night, saveNightBrightness);
ha.initMapModeSensor(settings.map_mode, MAP_MODES, MAP_MODES_COUNT, saveMapMode);
if (display.isDisplayAvailable()) {
displayModeHAMap = ha.initDisplayModeSensor(getLocalDisplayMode(settings.display_mode, ignoreDisplayModeOptions), DISPLAY_MODES,
Expand Down
48 changes: 47 additions & 1 deletion firmware/src/JaamHomeAssistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ char haFreeMemoryID[25];
char haUsedMemoryID[25];
char haCpuTempID[22];
char haBrightnessID[24];
char haBrightnessDayID[28];
char haBrightnessNightID[30];
char haMapModeID[22];
char haDisplayModeID[26];
char haAutoAlarmModeID[25];
Expand All @@ -39,6 +41,8 @@ HASensorNumber* haFreeMemory;
HASensorNumber* haUsedMemory;
HASensorNumber* haCpuTemp;
HANumber* haBrightness;
HANumber* haBrightnessDay;
HANumber* haBrightnessNight;
HASelect* haMapMode;
HASelect* haDisplayMode;
HASwitch* haShowHomeAlarmTime;
Expand All @@ -62,6 +66,8 @@ HASwitch* haNightMode;
const char* mqttServer;

bool (*brightnessChanged)(int newBrightness);
bool (*brightnessDayChanged)(int newBrightness);
bool (*brightnessNightChanged)(int newBrightness);
bool (*mapModeChanged)(int newMapMode);
bool (*displayModeChanged)(int newDisplayMode);
int (*displayModeTransform)(int haDisplayMode);
Expand All @@ -80,7 +86,7 @@ void (*onMqqtConnectionStatusChanged)(bool connected);
char configUrl[30];
byte macAddress[6];

#define SENSORS_COUNT 26
#define SENSORS_COUNT 28

char deviceUniqueID[15];

Expand Down Expand Up @@ -243,6 +249,32 @@ void JaamHomeAssistant::initBrightnessSensor(int currentBrightness, bool (*onCha
#endif
}

void JaamHomeAssistant::initDayBrightnessSensor(int currentBrightness, bool (*onChange)(int newBrightness)) {
#if HA_ENABLED
if (!haEnabled) return;
sprintf(haBrightnessDayID, "%s_brightness_day", deviceUniqueID);
haBrightnessDay = new HANumber(haBrightnessDayID);
brightnessDayChanged = onChange;
haBrightnessDay->onCommand([](HANumeric number, HANumber* sender) { brightnessDayChanged(number.toUInt8()); });
haBrightnessDay->setIcon("mdi:brightness-5");
haBrightnessDay->setName("Brightness Day");
haBrightnessDay->setCurrentState(currentBrightness);
#endif
}

void JaamHomeAssistant::initNightBrightnessSensor(int currentBrightness, bool (*onChange)(int newBrightness)) {
#if HA_ENABLED
if (!haEnabled) return;
sprintf(haBrightnessNightID, "%s_brightness_night", deviceUniqueID);
haBrightnessNight = new HANumber(haBrightnessNightID);
brightnessNightChanged = onChange;
haBrightnessNight->onCommand([](HANumeric number, HANumber* sender) { brightnessNightChanged(number.toUInt8()); });
haBrightnessNight->setIcon("mdi:brightness-4");
haBrightnessNight->setName("Brightness Night");
haBrightnessNight->setCurrentState(currentBrightness);
#endif
}

void JaamHomeAssistant::initMapModeSensor(int currentMapMode, const char* mapModes[], int mapModesSize, bool (*onChange)(int newMapMode)) {
#if HA_ENABLED
if (!haEnabled) return;
Expand Down Expand Up @@ -560,6 +592,20 @@ void JaamHomeAssistant::setBrightness(int brightness) {
#endif
}

void JaamHomeAssistant::setDayBrightness(int brightness) {
#if HA_ENABLED
if (!haEnabled) return;
haBrightnessDay->setState(brightness);
#endif
}

void JaamHomeAssistant::setNightBrightness(int brightness) {
#if HA_ENABLED
if (!haEnabled) return;
haBrightnessNight->setState(brightness);
#endif
}

void JaamHomeAssistant::setMapMode(int mapMode) {
#if HA_ENABLED
if (!haEnabled) return;
Expand Down
4 changes: 4 additions & 0 deletions firmware/src/JaamHomeAssistant.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class JaamHomeAssistant {
void initFreeMemorySensor();
void initUsedMemorySensor();
void initBrightnessSensor(int currentBrightness, bool (*onChange)(int newBrightness));
void initDayBrightnessSensor(int currentBrightness, bool (*onChange)(int newBrightness));
void initNightBrightnessSensor(int currentBrightness, bool (*onChange)(int newBrightness));
void initMapModeSensor(int currentMapMode, const char* mapModes[], int mapModesSize, bool (*onChange)(int newMapMode));
std::map<int, int> initDisplayModeSensor(int currentDisplayMode, const char* displayModes[], int displayModesSize, int ignoreOptions[],
bool (*onChange)(int newDisplayMode), int (*transform)(int haDisplayMode));
Expand Down Expand Up @@ -51,6 +53,8 @@ class JaamHomeAssistant {
void setFreeMemory(int freeMemory);
void setUsedMemory(int usedMemory);
void setBrightness(int brightness);
void setDayBrightness(int brightness);
void setNightBrightness(int brightness);
void setMapMode(int mapMode);
void setDisplayMode(int displayMode);
void setMapModeCurrent(const char* mapMode);
Expand Down
Loading