diff --git a/src/firmware/Constants.h b/src/firmware/Constants.h index 581ac6cf..16243a96 100644 --- a/src/firmware/Constants.h +++ b/src/firmware/Constants.h @@ -197,7 +197,7 @@ static char* DISPLAY_MODES[DISPLAY_MODE_OPTIONS_MAX] = { }; #define AUTO_ALARM_MODES_COUNT 3 -static char* AUTO_ALRMS_MODES[AUTO_ALARM_MODES_COUNT] = { +static char* AUTO_ALARM_MODES[AUTO_ALARM_MODES_COUNT] = { "Вимкнено", "Домашній та суміжні", "Лише домашній" diff --git a/src/firmware/JaamHomeAssistant.cpp b/src/firmware/JaamHomeAssistant.cpp index 4057a4c4..ee69a0bc 100644 --- a/src/firmware/JaamHomeAssistant.cpp +++ b/src/firmware/JaamHomeAssistant.cpp @@ -115,8 +115,11 @@ bool haEnabled = false; JaamHomeAssistant::JaamHomeAssistant() { } -void JaamHomeAssistant::initDevice(const char* deviceName, const char* currentFwVersion, const char* deviceDescription) { +bool JaamHomeAssistant::initDevice(const char* mqttServerIp, const char* deviceName, const char* currentFwVersion, const char* deviceDescription, const char* chipID) { #if HA_ENABLED + haEnabled = mqttServer.fromString(mqttServerIp); + if (!haEnabled) return false; + strcpy(deviceUniqueID, chipID); WiFi.macAddress(macAddress); sprintf(configUrl, "http://%s:80", WiFi.localIP().toString()); device = new HADevice(macAddress, sizeof(macAddress)); @@ -131,6 +134,7 @@ void JaamHomeAssistant::initDevice(const char* deviceName, const char* currentFw device->enableSharedAvailability(); device->enableLastWill(); #endif +return haEnabled; } void JaamHomeAssistant::loop() { @@ -152,18 +156,6 @@ bool JaamHomeAssistant::isHaEnabled() { #endif } -void JaamHomeAssistant::setChipID(const char* chipId) { -#if HA_ENABLED - strcpy(deviceUniqueID, chipId); -#endif -} - -void JaamHomeAssistant::setMqttServer(const char* mqttServerIp) { -#if HA_ENABLED - haEnabled = mqttServer.fromString(mqttServerIp); -#endif -} - bool JaamHomeAssistant::connectToMqtt(const uint16_t serverPort, const char* mqttUser, const char* mqttPassword, void (*onStatusChanged)(bool connected)){ #if HA_ENABLED if (!haEnabled) return false; @@ -365,9 +357,9 @@ void JaamHomeAssistant::initAutoBrightnessModeSensor(int currentAutoBrightnessMo if (!haEnabled) return; sprintf(haBrightnessAutoID, "%s_brightness_auto", deviceUniqueID); haAutoBrightnessMode = new HASelect(haBrightnessAutoID); - char autoBrightnessOptionsString[sizeOfCharsArray(autoBrightnessModes, autoBrightmesSize) + autoBrightmesSize]; - getHaOptions(autoBrightnessOptionsString, autoBrightnessModes, autoBrightmesSize); - haAutoBrightnessMode->setOptions(autoBrightnessOptionsString); + char autoBrightnessOptions[sizeOfCharsArray(autoBrightnessModes, autoBrightmesSize) + autoBrightmesSize]; + getHaOptions(autoBrightnessOptions, autoBrightnessModes, autoBrightmesSize); + haAutoBrightnessMode->setOptions(autoBrightnessOptions); onHaAutoBrightnessModeChanged = onChange; haAutoBrightnessMode->onCommand([](int8_t index, HASelect* sender) { onHaAutoBrightnessModeChanged(index); }); haAutoBrightnessMode->setIcon("mdi:brightness-auto"); diff --git a/src/firmware/JaamHomeAssistant.h b/src/firmware/JaamHomeAssistant.h index 2579b6ef..5e522a4a 100644 --- a/src/firmware/JaamHomeAssistant.h +++ b/src/firmware/JaamHomeAssistant.h @@ -9,14 +9,12 @@ class JaamHomeAssistant { public: JaamHomeAssistant(); - void initDevice(const char* deviceName, const char* currentFwVersion, const char* deviceDescription); + bool initDevice(const char* mqttServeIp, const char* deviceName, const char* currentFwVersion, const char* deviceDescription, const char* chipID); void loop(); bool isHaAvailable(); bool isHaEnabled(); - void setChipID(const char* chipID); - void setMqttServer(const char* mqttServer); bool connectToMqtt(const uint16_t serverPort, const char* mqttUser, const char* mqttPassword, void (*onStatusChanged)(bool connected)); bool isMqttConnected(); diff --git a/src/firmware/firmware.ino b/src/firmware/firmware.ino index 3f970452..5f1508b4 100644 --- a/src/firmware/firmware.ino +++ b/src/firmware/firmware.ino @@ -284,7 +284,6 @@ void initChipID() { uint64_t chipid = ESP.getEfuseMac(); sprintf(chipID, "%04x%04x", (uint32_t)(chipid >> 32), (uint32_t)chipid); Serial.printf("ChipID Inited: '%s'\n", chipID); - ha.setChipID(chipID); } void initSettings() { @@ -784,17 +783,16 @@ void initHA() { if (shouldWifiReconnect) return; Serial.println("Init Home assistant API"); - ha.setMqttServer(settings.ha_brokeraddress); - if (!ha.isHaAvailable()) { - Serial.println("Invalid IP-address format!"); + if (!ha.initDevice(settings.ha_brokeraddress, settings.devicename, currentFwVersion, settings.devicedescription, chipID)) { + Serial.println("Home Assistant is not available!"); return; } - ha.initDevice(settings.devicename, currentFwVersion, settings.devicedescription); ha.initUptimeSensor(); ha.initWifiSignalSensor(); ha.initFreeMemorySensor(); ha.initUsedMemorySensor(); + ha.initCpuTempSensor(temperatureRead()); ha.initBrightnessSensor(settings.brightness, saveBrightness); ha.initMapModeSensor(settings.map_mode, MAP_MODES, MAP_MODES_COUNT, saveMapMode); if (display.isDisplayAvailable()) { @@ -803,14 +801,13 @@ void initHA() { ha.initDisplayModeToggleSensor(nextDisplayMode); ha.initShowHomeAlarmTimeSensor(settings.home_alert_time, saveShowHomeAlarmTime); } - ha.initAutoAlarmModeSensor(settings.alarms_auto_switch, AUTO_ALRMS_MODES, AUTO_ALARM_MODES_COUNT, saveAutoAlarmMode); - ha.initMapModeCurrentSensor(); - ha.initMapApiConnectSensor(client_websocket.available()); + ha.initAutoAlarmModeSensor(settings.alarms_auto_switch, AUTO_ALARM_MODES, AUTO_ALARM_MODES_COUNT, saveAutoAlarmMode); ha.initAutoBrightnessModeSensor(settings.brightness_mode, AUTO_BRIGHTNESS_MODES, AUTO_BRIGHTNESS_OPTIONS_COUNT, saveAutoBrightnessMode); + ha.initMapModeCurrentSensor(); + ha.initHomeDistrictSensor(); + ha.initMapApiConnectSensor(apiConnected); ha.initRebootSensor([] { rebootDevice(); }); ha.initMapModeToggleSensor(nextMapMode); - ha.initCpuTempSensor(temperatureRead()); - ha.initHomeDistrictSensor(); ha.initLampSensor(settings.map_mode == 5, settings.ha_light_brightness, settings.ha_light_r, settings.ha_light_g, settings.ha_light_b, onNewLampStateFromHa, saveLampBrightness, saveLampRgb); ha.initAlarmAtHomeSensor(alarmNow); @@ -2107,7 +2104,7 @@ void handleRoot(AsyncWebServerRequest* request) { html += addSlider("alert_off_time", "Тривалість відображення відбою", settings.alert_off_time, 1, 10, 1, " хвилин", settings.alarms_notify_mode == 0); html += addSlider("explosion_time", "Тривалість відображення інформації про вибухи", settings.explosion_time, 1, 10, 1, " хвилин", settings.alarms_notify_mode == 0); html += addSlider("alert_blink_time", "Тривалість анімації зміни яскравості", settings.alert_blink_time, 1, 5, 1, " секунд", settings.alarms_notify_mode != 2); - html += addSelectBox("alarms_auto_switch", "Перемикання мапи в режим тривоги у випадку тривоги у домашньому регіоні", settings.alarms_auto_switch, AUTO_ALRMS_MODES, AUTO_ALARM_MODES_COUNT); + html += addSelectBox("alarms_auto_switch", "Перемикання мапи в режим тривоги у випадку тривоги у домашньому регіоні", settings.alarms_auto_switch, AUTO_ALARM_MODES, AUTO_ALARM_MODES_COUNT); if (!settings.legacy) { html += addCheckbox("service_diodes_mode", settings.service_diodes_mode, "Ввімкнути сервісні діоди"); }