Skip to content

Instantly share code, notes, and snippets.

@denpamusic
Last active March 26, 2022 22:57
Show Gist options
  • Save denpamusic/863a08c69d2fd6fb33b9ab8095e13f16 to your computer and use it in GitHub Desktop.
Save denpamusic/863a08c69d2fd6fb33b9ab8095e13f16 to your computer and use it in GitHub Desktop.
Custom ESPHome component for RadSens Board by ClimateGuard
#include "esphome.h"
#include "RadSensBoard.h"
class RadSens_Sensor : public PollingComponent {
public:
RadSensBoard radSensBoard;
Sensor *radiation_dynamic_sensor = new Sensor();
Sensor *radiation_static_sensor = new Sensor();
Sensor *calibration_value_sensor = new Sensor();
RadSens_Sensor() : PollingComponent(5000) { }
void setup() override {
if (!radSensBoard.init()) {
ESP_LOGE("radsens", "Component radsens.sensor init failed.");
this->mark_failed();
return;
}
}
void update() override {
float radiation_dynamic = abs(radSensBoard.getRadiationLevelDynamic());
radiation_dynamic_sensor->publish_state(radiation_dynamic);
float radiation_static = abs(radSensBoard.getRadiationLevelStatic());
radiation_static_sensor->publish_state(radiation_static);
uint16_t calibration_value = radSensBoard.getCalibrationValue();
calibration_value_sensor->publish_state(calibration_value);
}
};
class HighVoltageGeneratorSwitch : public Switch {
public:
RadSensBoard radSensBoard;
bool state = radSensBoard.getHighVoltageGeneratorState();
void write_state(bool state) override {
radSensBoard.setHighVoltageGeneratorState(state);
publish_state(state);
}
};
class LedIndicatorSwitch : public Switch {
public:
RadSensBoard radSensBoard;
bool state = radSensBoard.getLedIndicationState();
void write_state(bool state) override {
radSensBoard.setLedIndicationState(state);
publish_state(state);
}
};
class RadSens_Switch : public Component {
public:
RadSensBoard radSensBoard;
Switch *hv_generator_switch = new HighVoltageGeneratorSwitch();
Switch *led_indicator_switch = new LedIndicatorSwitch();
void setup() override {
if (!radSensBoard.init()) {
ESP_LOGE("radsens", "Component radsens.switch init failed.");
this->mark_failed();
return;
}
}
};
i2c:
sensor:
- platform: custom
lambda: |-
auto radsens_sensor = new RadSens_Sensor();
App.register_component(radsens_sensor);
return {radsens_sensor->radiation_dynamic_sensor, radsens_sensor->radiation_static_sensor, radsens_sensor->calibration_value_sensor};
sensors:
- name: "Dynamic Intensity"
accuracy_decimals: 1
unit_of_measurement: "μR/h"
icon: "mdi:radioactive"
state_class: "measurement"
- name: "Static Intensity"
accuracy_decimals: 1
unit_of_measurement: "μR/h"
icon: "mdi:radioactive"
state_class: "measurement"
- name: "Calibration Value"
accuracy_decimals: 0
unit_of_measurement: "imp/μR"
switch:
- platform: custom
lambda: |-
auto radsens_switch = new RadSens_Switch();
App.register_component(radsens_switch);
return {radsens_switch->hv_generator_switch, radsens_switch->led_indicator_switch};
switches:
- name: "High Voltage Generator Switch"
- name: "LED Indicator Switch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment