@@ -51,17 +51,17 @@ void addHADiscoveryFan(JsonObject obj, const char *name)
5151 snprintf (modestatetopic, sizeof (modestatetopic), " %s%s" , systemConfig.mqtt_base_topic , " /modestate" );
5252
5353 std::string uniqueId = normalizeUniqueId (std::string (name) + " _fan" );
54- JsonObject componentJson = obj[const_cast < char *>( uniqueId. c_str ()) ].to <JsonObject>();
54+ JsonObject componentJson = obj[uniqueId].to <JsonObject>();
5555 componentJson[" name" ] = " Fan" ;
5656 componentJson[" p" ] = " fan" ;
57- componentJson[" uniq_id" ] = const_cast < char *>( uniqueId. c_str ()) ;
58- componentJson[" json_attr_t" ] = const_cast < char *>( ihtostatustopic); // json_attributes_topic
59- componentJson[" cmd_t" ] = const_cast < char *>( cmdtopic); // command_topic
60- componentJson[" pct_cmd_t" ] = const_cast < char *>( cmdtopic); // percentage_command_topic
61- componentJson[" pct_stat_t" ] = const_cast < char *>( statetopic); // percentage_state_topic
57+ componentJson[" uniq_id" ] = uniqueId;
58+ componentJson[" json_attr_t" ] = ihtostatustopic; // json_attributes_topic
59+ componentJson[" cmd_t" ] = cmdtopic; // command_topic
60+ componentJson[" pct_cmd_t" ] = cmdtopic; // percentage_command_topic
61+ componentJson[" pct_stat_t" ] = statetopic; // percentage_state_topic
6262 componentJson[" stat_val_tpl" ] = " {% if value == '0' %}OFF{% else %}ON{% endif %}" ; // state_value_template
63- componentJson[" pr_mode_cmd_t" ] = const_cast < char *>( cmdtopic); // preset_mode_command_topic
64- componentJson[" pr_mode_stat_t" ] = const_cast < char *>( ihtostatustopic); // preset_mode_state_topic
63+ componentJson[" pr_mode_cmd_t" ] = cmdtopic; // preset_mode_command_topic
64+ componentJson[" pr_mode_stat_t" ] = ihtostatustopic; // preset_mode_state_topic
6565
6666 JsonArray modes = componentJson[" pr_modes" ].to <JsonArray>(); // preset_modes
6767 modes.add (" Low" );
@@ -126,7 +126,7 @@ void addHADiscoveryFan(JsonObject obj, const char *name)
126126 else if (deviceID == 0x4 ) // cve eco2 0x4
127127 {
128128 actualSpeedLabel = getSpeedLabel (); // -> {"Speed status", "speed-status"}, of error_info_labels.h
129- }
129+ }
130130 else if (deviceID == 0x14 ) // cve 0x14
131131 {
132132 actualSpeedLabel = getStatusLabel (0 , ithoDeviceptr); // -> {"Ventilation level (%)", "ventilation-level_perc"}, of cve14.h
@@ -182,20 +182,20 @@ void addHADiscoveryFWUpdate(JsonObject obj, const char *name)
182182 snprintf (ithodevicetopic, sizeof (ithodevicetopic), " %s%s" , systemConfig.mqtt_base_topic , " /deviceinfo" );
183183
184184 std::string uniqueId = normalizeUniqueId (std::string (name) + " _fwupdate" );
185- JsonObject componentJson = obj[const_cast < char *>( uniqueId. c_str ()) ].to <JsonObject>();
185+ JsonObject componentJson = obj[uniqueId].to <JsonObject>();
186186 componentJson[" name" ] = " Firmware Update" ;
187187 componentJson[" p" ] = " update" ;
188- componentJson[" uniq_id" ] = const_cast < char *>( uniqueId. c_str ()) ;
188+ componentJson[" uniq_id" ] = uniqueId;
189189 componentJson[" dev_cla" ] = " firmware" ;
190190
191191 std::string tmpstr = " http://" + std::string (WiFi.localIP ().toString ().c_str ()) + " /favicon.png" ;
192- componentJson[" ent_pic" ] = const_cast < char *>( tmpstr. c_str ()) ;
192+ componentJson[" ent_pic" ] = tmpstr;
193193 // componentJson["icon"] = "mdi:update";
194194
195195 componentJson[" stat_t" ] = ithodevicetopic;
196196
197197 tmpstr = " {{ {'installed_version': value_json['add-on_fwversion'], 'latest_version': value_json['add-on_latest_fw'], 'title': 'Add-on Firmware', 'release_url': 'https://github.com/arjenhiemstra/ithowifi/releases/tag/Version-" + std::string (firmwareInfo.latest_fw ) + " ' } | to_json }}" ;
198- componentJson[" val_tpl" ] = const_cast < char *>( tmpstr. c_str ()) ;
198+ componentJson[" val_tpl" ] = tmpstr;
199199}
200200
201201void generateHADiscoveryJson (JsonObject compactJson, JsonObject outputJson)
@@ -212,20 +212,21 @@ void generateHADiscoveryJson(JsonObject compactJson, JsonObject outputJson)
212212 const char *deviceName = compactJson[" d" ]; // Retrieve device name from compact JSON
213213
214214 // generic status topics, can be overiden per component
215- outputJson[" avty_t" ] = const_cast < char *>( lwttopic) ;
216- outputJson[" stat_t" ] = const_cast < char *>( ihtostatustopic) ; // state_topic
215+ outputJson[" avty_t" ] = lwttopic;
216+ outputJson[" stat_t" ] = ihtostatustopic; // state_topic
217217
218218 // Static device information
219219 addHADevInfo (outputJson);
220220
221221 // Update name from config
222- outputJson[" dev" ][" name" ] = const_cast < char *>( deviceName) ;
222+ outputJson[" dev" ][" name" ] = deviceName;
223223
224224 // Origin object
225225 JsonObject origin = outputJson[" o" ].to <JsonObject>();
226226
227- origin[" name" ] = const_cast <char *>(deviceName);
228- origin[" sw" ] = fw_version;
227+ origin[" name" ] = deviceName;
228+ // origin["sw"] = fw_version;
229+ origin[" sw" ] = JsonString (fw_version, true );
229230 origin[" url" ] = " https://www.nrgwatch.nl/support-ondersteuning/" ;
230231
231232 // Components object
@@ -240,7 +241,6 @@ void generateHADiscoveryJson(JsonObject compactJson, JsonObject outputJson)
240241
241242 for (JsonObject component : componentsArray)
242243 {
243- // each object needs to be copied into the JSON to prevent dangling pointers to be inserted, therefor const_cast<char *>() is applied
244244
245245 configuredIndices.insert (component[" i" ].as <uint8_t >());
246246
@@ -250,10 +250,10 @@ void generateHADiscoveryJson(JsonObject compactJson, JsonObject outputJson)
250250 // Generate normalized unique ID
251251 std::string uniqueId = normalizeUniqueId (std::string (outputJson[" dev" ][" ids" ] | " default_name" ) + " _" + platform + " _i_" + std::to_string (index));
252252 // Create component object using uniqueID
253- JsonObject componentJson = components[const_cast < char *>( uniqueId. c_str ()) ].to <JsonObject>();
254- componentJson[" name" ] = const_cast < char *>( name) ;
255- componentJson[" p" ] = const_cast < char *>( platform) ;
256- componentJson[" uniq_id" ] = const_cast < char *>( uniqueId. c_str ()) ; // Add unique_id
253+ JsonObject componentJson = components[uniqueId].to <JsonObject>();
254+ componentJson[" name" ] = name;
255+ componentJson[" p" ] = platform;
256+ componentJson[" uniq_id" ] = uniqueId; // Add unique_id
257257 if (component[" vt" ].isNull ())
258258 {
259259 JsonObject::iterator it = statusitemsdoc.as <JsonObject>().begin ();
@@ -265,19 +265,25 @@ void generateHADiscoveryJson(JsonObject compactJson, JsonObject outputJson)
265265 }
266266 else
267267 {
268- componentJson[" val_tpl" ] = const_cast < char *>( component[" vt" ].as <const char *>() );
268+ componentJson[" val_tpl" ] = component[" vt" ].as <const char *>();
269269 }
270270 const char *unitOfMeasurement = component[" um" ].is <const char *>() ? component[" um" ].as <const char *>() : nullptr ;
271271 const char *deviceClass = component[" dc" ].is <const char *>() ? component[" dc" ].as <const char *>() : nullptr ;
272+ const char *stateClass = component[" sc" ].is <const char *>() ? component[" sc" ].as <const char *>() : nullptr ;
272273
273274 if (unitOfMeasurement)
274275 {
275- componentJson[" unit_of_meas" ] = const_cast < char *>( unitOfMeasurement) ;
276+ componentJson[" unit_of_meas" ] = unitOfMeasurement;
276277 }
277278
278279 if (deviceClass)
279280 {
280- componentJson[" dev_cla" ] = const_cast <char *>(deviceClass);
281+ componentJson[" dev_cla" ] = deviceClass;
282+ }
283+
284+ if (stateClass)
285+ {
286+ componentJson[" stat_cla" ] = stateClass; // Use "sc" from compact JSON
281287 }
282288 }
283289
@@ -291,7 +297,7 @@ void generateHADiscoveryJson(JsonObject compactJson, JsonObject outputJson)
291297 std::string uniqueId = normalizeUniqueId (std::string (outputJson[" dev" ][" ids" ] | " default_name" ) + " _sensor_i_" + std::to_string (index));
292298
293299 // Create empty component
294- JsonObject componentJson = components[const_cast < char *>( uniqueId. c_str ()) ].to <JsonObject>();
300+ JsonObject componentJson = components[uniqueId].to <JsonObject>();
295301 componentJson[" p" ] = " sensor" ; // Default platform
296302 }
297303 }
0 commit comments