Add Alarm Sensor | Fix Log Output

This commit is contained in:
Dotsch2005 2024-10-10 21:40:20 +02:00
parent 20910cc45e
commit 44a6a195af
5 changed files with 100 additions and 17 deletions

View file

@ -15,10 +15,45 @@ void RadonEyeRD200::handle_status_response_(const uint8_t *response, uint16_t le
radoneye_value_response_t *data = (radoneye_value_response_t *) response;
ESP_LOGD(TAG, " Measurements (Bq/m³) now: %u", data->latest_bq_m3);
ESP_LOGD(TAG, " Measurements Day (Bq/m³) avg: %u", data->day_avg_bq_m3);
ESP_LOGD(TAG, " Measurements Month (Bq/m³) avg: %u", data->month_avg_bq_m3);
ESP_LESP_LOGDOGW(TAG, " Measurements Peak (Bq/m³): %u", data->peak_bq_m3);
ESP_LOGD(TAG, " Model: %.6s", data->model);
ESP_LOGD(TAG, " Version: %.6s", data->version);
if (data->unit == 0) {
ESP_LOGD(TAG, " Unit: pCi/");
ESP_LOGD(TAG, " Measurements (pCi/) now: %u", data->latest_bq_m3);
ESP_LOGD(TAG, " Measurements Day (pCi/) avg: %u", data->day_avg_bq_m3);
ESP_LOGD(TAG, " Measurements Month (pCi/) avg: %u", data->month_avg_bq_m3);
ESP_LOGD(TAG, " Measurements Peak (pCi/): %u", data->peak_bq_m3);
} else {
ESP_LOGD(TAG, " Unit: Bq/m³");
ESP_LOGD(TAG, " Measurements (Bq/m³) now: %u", data->latest_bq_m3);
ESP_LOGD(TAG, " Measurements Day (Bq/m³) avg: %u", data->day_avg_bq_m3);
ESP_LOGD(TAG, " Measurements Month (Bq/m³) avg: %u", data->month_avg_bq_m3);
ESP_LOGD(TAG, " Measurements Peak (Bq/m³): %u", data->peak_bq_m3);
}
if (data->alarm != 0) {
ESP_LOGD(TAG, " AlarmStatus on!");
} else {
ESP_LOGD(TAG, " AlarmStatus off");
}
ESP_LOGD(TAG, " Alarm Value: %u", data->alarm_value);
if (data->alarm_interval == 1) {
ESP_LOGD(TAG, " Alarm Interval: 10min");
} else if (data->alarm_interval == 6) {
ESP_LOGD(TAG, " Alarm Interval: 6H");
} else if (data->alarm_interval == 24) {
ESP_LOGD(TAG, " Alarm Interval: 24H");
}
if (this->radon_version_ == 3) {
ESP_LOGD(TAG, " Barcode %.6s%.3s%.4s", (char *) data->serial_part2, (char *) data->serial_part1,
(char *) data->serial_part3);
} else {
ESP_LOGD(TAG, " Barcode %.3s%.6s%.4s", (char *) data->serial_part1, (char *) data->serial_part2,
(char *) data->serial_part3);
}
ESP_LOGV(TAG, " HexData: %s", format_hex_pretty(response, length).c_str());
float flatest_bq_m3 = (float) data->latest_bq_m3;
float fday_avg_bq_m3 = (float) data->day_avg_bq_m3;
@ -53,6 +88,18 @@ void RadonEyeRD200::handle_status_response_(const uint8_t *response, uint16_t le
} else {
ESP_LOGI(TAG, " Sensor radon_peak not exists!");
}
if (alarm_sensor_ != nullptr) {
ESP_LOGD(TAG, " Sensor alarm send!");
if (data->alarm != 0) {
alarm_sensor_->publish_state(true);
} else {
alarm_sensor_->publish_state(false);
}
delay(25);
} else {
ESP_LOGI(TAG, " Sensor alarm not exists!");
}
return;
}
@ -282,8 +329,8 @@ void RadonEyeRD200::update() {
}
} else {
if (this->radon_version_ == 2 || this->radon_version_ == 3) {
ESP_LOGV(TAG, "Update Version 2 or 3");
write_status_query_message_();
ESP_LOGV(TAG, "Update Version 2 or 3");
write_status_query_message_();
}
}
}
@ -321,6 +368,17 @@ void RadonEyeRD200::write_history_query_message_() {
}
}
void RadonEyeRD200::write_beep_query_message_() {
ESP_LOGD(TAG, "writing 0xA1 to command service");
int request = 0xA1;
auto status = esp_ble_gattc_write_char_descr(this->parent()->get_gattc_if(), this->parent()->get_conn_id(),
this->command_handle_, sizeof(request), (uint8_t *) &request,
ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
if (status) {
ESP_LOGW(TAG, "Error sending command request for sensor, status=%d", status);
}
}
void RadonEyeRD200::request_read_values_() {
auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(),
this->read_handle_, ESP_GATT_AUTH_REQ_NONE);

View file

@ -8,8 +8,10 @@
#include "esphome/components/ble_client/ble_client.h"
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/core/component.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace radon_eye_rd200 {
@ -33,13 +35,16 @@ typedef struct {
/* 15 */ uint8_t __unk1[1];
/* 16 */ char model[6];
/* 22 */ char version[6];
/* 28 */ uint8_t __unk2[5];
/* 28 */ uint8_t unit;
/* 29 */ uint8_t alarm;
/* 30 */ uint16_t alarm_value;
/* 32 */ uint8_t alarm_interval;
/* 33 */ uint16_t latest_bq_m3;
/* 35 */ uint16_t day_avg_bq_m3;
/* 37 */ uint16_t month_avg_bq_m3;
/* 39 */ uint8_t __unk3[12];
/* 51 */ uint16_t peak_bq_m3;
/* 53 */ uint8_t __unk4[16]; // Length 15 or 16? Maybe because of version 3
/* 53 */ uint8_t __unk4[16];
} __attribute__((packed)) radoneye_value_response_t;
typedef struct {
@ -51,7 +56,6 @@ typedef struct {
} __attribute__((packed)) radoneye_history_response_t;
/* Thanks to sormy https://github.com/esphome/issues/issues/3371#issuecomment-1851004514 */
class RadonEyeRD200 : public PollingComponent, public ble_client::BLEClientNode {
public:
RadonEyeRD200();
@ -62,7 +66,8 @@ class RadonEyeRD200 : public PollingComponent, public ble_client::BLEClientNode
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t *param) override;
void set_version(int version) { radon_version_ = version; }
void set_version(int version) { radon_version_ = version; }
void set_alarm(binary_sensor::BinarySensor *alarm) { alarm_sensor_ = alarm; }
void set_radon(sensor::Sensor *radon) { radon_sensor_ = radon; }
void set_radon_day_avg(sensor::Sensor *radon_day_avg) { radon_day_avg_ = radon_day_avg; }
void set_radon_long_term(sensor::Sensor *radon_long_term) { radon_long_term_sensor_ = radon_long_term; }
@ -79,9 +84,11 @@ class RadonEyeRD200 : public PollingComponent, public ble_client::BLEClientNode
void write_query_message_();
void write_status_query_message_();
void write_history_query_message_();
void write_beep_query_message_();
void request_read_values_();
int radon_version_{1};
binary_sensor::BinarySensor *alarm_sensor_{nullptr};
sensor::Sensor *radon_sensor_{nullptr};
sensor::Sensor *radon_day_avg_{nullptr};
sensor::Sensor *radon_long_term_sensor_{nullptr};

View file

@ -1,20 +1,21 @@
import esphome.codegen as cg
from esphome.components import binary_sensor, ble_client, sensor
import esphome.config_validation as cv
from esphome.components import sensor, ble_client
from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_BECQUEREL_PER_CUBIC_METER,
CONF_ALARM,
CONF_ID,
CONF_VERSION,
CONF_RADON,
CONF_RADON_DAY_AVG,
CONF_RADON_LONG_TERM,
CONF_RADON_PEAK,
CONF_VERSION,
ICON_ALARM_LIGHT,
ICON_RADIOACTIVE,
STATE_CLASS_MEASUREMENT,
UNIT_BECQUEREL_PER_CUBIC_METER,
)
DEPENDENCIES = ["ble_client"]
DEPENDENCIES = ["ble_client", "binary_sensor"]
radon_eye_rd200_ns = cg.esphome_ns.namespace("radon_eye_rd200")
RadonEyeRD200 = radon_eye_rd200_ns.class_(
@ -26,6 +27,9 @@ CONFIG_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(RadonEyeRD200),
cv.Optional(CONF_VERSION, default=1): cv.int_range(1, 3),
cv.Optional(CONF_ALARM, default=0): binary_sensor.binary_sensor_schema(
icon=ICON_ALARM_LIGHT,
),
cv.Optional(CONF_RADON): sensor.sensor_schema(
unit_of_measurement=UNIT_BECQUEREL_PER_CUBIC_METER,
icon=ICON_RADIOACTIVE,
@ -65,6 +69,9 @@ async def to_code(config):
cg.add(var.set_version(config[CONF_VERSION]))
if CONF_ALARM in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_ALARM])
cg.add(var.set_alarm(sens))
if CONF_RADON in config:
sens = await sensor.new_sensor(config[CONF_RADON])
cg.add(var.set_radon(sens))

View file

@ -49,6 +49,7 @@ CONF_ADDRESS = "address"
CONF_ADDRESSABLE_LIGHT_ID = "addressable_light_id"
CONF_ADVANCED = "advanced"
CONF_AFTER = "after"
CONF_ALARM = "alarm"
CONF_ALLOW_OTHER_USES = "allow_other_uses"
CONF_ALPHA = "alpha"
CONF_ALTITUDE = "altitude"
@ -691,9 +692,9 @@ CONF_PULSE_LENGTH = "pulse_length"
CONF_QOS = "qos"
CONF_QUANTILE = "quantile"
CONF_RADON = "radon"
CONF_RADON_DAY_AVG = "radon_day_avg"
CONF_RADON_LONG_TERM = "radon_long_term"
CONF_RADON_PEAK = "radon_peak"
CONF_RADON_DAY_AVG = "radon_day_avg"
CONF_RANDOM = "random"
CONF_RANGE = "range"
CONF_RANGE_FROM = "range_from"
@ -961,6 +962,7 @@ ICON_ACCELERATION = "mdi:axis-arrow"
ICON_ACCELERATION_X = "mdi:axis-x-arrow"
ICON_ACCELERATION_Y = "mdi:axis-y-arrow"
ICON_ACCELERATION_Z = "mdi:axis-z-arrow"
ICON_ALARM_LIGHT = "mdi:alarm-light"
ICON_ACCOUNT = "mdi:account"
ICON_ACCOUNT_CHECK = "mdi:account-check"
ICON_AIR_FILTER = "mdi:air-filter"

View file

@ -4,11 +4,20 @@ ble_client:
- mac_address: 01:02:03:04:05:06
id: radon_eye_blec
binary_sensor:
sensor:
- platform: radon_eye_rd200
version: 1
ble_client_id: radon_eye_blec
radon:
name: RD200 Radon
radon_day_avg:
name: RD200 Radon Day average
radon_long_term:
name: RD200 Radon Long Term
radon_peak:
name: RD200 Radon Peak
alarm:
name: Alarm
update_interval: 10min