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