Merge branch 'v1.1.0'
This commit is contained in:
commit
6a4350a682
@ -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.0.1",
|
"version": "1.1.0",
|
||||||
"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",
|
||||||
|
|||||||
71
src/ha.h
71
src/ha.h
@ -65,12 +65,32 @@ namespace Ha {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Component {
|
struct Component {
|
||||||
|
|
||||||
|
template <typename V>
|
||||||
|
struct JsonPairs {
|
||||||
|
typedef pair<const char*, V> JsonPair;
|
||||||
|
unordered_map<const char*, V> jsonPairs;
|
||||||
|
|
||||||
|
void add(JsonPair pair) {
|
||||||
|
jsonPairs.insert(pair);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addToJson(JsonDocument& jsonDoc) {
|
||||||
|
for (JsonPair el : jsonPairs) {
|
||||||
|
jsonDoc[el.first] = el.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const char* entityCategory = nullptr;
|
const char* entityCategory = nullptr;
|
||||||
const char* deviceClass = nullptr;
|
const char* deviceClass = nullptr;
|
||||||
const char* name = nullptr;
|
const char* name = nullptr;
|
||||||
char* id = nullptr;
|
char* id = nullptr;
|
||||||
const char* type = nullptr;
|
const char* type = nullptr;
|
||||||
char configTopic[TOPIC_SIZE];
|
char configTopic[TOPIC_SIZE];
|
||||||
|
JsonPairs<bool> jsonBooleans;
|
||||||
|
JsonPairs<float> jsonNumbers;
|
||||||
|
JsonPairs<const char*> jsonStrings;
|
||||||
DeviceConfig* mainDevice = nullptr;
|
DeviceConfig* mainDevice = nullptr;
|
||||||
static List<Component> components;
|
static List<Component> components;
|
||||||
|
|
||||||
@ -86,7 +106,9 @@ namespace Ha {
|
|||||||
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
|
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void buildConfig(JsonDocument& jsonDoc) {
|
virtual void buildComponentConfig(JsonDocument& jsonDoc) = 0;
|
||||||
|
|
||||||
|
void buildConfig(JsonDocument& jsonDoc) {
|
||||||
if (mainDevice) mainDevice->buildConfig(jsonDoc);
|
if (mainDevice) mainDevice->buildConfig(jsonDoc);
|
||||||
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
||||||
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
||||||
@ -95,6 +117,12 @@ namespace Ha {
|
|||||||
buildUniqueId(uniqueId);
|
buildUniqueId(uniqueId);
|
||||||
jsonDoc["unique_id"] = uniqueId;
|
jsonDoc["unique_id"] = uniqueId;
|
||||||
buildConfigTopic();
|
buildConfigTopic();
|
||||||
|
|
||||||
|
buildComponentConfig(jsonDoc);
|
||||||
|
|
||||||
|
jsonBooleans.addToJson(jsonDoc);
|
||||||
|
jsonNumbers.addToJson(jsonDoc);
|
||||||
|
jsonStrings.addToJson(jsonDoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishConfig() {
|
void publishConfig() {
|
||||||
@ -162,6 +190,27 @@ namespace Ha {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Builder& withPrecision(unsigned int value) {
|
||||||
|
cmp->precision = value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Builder& overrideConfig(const char* key, bool value) {
|
||||||
|
cmp->jsonBooleans.add({key, value});
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename V>
|
||||||
|
Builder& overrideConfig(const char* key, V value) {
|
||||||
|
cmp->jsonNumbers.add({key, value});
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Builder& overrideConfig(const char* key, const char* value) {
|
||||||
|
cmp->jsonStrings.add({key, value});
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Builder& addSecondary(Component* c) {
|
Builder& addSecondary(Component* c) {
|
||||||
if (cmp->mainDevice) c->mainDevice = &DeviceConfig::create(cmp->mainDevice->id);
|
if (cmp->mainDevice) c->mainDevice = &DeviceConfig::create(cmp->mainDevice->id);
|
||||||
return *this;
|
return *this;
|
||||||
@ -213,10 +262,12 @@ namespace Ha {
|
|||||||
if (f) f(msg);
|
if (f) f(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
virtual void buildCommandConfig(JsonDocument& jsonDoc) = 0;
|
||||||
Component::buildConfig(jsonDoc);
|
|
||||||
|
void buildComponentConfig(JsonDocument& jsonDoc) override {
|
||||||
jsonDoc["command_topic"] = commandTopic;
|
jsonDoc["command_topic"] = commandTopic;
|
||||||
if (retain) jsonDoc["retain"] = true;
|
if (retain) jsonDoc["retain"] = true;
|
||||||
|
buildCommandConfig(jsonDoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -225,6 +276,8 @@ namespace Ha {
|
|||||||
|
|
||||||
Button(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "button", f) {}
|
Button(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "button", f) {}
|
||||||
|
|
||||||
|
void buildCommandConfig(JsonDocument& jsonDoc) override {}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct StateConfig {
|
struct StateConfig {
|
||||||
@ -251,8 +304,7 @@ namespace Ha {
|
|||||||
|
|
||||||
Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f), StateConfig(this) {}
|
Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f), StateConfig(this) {}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildCommandConfig(JsonDocument& jsonDoc) override {
|
||||||
Command::buildConfig(jsonDoc);
|
|
||||||
StateConfig::buildConfig(jsonDoc);
|
StateConfig::buildConfig(jsonDoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,8 +323,7 @@ namespace Ha {
|
|||||||
Number(const char* name, const char* id, unsigned int min, unsigned int max, unsigned int step, onMessage f)
|
Number(const char* name, const char* id, unsigned int min, unsigned int max, unsigned int step, onMessage f)
|
||||||
: Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {}
|
: Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildCommandConfig(JsonDocument& jsonDoc) override {
|
||||||
Command::buildConfig(jsonDoc);
|
|
||||||
StateConfig::buildConfig(jsonDoc);
|
StateConfig::buildConfig(jsonDoc);
|
||||||
jsonDoc["min"] = min;
|
jsonDoc["min"] = min;
|
||||||
jsonDoc["max"] = max;
|
jsonDoc["max"] = max;
|
||||||
@ -294,6 +345,7 @@ namespace Ha {
|
|||||||
struct Sensor : Component, StateConfig {
|
struct Sensor : Component, StateConfig {
|
||||||
const char* unitMeasure = nullptr;
|
const char* unitMeasure = nullptr;
|
||||||
const char* valueTemplate = nullptr;
|
const char* valueTemplate = nullptr;
|
||||||
|
unsigned int precision = 2;
|
||||||
static unordered_map<string, Sensor*> mapSensors;
|
static unordered_map<string, Sensor*> mapSensors;
|
||||||
|
|
||||||
Sensor(const char* name, const char* id) : Component(name, id, "sensor"), StateConfig(this) {
|
Sensor(const char* name, const char* id) : Component(name, id, "sensor"), StateConfig(this) {
|
||||||
@ -317,12 +369,11 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildComponentConfig(JsonDocument& jsonDoc) override {
|
||||||
Component::buildConfig(jsonDoc);
|
|
||||||
StateConfig::buildConfig(jsonDoc);
|
StateConfig::buildConfig(jsonDoc);
|
||||||
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
|
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
|
||||||
if (valueTemplate) jsonDoc["value_template"] = valueTemplate;
|
if (valueTemplate) jsonDoc["value_template"] = valueTemplate;
|
||||||
jsonDoc["suggested_display_precision"] = 2;
|
jsonDoc["suggested_display_precision"] = precision;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user