forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for Tuya pink version of miflora (esphome#5402)
- Loading branch information
Showing
6 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CODEOWNERS = ["@fariouche"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters