Skip to content

Commit

Permalink
fixed typo, HA init
Browse files Browse the repository at this point in the history
  • Loading branch information
Foroxon committed Apr 12, 2024
1 parent 5762c1f commit 77e8da2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/firmware/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
"Вимкнено",
"Домашній та суміжні",
"Лише домашній"
Expand Down
24 changes: 8 additions & 16 deletions src/firmware/JaamHomeAssistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -131,6 +134,7 @@ void JaamHomeAssistant::initDevice(const char* deviceName, const char* currentFw
device->enableSharedAvailability();
device->enableLastWill();
#endif
return haEnabled;
}

void JaamHomeAssistant::loop() {
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 1 addition & 3 deletions src/firmware/JaamHomeAssistant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
19 changes: 8 additions & 11 deletions src/firmware/firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
Expand Down Expand Up @@ -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, "Ввімкнути сервісні діоди");
}
Expand Down

0 comments on commit 77e8da2

Please sign in to comment.