publish sensor configuration
This commit is contained in:
parent
278d82dec6
commit
f6fed5a115
@ -7,6 +7,7 @@
|
|||||||
using namespace Ha;
|
using namespace Ha;
|
||||||
|
|
||||||
DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
|
DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
|
||||||
|
DeviceConfig* atTinyDevice = (new DeviceConfig{})->withManufacturer("Atmel")->withModel("AtTiny85")->withParent(gatewayDevice);
|
||||||
|
|
||||||
struct PollinSwitch : Switch {
|
struct PollinSwitch : Switch {
|
||||||
const char* group;
|
const char* group;
|
||||||
|
|||||||
@ -17,7 +17,9 @@ namespace Ha {
|
|||||||
const char* area = nullptr;
|
const char* area = nullptr;
|
||||||
DeviceConfig* parent = nullptr;
|
DeviceConfig* parent = nullptr;
|
||||||
|
|
||||||
|
DeviceConfig() {}
|
||||||
DeviceConfig(const char* id, const char* name) : id(id), name(name) {}
|
DeviceConfig(const char* id, const char* name) : id(id), name(name) {}
|
||||||
|
DeviceConfig(DeviceConfig& d) : id(d.id), name(d.name), model(d.model), manufacturer(d.manufacturer), area(d.area), parent(d.parent) {}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) {
|
void buildConfig(JsonDocument& jsonDoc) {
|
||||||
JsonObject device = jsonDoc.createNestedObject("device");
|
JsonObject device = jsonDoc.createNestedObject("device");
|
||||||
@ -94,6 +96,13 @@ namespace Ha {
|
|||||||
mainDevice = deviceConfig;
|
mainDevice = deviceConfig;
|
||||||
return static_cast<T*>(this);
|
return static_cast<T*>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T* copyFromDevice(DeviceConfig* deviceConfig) {
|
||||||
|
mainDevice = new DeviceConfig{*deviceConfig};
|
||||||
|
mainDevice->id = id;
|
||||||
|
mainDevice->name = name;
|
||||||
|
return static_cast<T*>(this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
List<Component> Component::components;
|
List<Component> Component::components;
|
||||||
@ -156,17 +165,16 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Sensor : EntityConfig<Sensor> {
|
struct Sensor : EntityConfig<Sensor>, StateConfig<Sensor> {
|
||||||
const char* deviceClass;
|
const char* deviceClass = nullptr;
|
||||||
const char* unitMeasure;
|
const char* unitMeasure = nullptr;
|
||||||
const char* valueTemplate;
|
const char* valueTemplate = nullptr;
|
||||||
static constexpr const char* stateTopic = "homeassistant/sensor/rc-gateway/state";
|
|
||||||
|
|
||||||
Sensor(const char* name, const char* id) : EntityConfig(name, id, "sensor") {
|
Sensor(const char* name, const char* id) : EntityConfig(name, id, "sensor") {
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildUniqueId(char* uniqueId) override {
|
void buildUniqueId(char* uniqueId) override {
|
||||||
sprintf(uniqueId, "id%s", id);
|
sprintf(uniqueId, "%s", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildConfig(JsonDocument& jsonDoc) override {
|
||||||
@ -174,13 +182,14 @@ namespace Ha {
|
|||||||
jsonDoc["device_class"] = deviceClass;
|
jsonDoc["device_class"] = deviceClass;
|
||||||
jsonDoc["unit_of_measurement"] = unitMeasure;
|
jsonDoc["unit_of_measurement"] = unitMeasure;
|
||||||
jsonDoc["value_template"] = valueTemplate;
|
jsonDoc["value_template"] = valueTemplate;
|
||||||
jsonDoc["state_topic"] = stateTopic;
|
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TemperatureSensor : Sensor {
|
struct TemperatureSensor : Sensor {
|
||||||
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
|
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||||
|
name = "Temperature";
|
||||||
deviceClass = "temperature";
|
deviceClass = "temperature";
|
||||||
unitMeasure = "°C";
|
unitMeasure = "°C";
|
||||||
valueTemplate = "{{ value_json.sensor.temperature }}";
|
valueTemplate = "{{ value_json.sensor.temperature }}";
|
||||||
@ -189,6 +198,7 @@ namespace Ha {
|
|||||||
|
|
||||||
struct HumiditySensor : Sensor {
|
struct HumiditySensor : Sensor {
|
||||||
HumiditySensor(const char* name, const char* id) : Sensor(name, id) {
|
HumiditySensor(const char* name, const char* id) : Sensor(name, id) {
|
||||||
|
name = "Humidity";
|
||||||
deviceClass = "humidity";
|
deviceClass = "humidity";
|
||||||
unitMeasure = "%";
|
unitMeasure = "%";
|
||||||
valueTemplate = "{{ value_json.sensor.humidity }}";
|
valueTemplate = "{{ value_json.sensor.humidity }}";
|
||||||
@ -197,6 +207,7 @@ namespace Ha {
|
|||||||
|
|
||||||
struct PressureSensor : Sensor {
|
struct PressureSensor : Sensor {
|
||||||
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||||
|
name = "Pressure";
|
||||||
deviceClass = "pressure";
|
deviceClass = "pressure";
|
||||||
unitMeasure = "hPa";
|
unitMeasure = "hPa";
|
||||||
valueTemplate = "{{ value_json.sensor.pressure }}";
|
valueTemplate = "{{ value_json.sensor.pressure }}";
|
||||||
|
|||||||
@ -33,6 +33,7 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ha::Sensor* sensors[] = {
|
Ha::Sensor* sensors[] = {
|
||||||
|
(new Ha::TemperatureSensor{"Oil tank room1", "id4"})->withStateTopic()->copyFromDevice(atTinyDevice)->withArea("Basement")
|
||||||
// new Ha::TemperatureSensor{"Temperature", "temperature"},
|
// new Ha::TemperatureSensor{"Temperature", "temperature"},
|
||||||
// new Ha::HumiditySensor{"Humidity", "humidity"},
|
// new Ha::HumiditySensor{"Humidity", "humidity"},
|
||||||
// new Ha::PressureSensor{"Pressure", "pressure"},
|
// new Ha::PressureSensor{"Pressure", "pressure"},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user