From b5e13c9ba26c4ffb3e0d493942712dfe36d939cc Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 16 May 2024 10:13:36 +0200 Subject: [PATCH] uniqueId, configTopic & commandTopic: - for sensors, use deviceClass as part of uniqueId & configTopic - use same commandTopic for all Commands - with /set at the end --- gateway/include/devices.h | 37 +++++++++++---------------------- gateway/include/ha.h | 43 ++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 48 deletions(-) diff --git a/gateway/include/devices.h b/gateway/include/devices.h index 0a51ba3..c3b8599 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -25,29 +25,6 @@ struct OilTankRoomSensorBuilder { } }; -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) { auto device = (new DeviceConfig{ id }) @@ -56,9 +33,17 @@ struct OilSensorBuilder { ->withModel("Pro Mini") ->withArea("Basement") ->withParent(gatewayDevice); - return Builder::instance(id) + return Builder::instance(new Sensor{ "Depth", id }) .asDevice(device) - .withSecondary(new OilTankLevelSensor(id)) + .withDeviceClass("distance") + .withUnitMseasure("cm") + .withValueTemplate("{{ value_json.sensor.value }}") + .withSecondary( + Builder::instance(new Sensor{ "Level", id }) + .withUnitMseasure("%") + .withValueTemplate("{{ 100 - ((value_json.sensor.value-7)|float*100/110)|round(2) }}") + .build() + ) .withDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}) .withDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"}) .build(); @@ -72,6 +57,7 @@ struct PollinSwitch : Switch { PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr) : Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) { mainDevice = (new DeviceConfig{id})->withName(name)->withManufacturer("Pollin")->withArea(area)->withParent(gatewayDevice); + deviceClass = "outlet"; p1Switches.insert({ string(id), this }); } @@ -91,6 +77,7 @@ struct EasyHomeSwitch : Switch { memcpy(&this->on[4], on, 4 * sizeof(unsigned long)); memcpy(&this->off[4], off, 4 * sizeof(unsigned long)); mainDevice = (new DeviceConfig{id})->withName(name)->withManufacturer("Intertek")->withModel("Easy Home")->withArea(area)->withParent(gatewayDevice); + deviceClass = "outlet"; for (int i = 0; i < 8; i++) { onSwitches.insert({ this->on[i], this }); offSwitches.insert({ this->off[i], this }); diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 1897ea7..4ec2a45 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -72,18 +72,15 @@ namespace Ha { components.add(this); } - virtual void buildUniqueId(char* uniqueId) = 0; - + virtual void buildUniqueId(char* uniqueId) { + sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, id); + } + 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); - } + 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; @@ -91,6 +88,7 @@ namespace Ha { char uniqueId[50]; buildUniqueId(uniqueId); jsonDoc["unique_id"] = uniqueId; + buildConfigTopic(); } void publishConfig() { @@ -183,7 +181,7 @@ namespace Ha { static unordered_map mapCommands; Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) { - buildCommandTopic(); + sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id); mapCommands.insert({ string(commandTopic), this }); } @@ -191,14 +189,6 @@ namespace Ha { if (f) f(msg); } - virtual void buildCommandTopic() { - sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id); - } - - void buildUniqueId(char* uniqueId) override { - sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, this->id); - } - void buildConfig(JsonDocument& jsonDoc) override { Component::buildConfig(jsonDoc); jsonDoc["command_topic"] = commandTopic; @@ -227,14 +217,9 @@ namespace Ha { Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f) {} - void buildCommandTopic() override { - sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id); - } - void buildConfig(JsonDocument& jsonDoc) override { Command::buildConfig(jsonDoc); jsonDoc["name"] = nullptr; - jsonDoc["device_class"] = "outlet"; // jsonDoc["retain"] = true; if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; } @@ -257,7 +242,19 @@ namespace Ha { } void buildUniqueId(char* uniqueId) override { - sprintf(uniqueId, "%s_%s", deviceClass, id); + if (deviceClass) { + sprintf(uniqueId, "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id); + } else { + Component::buildUniqueId(uniqueId); + } + } + + void buildConfigTopic() override { + if (deviceClass) { + sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id); + } else { + Component::buildConfigTopic(); + } } void buildConfig(JsonDocument& jsonDoc) override {