ESPHome  2022.6.3
binary_sensor_map.cpp
Go to the documentation of this file.
1 #include "binary_sensor_map.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace binary_sensor_map {
6 
7 static const char *const TAG = "binary_sensor_map";
8 
9 void BinarySensorMap::dump_config() { LOG_SENSOR(" ", "binary_sensor_map", this); }
10 
12  switch (this->sensor_type_) {
14  this->process_group_();
15  break;
16  }
17 }
18 
20  float total_current_value = 0.0;
21  uint8_t num_active_sensors = 0;
22  uint64_t mask = 0x00;
23  // check all binary_sensors for its state. when active add its value to total_current_value.
24  // create a bitmask for the binary_sensor status on all channels
25  for (size_t i = 0; i < this->channels_.size(); i++) {
26  auto bs = this->channels_[i];
27  if (bs.binary_sensor->state) {
28  num_active_sensors++;
29  total_current_value += bs.sensor_value;
30  mask |= 1 << i;
31  }
32  }
33  // check if the sensor map was touched
34  if (mask != 0ULL) {
35  // did the bit_mask change or is it a new sensor touch
36  if (this->last_mask_ != mask) {
37  float publish_value = total_current_value / num_active_sensors;
38  ESP_LOGD(TAG, "'%s' - Publishing %.2f", this->name_.c_str(), publish_value);
39  this->publish_state(publish_value);
40  }
41  } else if (this->last_mask_ != 0ULL) {
42  // is this a new sensor release
43  ESP_LOGD(TAG, "'%s' - No binary sensor active, publishing NAN", this->name_.c_str());
44  this->publish_state(NAN);
45  }
46  this->last_mask_ = mask;
47 }
48 
50  BinarySensorMapChannel sensor_channel{
51  .binary_sensor = sensor,
52  .sensor_value = value,
53  };
54  this->channels_.push_back(sensor_channel);
55 }
56 
57 void BinarySensorMap::set_sensor_type(BinarySensorMapType sensor_type) { this->sensor_type_ = sensor_type; }
58 
59 } // namespace binary_sensor_map
60 } // namespace esphome
void add_channel(binary_sensor::BinarySensor *sensor, float value)
Add binary_sensors to the group.
void set_sensor_type(BinarySensorMapType sensor_type)
std::string name_
Definition: entity_base.h:54
void loop() override
The loop checks all binary_sensor states When the binary_sensor reports a true value for its state...
std::vector< BinarySensorMapChannel > channels_
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:70
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
void process_group_()
methods to process the types of binary_sensor_maps GROUP: process_group_() just map to a value ...