From ca871f654b85c0f0260832a3a1a267749d36f05e Mon Sep 17 00:00:00 2001 From: Proddy Date: Fri, 23 Dec 2022 14:40:24 +0100 Subject: [PATCH] Remove the test.mosquitto.org URL in the MQTT settings #829 --- CHANGELOG_LATEST.md | 5 +-- factory_settings.ini | 6 ++-- .../src/framework/mqtt/MqttSettingsForm.tsx | 4 +-- interface/src/validators/mqtt.ts | 36 ++++++++++--------- lib/framework/MqttSettingsService.cpp | 6 ++-- lib/framework/MqttSettingsService.h | 2 +- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md index d7186d8a0..3bbfd9ef4 100644 --- a/CHANGELOG_LATEST.md +++ b/CHANGELOG_LATEST.md @@ -24,8 +24,9 @@ - Add more bus-ids [#673](https://github.com/emsesp/EMS-ESP32/issues/673) - Use HA connectivity device class for Status, added boot time [#751](https://github.com/emsesp/EMS-ESP32/issues/751) - Add commands for analog sensors outputs -- Support for multiple EMS-ESPs with MQTT and HA [[#759](https://github.com/emsesp/EMS-ESP32/issues/759)] -- Settings for heatpump silent mode and additional heater [[#802](https://github.com/emsesp/EMS-ESP32/issues/802)] [[#803](https://github.com/emsesp/EMS-ESP32/issues/803)] +- Support for multiple EMS-ESPs with MQTT and HA [#759](https://github.com/emsesp/EMS-ESP32/issues/759) +- Settings for heatpump silent mode and additional heater [#802](https://github.com/emsesp/EMS-ESP32/issues/802)] [[#803](https://github.com/emsesp/EMS-ESP32/issues/803) +- Default MQTT hostname is blank [#829](https://github.com/emsesp/EMS-ESP32/issues/829) ## Fixed diff --git a/factory_settings.ini b/factory_settings.ini index 12a99b948..106c29c62 100644 --- a/factory_settings.ini +++ b/factory_settings.ini @@ -7,8 +7,8 @@ build_flags = ; Access point settings -D FACTORY_AP_PROVISION_MODE=AP_MODE_DISCONNECTED - -D FACTORY_AP_SSID=\"ems-esp\" ; 1-64 characters - -D FACTORY_AP_PASSWORD=\"ems-esp-neo\" ; 8-64 characters + -D FACTORY_AP_SSID=\"ems-esp\" + -D FACTORY_AP_PASSWORD=\"ems-esp-neo\" -D FACTORY_AP_LOCAL_IP=\"192.168.4.1\" -D FACTORY_AP_GATEWAY_IP=\"192.168.4.1\" -D FACTORY_AP_SUBNET_MASK=\"255.255.255.0\" @@ -32,7 +32,7 @@ build_flags = ; MQTT settings -D FACTORY_MQTT_ENABLED=false - -D FACTORY_MQTT_HOST=\"test.mosquitto.org\" + -D FACTORY_MQTT_HOST=\"\" -D FACTORY_MQTT_PORT=1883 -D FACTORY_MQTT_USERNAME=\"\" -D FACTORY_MQTT_PASSWORD=\"\" diff --git a/interface/src/framework/mqtt/MqttSettingsForm.tsx b/interface/src/framework/mqtt/MqttSettingsForm.tsx index ebe7151f8..7c79251a7 100644 --- a/interface/src/framework/mqtt/MqttSettingsForm.tsx +++ b/interface/src/framework/mqtt/MqttSettingsForm.tsx @@ -4,7 +4,7 @@ import { ValidateFieldsError } from 'async-validator'; import { Button, Checkbox, MenuItem, Grid, Typography, InputAdornment } from '@mui/material'; import SaveIcon from '@mui/icons-material/Save'; -import { MQTT_SETTINGS_VALIDATOR, validate } from '../../validators'; +import { createMqttSettingsValidator, validate } from '../../validators'; import { BlockFormControlLabel, ButtonRow, @@ -39,7 +39,7 @@ const MqttSettingsForm: FC = () => { const validateAndSubmit = async () => { try { setFieldErrors(undefined); - await validate(MQTT_SETTINGS_VALIDATOR, data); + await validate(createMqttSettingsValidator(data), data); saveData(); } catch (errors: any) { setFieldErrors(errors); diff --git a/interface/src/validators/mqtt.ts b/interface/src/validators/mqtt.ts index a38e2d8a0..817c9b610 100644 --- a/interface/src/validators/mqtt.ts +++ b/interface/src/validators/mqtt.ts @@ -1,19 +1,23 @@ import Schema from 'async-validator'; +import { MqttSettings } from '../types'; import { IP_OR_HOSTNAME_VALIDATOR } from './shared'; -export const MQTT_SETTINGS_VALIDATOR = new Schema({ - host: [{ required: true, message: 'Host is required' }, IP_OR_HOSTNAME_VALIDATOR], - base: { required: true, message: 'Base is required' }, - port: [ - { required: true, message: 'Port is required' }, - { type: 'number', min: 0, max: 65535, message: 'Invalid Port' } - ], - keep_alive: [ - { required: true, message: 'Keep alive is required' }, - { type: 'number', min: 1, max: 86400, message: 'Keep alive must be between 1 and 86400' } - ], - publish_time_heartbeat: [ - { required: true, message: 'Heartbeat is required' }, - { type: 'number', min: 10, max: 86400, message: 'Heartbeat must be between 10 and 86400' } - ] -}); +export const createMqttSettingsValidator = (mqttSettings: MqttSettings) => + new Schema({ + ...(mqttSettings.enabled && { + host: [{ required: true, message: 'Host is required' }, IP_OR_HOSTNAME_VALIDATOR], + base: { required: true, message: 'Base is required' }, + port: [ + { required: true, message: 'Port is required' }, + { type: 'number', min: 0, max: 65535, message: 'Invalid Port' } + ], + keep_alive: [ + { required: true, message: 'Keep alive is required' }, + { type: 'number', min: 1, max: 86400, message: 'Keep alive must be between 1 and 86400' } + ], + publish_time_heartbeat: [ + { required: true, message: 'Heartbeat is required' }, + { type: 'number', min: 10, max: 86400, message: 'Heartbeat must be between 10 and 86400' } + ] + }) + }); diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index f54372578..7491691c5 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -125,8 +125,8 @@ void MqttSettingsService::configureMqtt() { // disconnect if connected _mqttClient.disconnect(); // only connect if WiFi is connected and MQTT is enabled - if (_state.enabled && emsesp::EMSESP::system_.network_connected()) { - // emsesp::EMSESP::logger().info("Configuring Mqtt client"); + if (_state.enabled && emsesp::EMSESP::system_.network_connected() && !_state.host.isEmpty()) { + // emsesp::EMSESP::logger().info("Configuring MQTT client"); _mqttClient.setServer(retainCstr(_state.host.c_str(), &_retainedHost), _state.port); if (_state.username.length() > 0) { _mqttClient.setCredentials(retainCstr(_state.username.c_str(), &_retainedUsername), @@ -140,7 +140,7 @@ void MqttSettingsService::configureMqtt() { _mqttClient.setMaxTopicLength(FACTORY_MQTT_MAX_TOPIC_LENGTH); // hardcode. We don't take this from the settings anymore. _mqttClient.connect(); // } else { - // emsesp::EMSESP::logger().info("Error configuring Mqtt client"); + // emsesp::EMSESP::logger().info("Error configuring MQTT client"); } } diff --git a/lib/framework/MqttSettingsService.h b/lib/framework/MqttSettingsService.h index ab6766b35..d4ec575cc 100644 --- a/lib/framework/MqttSettingsService.h +++ b/lib/framework/MqttSettingsService.h @@ -19,7 +19,7 @@ #endif #ifndef FACTORY_MQTT_HOST -#define FACTORY_MQTT_HOST "test.mosquitto.org" +#define FACTORY_MQTT_HOST "" // is blank #endif #ifndef FACTORY_MQTT_PORT