mirror of
https://github.com/esphome/esphome.git
synced 2024-11-09 16:57:47 +01:00
Added support for pvvx_mithermometer sensor (#1546)
Co-authored-by: Pasi Suominen <pasiz@pasizdesk.pasiz.net> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
5379794f16
commit
90394a50df
6 changed files with 276 additions and 0 deletions
|
@ -88,6 +88,7 @@ esphome/components/pn532_i2c/* @OttoWinter @jesserockz
|
|||
esphome/components/pn532_spi/* @OttoWinter @jesserockz
|
||||
esphome/components/power_supply/* @esphome/core
|
||||
esphome/components/pulse_meter/* @stevebaxter
|
||||
esphome/components/pvvx_mithermometer/* @pasiz
|
||||
esphome/components/rc522/* @glmnet
|
||||
esphome/components/rc522_i2c/* @glmnet
|
||||
esphome/components/rc522_spi/* @glmnet
|
||||
|
|
0
esphome/components/pvvx_mithermometer/__init__.py
Normal file
0
esphome/components/pvvx_mithermometer/__init__.py
Normal file
145
esphome/components/pvvx_mithermometer/pvvx_mithermometer.cpp
Normal file
145
esphome/components/pvvx_mithermometer/pvvx_mithermometer.cpp
Normal file
|
@ -0,0 +1,145 @@
|
|||
#include "pvvx_mithermometer.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace pvvx_mithermometer {
|
||||
|
||||
static const char *TAG = "pvvx_mithermometer";
|
||||
|
||||
void PVVXMiThermometer::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "PVVX MiThermometer");
|
||||
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
||||
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
|
||||
LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_);
|
||||
}
|
||||
|
||||
bool PVVXMiThermometer::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()) {
|
||||
auto res = parse_header(service_data);
|
||||
if (res->is_duplicate) {
|
||||
continue;
|
||||
}
|
||||
if (!(parse_message(service_data.data, *res))) {
|
||||
continue;
|
||||
}
|
||||
if (!(report_results(res, device.address_str()))) {
|
||||
continue;
|
||||
}
|
||||
if (res->temperature.has_value() && this->temperature_ != nullptr)
|
||||
this->temperature_->publish_state(*res->temperature);
|
||||
if (res->humidity.has_value() && this->humidity_ != nullptr)
|
||||
this->humidity_->publish_state(*res->humidity);
|
||||
if (res->battery_level.has_value() && this->battery_level_ != nullptr)
|
||||
this->battery_level_->publish_state(*res->battery_level);
|
||||
if (res->battery_voltage.has_value() && this->battery_voltage_ != nullptr)
|
||||
this->battery_voltage_->publish_state(*res->battery_voltage);
|
||||
success = true;
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
optional<ParseResult> PVVXMiThermometer::parse_header(const esp32_ble_tracker::ServiceData &service_data) {
|
||||
ParseResult result;
|
||||
if (!service_data.uuid.contains(0x1A, 0x18)) {
|
||||
ESP_LOGVV(TAG, "parse_header(): no service data UUID magic bytes.");
|
||||
return {};
|
||||
}
|
||||
|
||||
auto raw = service_data.data;
|
||||
|
||||
static uint8_t last_frame_count = 0;
|
||||
if (last_frame_count == raw[13]) {
|
||||
ESP_LOGVV(TAG, "parse_header(): duplicate data packet received (%d).", static_cast<int>(last_frame_count));
|
||||
result.is_duplicate = true;
|
||||
return {};
|
||||
}
|
||||
last_frame_count = raw[13];
|
||||
result.is_duplicate = false;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PVVXMiThermometer::parse_message(const std::vector<uint8_t> &message, ParseResult &result) {
|
||||
/*
|
||||
All data little endian
|
||||
uint8_t size; // = 19
|
||||
uint8_t uid; // = 0x16, 16-bit UUID
|
||||
uint16_t UUID; // = 0x181A, GATT Service 0x181A Environmental Sensing
|
||||
uint8_t MAC[6]; // [0] - lo, .. [5] - hi digits
|
||||
int16_t temperature; // x 0.01 degree [6,7]
|
||||
uint16_t humidity; // x 0.01 % [8,9]
|
||||
uint16_t battery_mv; // mV [10,11]
|
||||
uint8_t battery_level; // 0..100 % [12]
|
||||
uint8_t counter; // measurement count [13]
|
||||
uint8_t flags; [14]
|
||||
*/
|
||||
|
||||
const uint8_t *data = message.data();
|
||||
const int data_length = 15;
|
||||
|
||||
if (message.size() != data_length) {
|
||||
ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", message.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
// int16_t temperature; // x 0.01 degree [6,7]
|
||||
const int16_t temperature = int16_t(data[6]) | (int16_t(data[7]) << 8);
|
||||
result.temperature = temperature / 1.0e2f;
|
||||
|
||||
// uint16_t humidity; // x 0.01 % [8,9]
|
||||
const int16_t humidity = uint16_t(data[8]) | (uint16_t(data[9]) << 8);
|
||||
result.humidity = humidity / 1.0e2f;
|
||||
|
||||
// uint16_t battery_mv; // mV [10,11]
|
||||
const int16_t battery_voltage = uint16_t(data[10]) | (uint16_t(data[11]) << 8);
|
||||
result.battery_voltage = battery_voltage / 1.0e3f;
|
||||
|
||||
// uint8_t battery_level; // 0..100 % [12]
|
||||
result.battery_level = uint8_t(data[12]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PVVXMiThermometer::report_results(const optional<ParseResult> &result, const std::string &address) {
|
||||
if (!result.has_value()) {
|
||||
ESP_LOGVV(TAG, "report_results(): no results available.");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Got PVVX MiThermometer (%s):", address.c_str());
|
||||
|
||||
if (result->temperature.has_value()) {
|
||||
ESP_LOGD(TAG, " Temperature: %.2f °C", *result->temperature);
|
||||
}
|
||||
if (result->humidity.has_value()) {
|
||||
ESP_LOGD(TAG, " Humidity: %.2f %%", *result->humidity);
|
||||
}
|
||||
if (result->battery_level.has_value()) {
|
||||
ESP_LOGD(TAG, " Battery Level: %.0f %%", *result->battery_level);
|
||||
}
|
||||
if (result->battery_voltage.has_value()) {
|
||||
ESP_LOGD(TAG, " Battery Voltage: %.3f V", *result->battery_voltage);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace pvvx_mithermometer
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
48
esphome/components/pvvx_mithermometer/pvvx_mithermometer.h
Normal file
48
esphome/components/pvvx_mithermometer/pvvx_mithermometer.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
#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 pvvx_mithermometer {
|
||||
|
||||
struct ParseResult {
|
||||
optional<float> temperature;
|
||||
optional<float> humidity;
|
||||
optional<float> battery_level;
|
||||
optional<float> battery_voltage;
|
||||
bool is_duplicate;
|
||||
int raw_offset;
|
||||
};
|
||||
|
||||
class PVVXMiThermometer : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { 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) { temperature_ = temperature; }
|
||||
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
|
||||
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
|
||||
void set_battery_voltage(sensor::Sensor *battery_voltage) { battery_voltage_ = battery_voltage; }
|
||||
|
||||
protected:
|
||||
uint64_t address_;
|
||||
sensor::Sensor *temperature_{nullptr};
|
||||
sensor::Sensor *humidity_{nullptr};
|
||||
sensor::Sensor *battery_level_{nullptr};
|
||||
sensor::Sensor *battery_voltage_{nullptr};
|
||||
|
||||
optional<ParseResult> parse_header(const esp32_ble_tracker::ServiceData &service_data);
|
||||
bool parse_message(const std::vector<uint8_t> &message, ParseResult &result);
|
||||
bool report_results(const optional<ParseResult> &result, const std::string &address);
|
||||
};
|
||||
|
||||
} // namespace pvvx_mithermometer
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
72
esphome/components/pvvx_mithermometer/sensor.py
Normal file
72
esphome/components/pvvx_mithermometer/sensor.py
Normal file
|
@ -0,0 +1,72 @@
|
|||
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_LEVEL,
|
||||
CONF_BATTERY_VOLTAGE,
|
||||
CONF_MAC_ADDRESS,
|
||||
CONF_HUMIDITY,
|
||||
CONF_TEMPERATURE,
|
||||
CONF_ID,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
ICON_EMPTY,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_PERCENT,
|
||||
UNIT_VOLT,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@pasiz"]
|
||||
|
||||
DEPENDENCIES = ["esp32_ble_tracker"]
|
||||
|
||||
pvvx_mithermometer_ns = cg.esphome_ns.namespace("pvvx_mithermometer")
|
||||
PVVXMiThermometer = pvvx_mithermometer_ns.class_(
|
||||
"PVVXMiThermometer", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(PVVXMiThermometer),
|
||||
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
|
||||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||
UNIT_CELSIUS, ICON_EMPTY, 2, DEVICE_CLASS_TEMPERATURE
|
||||
),
|
||||
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
||||
UNIT_PERCENT, ICON_EMPTY, 2, DEVICE_CLASS_HUMIDITY
|
||||
),
|
||||
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
|
||||
UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY
|
||||
),
|
||||
cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema(
|
||||
UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE
|
||||
),
|
||||
}
|
||||
)
|
||||
.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))
|
||||
|
||||
if CONF_TEMPERATURE in config:
|
||||
sens = yield sensor.new_sensor(config[CONF_TEMPERATURE])
|
||||
cg.add(var.set_temperature(sens))
|
||||
if CONF_HUMIDITY in config:
|
||||
sens = yield sensor.new_sensor(config[CONF_HUMIDITY])
|
||||
cg.add(var.set_humidity(sens))
|
||||
if CONF_BATTERY_LEVEL in config:
|
||||
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
|
||||
cg.add(var.set_battery_level(sens))
|
||||
if CONF_BATTERY_VOLTAGE in config:
|
||||
sens = yield sensor.new_sensor(config[CONF_BATTERY_VOLTAGE])
|
||||
cg.add(var.set_battery_voltage(sens))
|
|
@ -218,6 +218,16 @@ sensor:
|
|||
name: 'ATC Battery-Level'
|
||||
battery_voltage:
|
||||
name: 'ATC Battery-Voltage'
|
||||
- platform: pvvx_mithermometer
|
||||
mac_address: 'A4:C1:38:4E:16:78'
|
||||
temperature:
|
||||
name: 'PVVX Temperature'
|
||||
humidity:
|
||||
name: 'PVVX Humidity'
|
||||
battery_level:
|
||||
name: 'PVVX Battery-Level'
|
||||
battery_voltage:
|
||||
name: 'PVVX Battery-Voltage'
|
||||
- platform: inkbird_ibsth1_mini
|
||||
mac_address: 38:81:D7:0A:9C:11
|
||||
temperature:
|
||||
|
|
Loading…
Reference in a new issue