mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 06:58:11 +01:00
Adds support for b-parasite soil moisture sensor (#1666)
This commit is contained in:
parent
99f14e03d4
commit
bf01c22e1f
6 changed files with 201 additions and 0 deletions
|
@ -18,6 +18,7 @@ esphome/components/animation/* @syndlex
|
||||||
esphome/components/api/* @OttoWinter
|
esphome/components/api/* @OttoWinter
|
||||||
esphome/components/async_tcp/* @OttoWinter
|
esphome/components/async_tcp/* @OttoWinter
|
||||||
esphome/components/atc_mithermometer/* @ahpohl
|
esphome/components/atc_mithermometer/* @ahpohl
|
||||||
|
esphome/components/b_parasite/* @rbaron
|
||||||
esphome/components/bang_bang/* @OttoWinter
|
esphome/components/bang_bang/* @OttoWinter
|
||||||
esphome/components/binary_sensor/* @esphome/core
|
esphome/components/binary_sensor/* @esphome/core
|
||||||
esphome/components/bme680_bsec/* @trvrnrth
|
esphome/components/bme680_bsec/* @trvrnrth
|
||||||
|
|
0
esphome/components/b_parasite/__init__.py
Normal file
0
esphome/components/b_parasite/__init__.py
Normal file
82
esphome/components/b_parasite/b_parasite.cpp
Normal file
82
esphome/components/b_parasite/b_parasite.cpp
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
#include "b_parasite.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace b_parasite {
|
||||||
|
|
||||||
|
static const char* TAG = "b_parasite";
|
||||||
|
|
||||||
|
void BParasite::dump_config() {
|
||||||
|
ESP_LOGCONFIG(TAG, "b_parasite");
|
||||||
|
LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_);
|
||||||
|
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||||
|
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
||||||
|
LOG_SENSOR(" ", "Soil Moisture", this->soil_moisture_);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BParasite::parse_device(const esp32_ble_tracker::ESPBTDevice& device) {
|
||||||
|
if (device.address_uint64() != 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());
|
||||||
|
const auto& service_datas = device.get_service_datas();
|
||||||
|
if (service_datas.size() != 1) {
|
||||||
|
ESP_LOGE(TAG, "Unexpected service_datas size (%d)", service_datas.size());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto& service_data = service_datas[0];
|
||||||
|
|
||||||
|
ESP_LOGVV(TAG, "Service data:");
|
||||||
|
for (const uint8_t byte : service_data.data) {
|
||||||
|
ESP_LOGVV(TAG, "0x%02x", byte);
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& data = service_data.data;
|
||||||
|
|
||||||
|
// Counter for deduplicating messages.
|
||||||
|
uint8_t counter = data[1] & 0x0f;
|
||||||
|
if (last_processed_counter_ == counter) {
|
||||||
|
ESP_LOGVV(TAG, "Skipping already processed counter (%u)", counter);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Battery voltage in millivolts.
|
||||||
|
uint16_t battery_millivolt = data[2] << 8 | data[3];
|
||||||
|
float battery_voltage = battery_millivolt / 1000.0f;
|
||||||
|
|
||||||
|
// Temperature in 1000 * Celcius.
|
||||||
|
uint16_t temp_millicelcius = data[4] << 8 | data[5];
|
||||||
|
float temp_celcius = temp_millicelcius / 1000.0f;
|
||||||
|
|
||||||
|
// Relative air humidity in the range [0, 2^16).
|
||||||
|
uint16_t humidity = data[6] << 8 | data[7];
|
||||||
|
float humidity_percent = (100.0f * humidity) / (1 << 16);
|
||||||
|
|
||||||
|
// Relative soil moisture in [0 - 2^16).
|
||||||
|
uint16_t soil_moisture = data[8] << 8 | data[9];
|
||||||
|
float moisture_percent = (100.0f * soil_moisture) / (1 << 16);
|
||||||
|
|
||||||
|
if (battery_voltage_ != nullptr) {
|
||||||
|
battery_voltage_->publish_state(battery_voltage);
|
||||||
|
}
|
||||||
|
if (temperature_ != nullptr) {
|
||||||
|
temperature_->publish_state(temp_celcius);
|
||||||
|
}
|
||||||
|
if (humidity_ != nullptr) {
|
||||||
|
humidity_->publish_state(humidity_percent);
|
||||||
|
}
|
||||||
|
if (soil_moisture_ != nullptr) {
|
||||||
|
soil_moisture_->publish_state(moisture_percent);
|
||||||
|
}
|
||||||
|
|
||||||
|
last_processed_counter_ = counter;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace b_parasite
|
||||||
|
} // namespace esphome
|
||||||
|
|
||||||
|
#endif // ARDUINO_ARCH_ESP32
|
40
esphome/components/b_parasite/b_parasite.h
Normal file
40
esphome/components/b_parasite/b_parasite.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
#include "esphome/components/sensor/sensor.h"
|
||||||
|
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
||||||
|
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace b_parasite {
|
||||||
|
|
||||||
|
class BParasite : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||||
|
public:
|
||||||
|
void set_address(uint64_t address) { address_ = address; };
|
||||||
|
void set_bindkey(const std::string &bindkey);
|
||||||
|
|
||||||
|
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_battery_voltage(sensor::Sensor *battery_voltage) { battery_voltage_ = battery_voltage; }
|
||||||
|
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
|
||||||
|
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
|
||||||
|
void set_soil_moisture(sensor::Sensor *soil_moisture) { soil_moisture_ = soil_moisture; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// The received advertisement packet contains an unsigned 4 bits wrap-around counter
|
||||||
|
// for deduplicating messages.
|
||||||
|
int8_t last_processed_counter_ = -1;
|
||||||
|
uint64_t address_;
|
||||||
|
sensor::Sensor *battery_voltage_{nullptr};
|
||||||
|
sensor::Sensor *temperature_{nullptr};
|
||||||
|
sensor::Sensor *humidity_{nullptr};
|
||||||
|
sensor::Sensor *soil_moisture_{nullptr};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace b_parasite
|
||||||
|
} // namespace esphome
|
||||||
|
|
||||||
|
#endif // ARDUINO_ARCH_ESP32
|
68
esphome/components/b_parasite/sensor.py
Normal file
68
esphome/components/b_parasite/sensor.py
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
import esphome.codegen as cg
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.components import sensor, esp32_ble_tracker
|
||||||
|
from esphome.const import (
|
||||||
|
CONF_BATTERY_VOLTAGE,
|
||||||
|
CONF_HUMIDITY,
|
||||||
|
CONF_ID,
|
||||||
|
CONF_MOISTURE,
|
||||||
|
CONF_MAC_ADDRESS,
|
||||||
|
CONF_TEMPERATURE,
|
||||||
|
DEVICE_CLASS_HUMIDITY,
|
||||||
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
|
DEVICE_CLASS_VOLTAGE,
|
||||||
|
ICON_EMPTY,
|
||||||
|
UNIT_CELSIUS,
|
||||||
|
UNIT_PERCENT,
|
||||||
|
UNIT_VOLT,
|
||||||
|
)
|
||||||
|
|
||||||
|
CODEOWNERS = ["@rbaron"]
|
||||||
|
|
||||||
|
DEPENDENCIES = ["esp32_ble_tracker"]
|
||||||
|
|
||||||
|
b_parasite_ns = cg.esphome_ns.namespace("b_parasite")
|
||||||
|
BParasite = b_parasite_ns.class_(
|
||||||
|
"BParasite", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
|
||||||
|
)
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = (
|
||||||
|
cv.Schema(
|
||||||
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(BParasite),
|
||||||
|
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
|
||||||
|
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||||
|
UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
||||||
|
UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema(
|
||||||
|
UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_MOISTURE): sensor.sensor_schema(
|
||||||
|
UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
|
||||||
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def to_code(config):
|
||||||
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
|
yield cg.register_component(var, config)
|
||||||
|
yield esp32_ble_tracker.register_ble_device(var, config)
|
||||||
|
|
||||||
|
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
|
||||||
|
|
||||||
|
for (config_key, setter) in [
|
||||||
|
(CONF_TEMPERATURE, var.set_temperature),
|
||||||
|
(CONF_HUMIDITY, var.set_humidity),
|
||||||
|
(CONF_BATTERY_VOLTAGE, var.set_battery_voltage),
|
||||||
|
(CONF_MOISTURE, var.set_soil_moisture),
|
||||||
|
]:
|
||||||
|
if config_key in config:
|
||||||
|
sens = yield sensor.new_sensor(config[config_key])
|
||||||
|
cg.add(setter(sens))
|
|
@ -70,6 +70,16 @@ sensor:
|
||||||
- platform: ble_rssi
|
- platform: ble_rssi
|
||||||
service_uuid: '11223344-5566-7788-99aa-bbccddeeff00'
|
service_uuid: '11223344-5566-7788-99aa-bbccddeeff00'
|
||||||
name: 'BLE Test Service 128'
|
name: 'BLE Test Service 128'
|
||||||
|
- platform: b_parasite
|
||||||
|
mac_address: F0:CA:F0:CA:01:01
|
||||||
|
humidity:
|
||||||
|
name: 'b-parasite Air Humidity'
|
||||||
|
temperature:
|
||||||
|
name: 'b-parasite Air Temperature'
|
||||||
|
moisture:
|
||||||
|
name: 'b-parasite Soil Moisture'
|
||||||
|
battery_voltage:
|
||||||
|
name: 'b-parasite Battery Voltage'
|
||||||
- platform: senseair
|
- platform: senseair
|
||||||
id: senseair0
|
id: senseair0
|
||||||
co2:
|
co2:
|
||||||
|
|
Loading…
Reference in a new issue