add oil tank sensor
This commit is contained in:
parent
6fd9bdc8ce
commit
965e158b6a
@ -17,6 +17,39 @@ struct SensorBuilder {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct OilTankSensor : Sensor {
|
||||||
|
OilTankSensor(const char* id) : Sensor("Depth", id) {
|
||||||
|
deviceClass = "distance";
|
||||||
|
unitMeasure = "cm";
|
||||||
|
valueTemplate = "{{ value_json.sensor.value }}";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct OilTankLevelSensor : Sensor {
|
||||||
|
OilTankLevelSensor(const char* id) : Sensor("Level", id) {
|
||||||
|
unitMeasure = "%";
|
||||||
|
valueTemplate = "{{ 100 - ((value_json.sensor.value-7)|float*100/110)|round(2) }}";
|
||||||
|
}
|
||||||
|
|
||||||
|
void buildUniqueId(char* uniqueId) override {
|
||||||
|
sprintf(uniqueId, "level_%s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void buildConfigTopic() override {
|
||||||
|
sprintf(configTopic, "homeassistant/%s/%s/level_%s/config", type, MAIN_DEVICE_ID, id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct OilSensorBuilder {
|
||||||
|
static Sensor* build(const char* id) {
|
||||||
|
Sensor* sensor = (new OilTankSensor("7"))->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank")->withArea("Basement");
|
||||||
|
(new OilTankLevelSensor(sensor->id))->asDevice(new DeviceConfig{sensor->mainDevice->id});
|
||||||
|
(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id});
|
||||||
|
(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id});
|
||||||
|
return sensor;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct PollinSwitch : Switch {
|
struct PollinSwitch : Switch {
|
||||||
const char* group;
|
const char* group;
|
||||||
unsigned char channel;
|
unsigned char channel;
|
||||||
|
|||||||
@ -69,15 +69,20 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual void buildUniqueId(char* uniqueId) = 0;
|
virtual void buildUniqueId(char* uniqueId) = 0;
|
||||||
|
|
||||||
virtual void buildConfig(JsonDocument& jsonDoc) {
|
virtual void buildConfigTopic() {
|
||||||
if (String{"diagnostic"}.equals(entityCategory)) {
|
if (String{"diagnostic"}.equals(entityCategory) && deviceClass) {
|
||||||
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id);
|
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id);
|
||||||
} else {
|
} else {
|
||||||
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) {
|
||||||
|
buildConfigTopic();
|
||||||
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;
|
||||||
jsonDoc["name"] = name;
|
jsonDoc["name"] = name;
|
||||||
char uniqueId[50];
|
char uniqueId[50];
|
||||||
buildUniqueId(uniqueId);
|
buildUniqueId(uniqueId);
|
||||||
@ -184,9 +189,7 @@ namespace Ha {
|
|||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildConfig(JsonDocument& jsonDoc) override {
|
||||||
EntityConfig::buildConfig(jsonDoc);
|
EntityConfig::buildConfig(jsonDoc);
|
||||||
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
|
||||||
jsonDoc["device_class"] = deviceClass;
|
|
||||||
jsonDoc["unit_of_measurement"] = unitMeasure;
|
|
||||||
if (valueTemplate) jsonDoc["value_template"] = valueTemplate;
|
if (valueTemplate) jsonDoc["value_template"] = valueTemplate;
|
||||||
jsonDoc["state_topic"] = stateTopic;
|
jsonDoc["state_topic"] = stateTopic;
|
||||||
jsonDoc["suggested_display_precision"] = 2;
|
jsonDoc["suggested_display_precision"] = 2;
|
||||||
@ -241,12 +244,4 @@ namespace Ha {
|
|||||||
valueTemplate = "{{ value_json.sensor.pressure }}";
|
valueTemplate = "{{ value_json.sensor.pressure }}";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// struct AltitudeSensor : Sensor {
|
|
||||||
// AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
|
|
||||||
// deviceClass = "distance";
|
|
||||||
// unitMeasure = "m";
|
|
||||||
// valueTemplate = "{{ value_json.sensor.altitude }}";
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,6 @@ namespace Mqtt {
|
|||||||
AsyncMqttClient client;
|
AsyncMqttClient client;
|
||||||
|
|
||||||
void publishInit();
|
void publishInit();
|
||||||
void publishBme280();
|
|
||||||
void disconnect();
|
void disconnect();
|
||||||
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
||||||
Serial.println("Connecting to MQTT...");
|
Serial.println("Connecting to MQTT...");
|
||||||
@ -50,11 +49,8 @@ namespace Mqtt {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Ha::Sensor* sensors[] = {
|
Ha::Sensor* sensors[] = {
|
||||||
SensorBuilder::withVoltage((new Ha::TemperatureSensor{"4"})->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank room")->withArea("Basement"))
|
SensorBuilder::withVoltage((new Ha::TemperatureSensor{"4"})->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank room")->withArea("Basement")),
|
||||||
// new Ha::TemperatureSensor{"Temperature", "temperature"},
|
OilSensorBuilder::build("7")
|
||||||
// new Ha::HumiditySensor{"Humidity", "humidity"},
|
|
||||||
// new Ha::PressureSensor{"Pressure", "pressure"},
|
|
||||||
// new Ha::AltitudeSensor{"Altitude", "altitude"}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void publishComponentConfig(Ha::Component& component) {
|
void publishComponentConfig(Ha::Component& component) {
|
||||||
@ -81,17 +77,6 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishBme280() {
|
|
||||||
// StaticJsonDocument<255> jsonDoc;
|
|
||||||
// jsonDoc["temperature"] = Bme::data.temp;
|
|
||||||
// jsonDoc["humidity"] = Bme::data.humidity;
|
|
||||||
// jsonDoc["pressure"] = Bme::data.pressure;
|
|
||||||
// jsonDoc["altitude"] = Bme::data.altitude;
|
|
||||||
// char message[255];
|
|
||||||
// serializeJson(jsonDoc, message);
|
|
||||||
// publish(Ha::Sensor::stateTopic, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||||
char msg[len + 1];
|
char msg[len + 1];
|
||||||
memcpy(msg, payload, len);
|
memcpy(msg, payload, len);
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
],
|
],
|
||||||
"settings": {
|
"settings": {
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
|
"*.yaml": "esphome",
|
||||||
"functional": "cpp",
|
"functional": "cpp",
|
||||||
"queue": "cpp",
|
"queue": "cpp",
|
||||||
"array": "cpp",
|
"array": "cpp",
|
||||||
@ -49,7 +50,8 @@
|
|||||||
"algorithm": "cpp",
|
"algorithm": "cpp",
|
||||||
"iterator": "cpp",
|
"iterator": "cpp",
|
||||||
"memory": "cpp",
|
"memory": "cpp",
|
||||||
"variant": "cpp"
|
"variant": "cpp",
|
||||||
|
"string_view": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user