fix possible bug: restore from state needs a state topic

This commit is contained in:
Nicu Hodos 2025-02-07 14:04:05 +01:00
parent 348ae0bd7d
commit 4b600a4686
3 changed files with 13 additions and 6 deletions

View File

@ -1,7 +1,7 @@
{ {
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json", "$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "ha-mqtt", "name": "ha-mqtt",
"version": "1.5.0", "version": "1.5.1",
"description": "Home Assistant classes for integration with MQTT auto discovery", "description": "Home Assistant classes for integration with MQTT auto discovery",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -10,8 +10,8 @@ using namespace std;
#define BASE_TOPIC "homeassistant/%s/%s/%s" #define BASE_TOPIC "homeassistant/%s/%s/%s"
namespace Ha { namespace Ha {
uint16_t(*publisher)(const char* topic, const char* message); uint16(*publisher)(const char*, const char*);
typedef void (*onMessage)(const char* msg); typedef void (*onMessage)(const char*);
struct Config { struct Config {
virtual void buildConfig(JsonDocument& jsonDoc) = 0; virtual void buildConfig(JsonDocument& jsonDoc) = 0;
@ -189,7 +189,8 @@ namespace Ha {
StatefulCommand(Component* cmp, onMessage f) : Command(cmp, f), State(cmp) {} StatefulCommand(Component* cmp, onMessage f) : Command(cmp, f), State(cmp) {}
void restoreFromState() { void restoreStateFromCommand() {
withStateTopic();
mapRestoreStateTopics.insert({stateTopic, this}); mapRestoreStateTopics.insert({stateTopic, this});
} }
@ -457,8 +458,14 @@ namespace Ha {
return *this; return *this;
} }
[[deprecated("Use restoreStateFromCommand() instead")]]
Builder& restoreFromState() { Builder& restoreFromState() {
cmp->restoreFromState(); cmp->restoreStateFromCommand();
return *this;
}
Builder& restoreStateFromCommand() {
cmp->restoreStateFromCommand();
return *this; return *this;
} }
}; };

View File

@ -22,7 +22,7 @@ namespace Mqtt {
client.disconnect(); client.disconnect();
} }
uint16_t publish(const char* topic, const char* message) { uint16 publish(const char* topic, const char* message) {
return client.publish(topic, 0, true, message); return client.publish(topic, 0, true, message);
} }