diff --git a/esphome/components/binary_sensor_map/__init__.py b/esphome/components/binary_sensor_map/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/binary_sensor_map/binary_sensor_map.cpp b/esphome/components/binary_sensor_map/binary_sensor_map.cpp new file mode 100644 index 0000000000..9d5b95a68c --- /dev/null +++ b/esphome/components/binary_sensor_map/binary_sensor_map.cpp @@ -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 diff --git a/esphome/components/binary_sensor_map/binary_sensor_map.h b/esphome/components/binary_sensor_map/binary_sensor_map.h new file mode 100644 index 0000000000..b6c1dc17e5 --- /dev/null +++ b/esphome/components/binary_sensor_map/binary_sensor_map.h @@ -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 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 diff --git a/esphome/components/binary_sensor_map/sensor.py b/esphome/components/binary_sensor_map/sensor.py new file mode 100644 index 0000000000..b48e2c4c2f --- /dev/null +++ b/esphome/components/binary_sensor_map/sensor.py @@ -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]))