From 51fec54153da64ce58315edacfdd89bf8238fed7 Mon Sep 17 00:00:00 2001 From: Vladyslav Heneraliuk Date: Sat, 21 Dec 2024 20:12:26 +0200 Subject: [PATCH] added brightness_day and brightness_night sensors to HA --- firmware/src/JaamFirmware.cpp | 49 +++++++++++++++++++++++++----- firmware/src/JaamHomeAssistant.cpp | 48 ++++++++++++++++++++++++++++- firmware/src/JaamHomeAssistant.h | 4 +++ 3 files changed, 92 insertions(+), 9 deletions(-) diff --git a/firmware/src/JaamFirmware.cpp b/firmware/src/JaamFirmware.cpp index 956090a0..3fefb77e 100644 --- a/firmware/src/JaamFirmware.cpp +++ b/firmware/src/JaamFirmware.cpp @@ -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; @@ -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; @@ -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)); @@ -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; @@ -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!"); @@ -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, diff --git a/firmware/src/JaamHomeAssistant.cpp b/firmware/src/JaamHomeAssistant.cpp index 71205b8c..95cfc743 100644 --- a/firmware/src/JaamHomeAssistant.cpp +++ b/firmware/src/JaamHomeAssistant.cpp @@ -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]; @@ -39,6 +41,8 @@ HASensorNumber* haFreeMemory; HASensorNumber* haUsedMemory; HASensorNumber* haCpuTemp; HANumber* haBrightness; +HANumber* haBrightnessDay; +HANumber* haBrightnessNight; HASelect* haMapMode; HASelect* haDisplayMode; HASwitch* haShowHomeAlarmTime; @@ -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); @@ -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]; @@ -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; @@ -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; diff --git a/firmware/src/JaamHomeAssistant.h b/firmware/src/JaamHomeAssistant.h index 876dc23c..e58336d1 100644 --- a/firmware/src/JaamHomeAssistant.h +++ b/firmware/src/JaamHomeAssistant.h @@ -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 initDisplayModeSensor(int currentDisplayMode, const char* displayModes[], int displayModesSize, int ignoreOptions[], bool (*onChange)(int newDisplayMode), int (*transform)(int haDisplayMode)); @@ -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);