diff --git a/gateway/include/devices.h b/gateway/include/devices.h index 3c7fdfe..d6cb24a 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -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 { const char* group; unsigned char channel; diff --git a/gateway/include/ha.h b/gateway/include/ha.h index c69830b..7dca89a 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -69,15 +69,20 @@ namespace Ha { } virtual void buildUniqueId(char* uniqueId) = 0; - - virtual void buildConfig(JsonDocument& jsonDoc) { - if (String{"diagnostic"}.equals(entityCategory)) { + + virtual void buildConfigTopic() { + if (String{"diagnostic"}.equals(entityCategory) && deviceClass) { sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id); } else { sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id); } + } + + virtual void buildConfig(JsonDocument& jsonDoc) { + buildConfigTopic(); if (mainDevice) mainDevice->buildConfig(jsonDoc); if (entityCategory) jsonDoc["entity_category"] = entityCategory; + if (deviceClass) jsonDoc["device_class"] = deviceClass; jsonDoc["name"] = name; char uniqueId[50]; buildUniqueId(uniqueId); @@ -184,9 +189,7 @@ namespace Ha { void buildConfig(JsonDocument& jsonDoc) override { EntityConfig::buildConfig(jsonDoc); - if (entityCategory) jsonDoc["entity_category"] = entityCategory; - jsonDoc["device_class"] = deviceClass; - jsonDoc["unit_of_measurement"] = unitMeasure; + if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure; if (valueTemplate) jsonDoc["value_template"] = valueTemplate; jsonDoc["state_topic"] = stateTopic; jsonDoc["suggested_display_precision"] = 2; @@ -241,12 +244,4 @@ namespace Ha { 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 }}"; - // } - // }; } diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index cd4950b..66c2187 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -13,7 +13,6 @@ namespace Mqtt { AsyncMqttClient client; void publishInit(); - void publishBme280(); void disconnect(); Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, []() { Serial.println("Connecting to MQTT..."); @@ -50,11 +49,8 @@ namespace Mqtt { }; Ha::Sensor* sensors[] = { - SensorBuilder::withVoltage((new Ha::TemperatureSensor{"4"})->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank room")->withArea("Basement")) - // new Ha::TemperatureSensor{"Temperature", "temperature"}, - // new Ha::HumiditySensor{"Humidity", "humidity"}, - // new Ha::PressureSensor{"Pressure", "pressure"}, - // new Ha::AltitudeSensor{"Altitude", "altitude"} + SensorBuilder::withVoltage((new Ha::TemperatureSensor{"4"})->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank room")->withArea("Basement")), + OilSensorBuilder::build("7") }; 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) { char msg[len + 1]; memcpy(msg, payload, len); diff --git a/rc-gateway.code-workspace b/rc-gateway.code-workspace index 6297c6b..53d97a2 100644 --- a/rc-gateway.code-workspace +++ b/rc-gateway.code-workspace @@ -16,6 +16,7 @@ ], "settings": { "files.associations": { + "*.yaml": "esphome", "functional": "cpp", "queue": "cpp", "array": "cpp", @@ -49,7 +50,8 @@ "algorithm": "cpp", "iterator": "cpp", "memory": "cpp", - "variant": "cpp" + "variant": "cpp", + "string_view": "cpp" } } } \ No newline at end of file