add support for Tuya pink version of miflora (#5402)

This commit is contained in:
fariouche 2024-04-09 00:17:51 +02:00 committed by GitHub
parent 708d5034cb
commit efc9fd060d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 216 additions and 0 deletions

View File

@ -404,6 +404,7 @@ esphome/components/wireguard/* @droscy @lhoracek @thomas0bernard
esphome/components/wl_134/* @hobbypunk90
esphome/components/x9c/* @EtienneMD
esphome/components/xgzp68xx/* @gcormier
esphome/components/xiaomi_hhccjcy10/* @fariouche
esphome/components/xiaomi_lywsd03mmc/* @ahpohl
esphome/components/xiaomi_mhoc303/* @drug123
esphome/components/xiaomi_mhoc401/* @vevsvevs

View File

@ -0,0 +1 @@
CODEOWNERS = ["@fariouche"]

View File

@ -0,0 +1,96 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, esp32_ble_tracker
from esphome.const import (
CONF_MAC_ADDRESS,
CONF_TEMPERATURE,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_TEMPERATURE,
ENTITY_CATEGORY_DIAGNOSTIC,
ICON_WATER_PERCENT,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_PERCENT,
CONF_ID,
CONF_MOISTURE,
CONF_ILLUMINANCE,
UNIT_LUX,
CONF_CONDUCTIVITY,
UNIT_MICROSIEMENS_PER_CENTIMETER,
ICON_FLOWER,
DEVICE_CLASS_BATTERY,
CONF_BATTERY_LEVEL,
)
DEPENDENCIES = ["esp32_ble_tracker"]
xiaomi_hhccjcy10_ns = cg.esphome_ns.namespace("xiaomi_hhccjcy10")
XiaomiHHCCJCY10 = xiaomi_hhccjcy10_ns.class_(
"XiaomiHHCCJCY10", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(XiaomiHHCCJCY10),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_MOISTURE): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon=ICON_WATER_PERCENT,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(
unit_of_measurement=UNIT_LUX,
accuracy_decimals=0,
device_class=DEVICE_CLASS_ILLUMINANCE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_CONDUCTIVITY): sensor.sensor_schema(
unit_of_measurement=UNIT_MICROSIEMENS_PER_CENTIMETER,
icon=ICON_FLOWER,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await esp32_ble_tracker.register_ble_device(var, config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature(sens))
if moisture_config := config.get(CONF_MOISTURE):
sens = await sensor.new_sensor(moisture_config)
cg.add(var.set_moisture(sens))
if illuminance_config := config.get(CONF_ILLUMINANCE):
sens = await sensor.new_sensor(illuminance_config)
cg.add(var.set_illuminance(sens))
if conductivity_config := config.get(CONF_CONDUCTIVITY):
sens = await sensor.new_sensor(conductivity_config)
cg.add(var.set_conductivity(sens))
if battery_level_config := config.get(CONF_BATTERY_LEVEL):
sens = await sensor.new_sensor(battery_level_config)
cg.add(var.set_battery_level(sens))

View File

@ -0,0 +1,68 @@
#include "xiaomi_hhccjcy10.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#ifdef USE_ESP32
namespace esphome {
namespace xiaomi_hhccjcy10 {
static const char *const TAG = "xiaomi_hhccjcy10";
void XiaomiHHCCJCY10::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi HHCCJCY10");
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Moisture", this->moisture_);
LOG_SENSOR(" ", "Conductivity", this->conductivity_);
LOG_SENSOR(" ", "Illuminance", this->illuminance_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
}
bool XiaomiHHCCJCY10::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
if (device.address_uint64() != this->address_) {
ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
return false;
}
ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
bool success = false;
for (auto &service_data : device.get_service_datas()) {
if (!service_data.uuid.contains(0x50, 0xFD)) {
ESP_LOGVV(TAG, "no tuya service data UUID.");
continue;
}
if (service_data.data.size() != 9) { // tuya alternate between two service data
continue;
}
const uint8_t *data = service_data.data.data();
if (this->temperature_ != nullptr) {
const int16_t temperature = encode_uint16(data[1], data[2]);
this->temperature_->publish_state((float) temperature / 10.0f);
}
if (this->moisture_ != nullptr)
this->moisture_->publish_state(data[0]);
if (this->conductivity_ != nullptr) {
const uint16_t conductivity = encode_uint16(data[7], data[8]);
this->conductivity_->publish_state((float) conductivity);
}
if (this->illuminance_ != nullptr) {
const uint32_t illuminance = encode_uint24(data[3], data[4], data[5]);
this->illuminance_->publish_state((float) illuminance);
}
if (this->battery_level_ != nullptr)
this->battery_level_->publish_state(data[6]);
success = true;
}
return success;
}
} // namespace xiaomi_hhccjcy10
} // namespace esphome
#endif

View File

@ -0,0 +1,38 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
#ifdef USE_ESP32
namespace esphome {
namespace xiaomi_hhccjcy10 {
class XiaomiHHCCJCY10 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
void set_address(uint64_t address) { this->address_ = address; }
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_temperature(sensor::Sensor *temperature) { this->temperature_ = temperature; }
void set_moisture(sensor::Sensor *moisture) { this->moisture_ = moisture; }
void set_conductivity(sensor::Sensor *conductivity) { this->conductivity_ = conductivity; }
void set_illuminance(sensor::Sensor *illuminance) { this->illuminance_ = illuminance; }
void set_battery_level(sensor::Sensor *battery_level) { this->battery_level_ = battery_level; }
protected:
uint64_t address_;
sensor::Sensor *temperature_{nullptr};
sensor::Sensor *moisture_{nullptr};
sensor::Sensor *conductivity_{nullptr};
sensor::Sensor *illuminance_{nullptr};
sensor::Sensor *battery_level_{nullptr};
};
} // namespace xiaomi_hhccjcy10
} // namespace esphome
#endif

View File

@ -203,6 +203,18 @@ sensor:
name: Xiaomi HHCCJCY01 Soil Conductivity
battery_level:
name: Xiaomi HHCCJCY01 Battery Level
- platform: xiaomi_hhccjcy10
mac_address: DD:25:6D:E4:FF:8F
temperature:
name: "Xiaomi HHCCJCY10 Temperature"
moisture:
name: "Xiaomi HHCCJCY10 Moisture"
illuminance:
name: "Xiaomi HHCCJCY10 Illuminance"
conductivity:
name: "Xiaomi HHCCJCY10 Soil Conductivity"
battery_level:
name: "Xiaomi HHCCJCY10 Battery Level"
- platform: xiaomi_lywsdcgq
mac_address: 7A:80:8E:19:36:BA
temperature: