From 00b2ec93cb6986c5ffe789ae032b737a334562a4 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang <132141222+realzhiqiang@users.noreply.github.com> Date: Thu, 5 Oct 2023 20:55:07 -0400 Subject: [PATCH] [DISC] make mi scale weighing_mode and unit enum (#1766) * make mi scale weighing_mode and unit enum * lint --- main/ZgatewayBT.ino | 18 ++++++++++++++++++ main/ZmqttDiscovery.ino | 6 +++++- main/config_mqttDiscovery.h | 5 +++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index eed31ad..e6c4e8c 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -1108,6 +1108,24 @@ void launchBTDiscovery(bool overrideDiscovery) { Gateway_AnnouncementMsg, will_Message, false, subjectMQTTtoBTset, model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false, stateClassNone, "off", "on"); + } else if ((p->sensorModel_id == TheengsDecoder::XMTZC04HM || p->sensorModel_id == TheengsDecoder::XMTZC05HM) && + strcmp(prop.key().c_str(), "weighing_mode") == 0) { + createDiscovery("sensor", + discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(), + will_Topic, "enum", value_template.c_str(), + "", "", prop.value()["unit"], + 0, "", "", false, "", + model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false, + stateClassMeasurement, nullptr, nullptr, "[\"person\",\"object\"]"); + } else if ((p->sensorModel_id == TheengsDecoder::XMTZC04HM || p->sensorModel_id == TheengsDecoder::XMTZC05HM) && + strcmp(prop.key().c_str(), "unit") == 0) { + createDiscovery("sensor", + discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(), + will_Topic, "enum", value_template.c_str(), + "", "", prop.value()["unit"], + 0, "", "", false, "", + model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false, + stateClassMeasurement, nullptr, nullptr, "[\"lb\",\"kg\",\"jin\"]"); } else if (strcmp(prop.key().c_str(), "device") != 0 || strcmp(prop.key().c_str(), "mac") != 0) { // Exception on device and mac as these ones are not sensors createDiscovery("sensor", discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(), diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index d59e5ee..b2b5012 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -239,7 +239,7 @@ void createDiscovery(const char* sensor_type, int off_delay, const char* payload_available, const char* payload_not_available, bool gateway_entity, const char* cmd_topic, const char* device_name, const char* device_manufacturer, const char* device_model, const char* device_id, bool retainCmd, - const char* state_class, const char* state_off, const char* state_on) { + const char* state_class, const char* state_off, const char* state_on, const char* enum_options) { StaticJsonDocument jsonBuffer; JsonObject sensor = jsonBuffer.to(); @@ -344,6 +344,10 @@ void createDiscovery(const char* sensor_type, sensor["cmd_t"] = command_topic; //command_topic } + if (enum_options != nullptr) { + sensor["options"] = enum_options; + } + StaticJsonDocument jsonDeviceBuffer; JsonObject device = jsonDeviceBuffer.to(); JsonArray identifiers = device.createNestedArray("ids"); diff --git a/main/config_mqttDiscovery.h b/main/config_mqttDiscovery.h index d465b92..b1ad133 100644 --- a/main/config_mqttDiscovery.h +++ b/main/config_mqttDiscovery.h @@ -88,7 +88,7 @@ extern void createDiscovery(const char* sensor_type, int off_delay, const char* payload_available, const char* payload_not_available, bool gateway_entity, const char* command_topic, const char* device_name, const char* device_manufacturer, const char* device_model, const char* device_mac, bool retainCmd, - const char* state_class, const char* state_off = nullptr, const char* state_on = nullptr); + const char* state_class, const char* state_off = nullptr, const char* state_on = nullptr, const char* enum_options = nullptr); /** * @brief Create a message for Discovery Device Trigger. For HA @see https://www.home-assistant.io/integrations/device_trigger.mqtt/ @@ -196,7 +196,8 @@ const char* availableHASSClasses[] = {"battery", "current", "energy", "power_factor", - "voltage"}; + "voltage", + "enum"}; // From https://github.com/home-assistant/core/blob/d7ac4bd65379e11461c7ce0893d3533d8d8b8cbf/homeassistant/const.py#L379 // List of units available in Home Assistant