mirror of
https://github.com/esphome/esphome.git
synced 2025-03-14 14:48:18 +00:00
Merge branch 'binsensmap_mpr121' into mpr121_threshold
This commit is contained in:
commit
88e72204bb
0
esphome/components/binary_sensor_map/__init__.py
Normal file
0
esphome/components/binary_sensor_map/__init__.py
Normal file
65
esphome/components/binary_sensor_map/binary_sensor_map.cpp
Normal file
65
esphome/components/binary_sensor_map/binary_sensor_map.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
#include "binary_sensor_map.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace binary_sensor_map {
|
||||
|
||||
static const char *TAG = "sensor.binary_sensor_map";
|
||||
|
||||
void BinarySensorMap::dump_config() { LOG_SENSOR(" ", "binary_sensor_map", this); }
|
||||
|
||||
void BinarySensorMap::loop() {
|
||||
switch (this->sensor_type_) {
|
||||
case BINARY_SENSOR_MAP_TYPE_GROUP:
|
||||
this->process_group_();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void BinarySensorMap::process_group_() {
|
||||
float total_current_value = 0.0;
|
||||
uint8_t num_active_sensors = 0;
|
||||
bool any_active = false;
|
||||
uint64_t mask = 0x00;
|
||||
// check all binary_sensors for its state. when active add its value to total_current_value.
|
||||
// create a bitmask for the binary_sensor status on all channels
|
||||
for (size_t i = 0; i < this->sensors_.size(); i++) {
|
||||
auto *bs = this->sensors_[i];
|
||||
if (bs->binary_sensor->state) {
|
||||
any_active = true;
|
||||
num_active_sensors++;
|
||||
total_current_value += bs->sensor_value;
|
||||
mask |= 1 << i;
|
||||
}
|
||||
}
|
||||
// check if the sensor map was touched
|
||||
if (any_active) {
|
||||
// did the bit_mask change or is it a new sensor touch
|
||||
if ((last_mask_ != mask) || !this->last_any_active_) {
|
||||
this->last_any_active_ = true;
|
||||
float publish_value = total_current_value / num_active_sensors;
|
||||
ESP_LOGD(TAG, "%s - Publishing %.2f", this->name_.c_str(), publish_value);
|
||||
this->publish_state(publish_value);
|
||||
this->last_mask_ = mask;
|
||||
}
|
||||
} else {
|
||||
// is this a new sensor release
|
||||
if (this->last_any_active_) {
|
||||
this->last_any_active_ = false;
|
||||
ESP_LOGD(TAG, "%s - No binary sensor active, publishing NAN", this->name_.c_str());
|
||||
this->publish_state(NAN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BinarySensorMap::add_channel(binary_sensor::BinarySensor *sensor, float value) {
|
||||
BinarySensorMapChannel *sensor_channel = new BinarySensorMapChannel;
|
||||
sensor_channel->binary_sensor = sensor;
|
||||
sensor_channel->sensor_value = value;
|
||||
this->sensors_.push_back(sensor_channel);
|
||||
}
|
||||
|
||||
void BinarySensorMap::set_sensor_type(BinarySensorMapType sensor_type) { this->sensor_type_ = sensor_type; }
|
||||
|
||||
} // namespace binary_sensor_map
|
||||
} // namespace esphome
|
59
esphome/components/binary_sensor_map/binary_sensor_map.h
Normal file
59
esphome/components/binary_sensor_map/binary_sensor_map.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace binary_sensor_map {
|
||||
|
||||
enum BinarySensorMapType {
|
||||
BINARY_SENSOR_MAP_TYPE_GROUP,
|
||||
};
|
||||
|
||||
struct BinarySensorMapChannel {
|
||||
binary_sensor::BinarySensor *binary_sensor;
|
||||
float sensor_value = 0;
|
||||
};
|
||||
|
||||
/** Class to group binary_sensors to one Sensor.
|
||||
*
|
||||
* Each binary sensor represents a float value in the group.
|
||||
*/
|
||||
class BinarySensorMap : public sensor::Sensor, public Component {
|
||||
public:
|
||||
void dump_config() override;
|
||||
/**
|
||||
* The loop checks all binary_sensor states
|
||||
* When the binary_sensor reports a true value for its state, then the float value it represents is added to the
|
||||
* total_current_value
|
||||
*
|
||||
* Only when the total_current_value changed and at least one sensor reports an active state we publish the sensors
|
||||
* average value. When the value changed and no sensors ar active we publish NAN.
|
||||
* */
|
||||
void loop() override;
|
||||
float get_setup_priority() const override { return setup_priority::LATE; }
|
||||
/** Add binary_sensors to the group.
|
||||
* Each binary_sensor represents a float value when its state is true
|
||||
*
|
||||
* @param *sensor The binary sensor.
|
||||
* @param value The value this binary_sensor represents
|
||||
*/
|
||||
void add_channel(binary_sensor::BinarySensor *sensor, float value);
|
||||
void set_sensor_type(BinarySensorMapType sensor_type);
|
||||
|
||||
protected:
|
||||
std::vector<BinarySensorMapChannel *> sensors_{};
|
||||
BinarySensorMapType sensor_type_{BINARY_SENSOR_MAP_TYPE_GROUP};
|
||||
bool last_any_active_{false};
|
||||
// this gives max 46 channels per binary_sensor_map
|
||||
uint64_t last_mask_{0x00};
|
||||
/**
|
||||
* methods to process the types of binary_sensor_maps
|
||||
* GROUP: process_group_() just map to a value
|
||||
* */
|
||||
void process_group_();
|
||||
};
|
||||
|
||||
} // namespace binary_sensor_map
|
||||
} // namespace esphome
|
38
esphome/components/binary_sensor_map/sensor.py
Normal file
38
esphome/components/binary_sensor_map/sensor.py
Normal file
@ -0,0 +1,38 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
|
||||
from esphome.components import sensor, binary_sensor
|
||||
from esphome.const import CONF_ID, CONF_CHANNELS, CONF_CHANNEL, CONF_VALUE, CONF_TYPE
|
||||
|
||||
DEPENDENCIES = ['binary_sensor']
|
||||
|
||||
binary_sensor_map_ns = cg.esphome_ns.namespace('binary_sensor_map')
|
||||
BinarySensorMap = binary_sensor_map_ns.class_('BinarySensorMap', cg.Component)
|
||||
SensorMapType = binary_sensor_map_ns.enum('SensorMapType')
|
||||
SENSOR_MAP_TYPES = {
|
||||
'GROUP': SensorMapType.BINARY_SENSOR_MAP_TYPE_GROUP,
|
||||
}
|
||||
|
||||
entry = {
|
||||
cv.Required(CONF_CHANNEL): cv.use_id(binary_sensor.BinarySensor),
|
||||
cv.Required(CONF_VALUE): cv.All(cv.positive_int, cv.Range(min=0, max=255)),
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(BinarySensorMap),
|
||||
cv.Required(CONF_TYPE): cv.one_of(*SENSOR_MAP_TYPES, upper=True),
|
||||
cv.Required(CONF_CHANNELS): cv.ensure_list(entry),
|
||||
})
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield sensor.register_sensor(var, config)
|
||||
if CONF_TYPE in config:
|
||||
constant = SENSOR_MAP_TYPES[config[CONF_TYPE]]
|
||||
cg.add(var.set_sensor_type(constant))
|
||||
|
||||
for ch in config[CONF_CHANNELS]:
|
||||
input_var = yield cg.get_variable(ch[CONF_CHANNEL])
|
||||
cg.add(var.add_channel(input_var, ch[CONF_VALUE]))
|
@ -19,6 +19,7 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield binary_sensor.register_binary_sensor(var, config)
|
||||
hub = yield cg.get_variable(config[CONF_MPR121_ID])
|
||||
|
||||
hub = yield cg.get_variable(config[CONF_MPR121_ID])
|
||||
cg.add(var.set_channel(config[CONF_CHANNEL]))
|
||||
|
@ -70,9 +70,9 @@ class MPR121Component : public Component, public i2c::I2CDevice {
|
||||
void set_touch_debounce(uint8_t debounce);
|
||||
void set_release_debounce(uint8_t debounce);
|
||||
void set_touch_threshold(uint8_t touch_threshold) { this->touch_threshold_ = touch_threshold; };
|
||||
void set_release_threshold(uint8_t release_threshold) { this->release_threshold_ = release_threshold; };
|
||||
void set_release_threshold(uint8_t release_threshold) { this->release_threshold = release_threshold; };
|
||||
uint8_t get_touch_threshold() { return this->touch_threshold_; };
|
||||
uint8_t get_release_threshold() { return this->release_threshold_; };
|
||||
uint8_t get_release_threshold() { return this->release_threshold; };
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
Loading…
x
Reference in New Issue
Block a user