Skip to content

Commit

Permalink
Revert "Refactor people counter to a number component"
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyr3x committed Jan 3, 2022
1 parent 73e4838 commit ae47139
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 157 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ roode:
# manual_threshold: 1300
# timing_budget: 100
invert_direction: true
number:
- platform: roode
people_counter:
name: People Count
restore_values: false
```

### Configuration variables
Expand All @@ -109,6 +105,7 @@ number:
- Options: `0=short`, `1=long`, `2=max`. Defaults to `true`.
- **timing_budget (optional, int)**: The timing budget for the sensor. Increasing this slows down detection but increases accuracy. Min: `10ms` Max: `1000s`. Defaults to `10ms`.
- **invert_direction (Optional, bool)**: Inverts the counting direction. Switch to `true` if the movement count appears backwards. Defaults to `false`.
- **restore_values (Optional, bool)**: Enables the restoration of the last count, after a reboot occurs. Defaults to `false`.
- **advised_sensor_orientation(Optional, bool)**: Advised orientation has the two sensor pads parallel to the entryway.
So `false` means the pads are perpendicular to the entryway.
Defaults to `true`.
Expand All @@ -128,6 +125,9 @@ binary_sensor:
sensor:
- platform: roode
id: hallway
people_counter_sensor:
id: peopleCounter
name: $friendly_name people counter
distance_sensor:
name: $friendly_name distance
filters:
Expand Down
24 changes: 0 additions & 24 deletions components/persisted_number/__init__.py

This file was deleted.

31 changes: 0 additions & 31 deletions components/persisted_number/persisted_number.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions components/persisted_number/persisted_number.h

This file was deleted.

12 changes: 10 additions & 2 deletions components/roode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from re import I
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
CONF_ID,
DEVICE_CLASS_EMPTY,
STATE_CLASS_MEASUREMENT,
UNIT_EMPTY,
UNIT_METER,
)


# DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "binary_sensor", "text_sensor", "number"]
AUTO_LOAD = ["sensor", "binary_sensor", "text_sensor"]
MULTI_CONF = True

CONF_ROODE_ID = "roode_id"
Expand All @@ -25,6 +30,7 @@
CONF_MIN_THRESHOLD_PERCENTAGE = "min_threshold_percentage"
CONF_MANUAL_THRESHOLD = "manual_threshold"
CONF_THRESHOLD_PERCENTAGE = "threshold_percentage"
CONF_RESTORE_VALUES = "restore_values"
CONF_I2C_ADDRESS = "i2c_address"
CONF_SENSOR_MODE = "sensor_mode"
CONF_MANUAL = "manual"
Expand All @@ -38,15 +44,16 @@
CONF_SENSOR_OFFSET_CALIBRATION = "sensor_offset_calibration"
CONF_SENSOR_XTALK_CALIBRATION = "sensor_xtalk_calibration"
TYPES = [
CONF_RESTORE_VALUES,
CONF_INVERT_DIRECTION,
CONF_ADVISED_SENSOR_ORIENTATION,
CONF_I2C_ADDRESS,
]

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(Roode),
cv.Optional(CONF_INVERT_DIRECTION, default="false"): cv.boolean,
cv.Optional(CONF_RESTORE_VALUES, default="false"): cv.boolean,
cv.Optional(CONF_ADVISED_SENSOR_ORIENTATION, default="true"): cv.boolean,
cv.Optional(CONF_I2C_ADDRESS, default=0x29): cv.uint8_t,
cv.Optional(CONF_SAMPLING, default=2): cv.Any(
Expand Down Expand Up @@ -158,6 +165,7 @@ def setup_sampling(config, hub):
async def to_code(config):
hub = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(hub, config)
cg.add_library("EEPROM", None)
cg.add_library("Wire", None)
cg.add_library("rneurink", "1.2.3", "VL53L1X_ULD")

Expand Down
43 changes: 0 additions & 43 deletions components/roode/number.py

This file was deleted.

42 changes: 32 additions & 10 deletions components/roode/roode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace esphome
{
version_sensor->publish_state(VERSION);
}
EEPROM.begin(EEPROM_SIZE);
Wire.begin();
Wire.setClock(400000);

Expand Down Expand Up @@ -88,6 +89,15 @@ namespace esphome
DIST_THRESHOLD_MAX[1] = Roode::manual_threshold_;
publishSensorConfiguration(DIST_THRESHOLD_MAX, true);
}
if (restore_values_)
{
ESP_LOGI("Roode setup", "Restoring last count value...");
peopleCounter = EEPROM.read(100);
if (peopleCounter == 255) // 255 is the default value if no value was stored
peopleCounter = 0;
ESP_LOGI("Roode setup", "last value: %u", peopleCounter);
}
sendCounter(peopleCounter);
distanceSensor.SetInterMeasurementInMs(delay_between_measurements);
distanceSensor.StartRanging();
}
Expand Down Expand Up @@ -287,10 +297,14 @@ namespace esphome
if ((PathTrack[1] == 1) && (PathTrack[2] == 3) && (PathTrack[3] == 2))
{
// This an exit
if (peopleCounter > 0)
{
peopleCounter--;
sendCounter(peopleCounter);
DistancesTableSize[0] = 0;
DistancesTableSize[1] = 0;
}
ESP_LOGD("Roode pathTracking", "Exit detected.");
DistancesTableSize[0] = 0;
DistancesTableSize[1] = 0;
this->updateCounter(-1);
if (entry_exit_event_sensor != nullptr)
{
entry_exit_event_sensor->publish_state("Exit");
Expand All @@ -299,8 +313,9 @@ namespace esphome
else if ((PathTrack[1] == 2) && (PathTrack[2] == 3) && (PathTrack[3] == 1))
{
// This an entry
peopleCounter++;
sendCounter(peopleCounter);
ESP_LOGD("Roode pathTracking", "Entry detected.");
this->updateCounter(1);
if (entry_exit_event_sensor != nullptr)
{
entry_exit_event_sensor->publish_state("Entry");
Expand Down Expand Up @@ -340,14 +355,21 @@ namespace esphome
}
}
}
void Roode::updateCounter(int delta) {
if (this->people_counter == nullptr)

void Roode::sendCounter(uint16_t counter)
{
ESP_LOGI(SETUP, "Sending people count: %d", counter);
peopleCounter = counter;
if (people_counter_sensor != nullptr)
{
return;
people_counter_sensor->publish_state(peopleCounter);
}

if (restore_values_)
{
EEPROM.write(100, peopleCounter);
EEPROM.commit();
}
auto next = this->people_counter->state + (float) delta;
ESP_LOGI(TAG, "Updating people count: %d", (int) next);
this->people_counter->set(next);
}
void Roode::recalibration()
{
Expand Down
9 changes: 6 additions & 3 deletions components/roode/roode.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/application.h"
#include "EEPROM.h"
// #include <VL53L1X.h>
#include "VL53L1X_ULD.h"
#include <math.h>

Expand All @@ -15,6 +17,7 @@ namespace esphome
#define NOBODY 0
#define SOMEONE 1
#define VERSION "v1.4.1-beta"
#define EEPROM_SIZE 512
#define VL53L1X_ULD_I2C_ADDRESS 0x52 // Default address is 0x52
static int LEFT = 0;
static int RIGHT = 1;
Expand Down Expand Up @@ -76,7 +79,7 @@ namespace esphome
void set_advised_sensor_orientation(bool val) { advised_sensor_orientation_ = val; }
void set_sampling_size(uint8_t size) { DISTANCES_ARRAY_SIZE = size; }
void set_distance_sensor(sensor::Sensor *distance_sensor_) { distance_sensor = distance_sensor_; }
void set_people_counter(number::Number *counter) { this->people_counter = counter; }
void set_people_counter_sensor(sensor::Sensor *people_counter_sensor_) { people_counter_sensor = people_counter_sensor_; }
void set_max_threshold_zone0_sensor(sensor::Sensor *max_threshold_zone0_sensor_) { max_threshold_zone0_sensor = max_threshold_zone0_sensor_; }
void set_max_threshold_zone1_sensor(sensor::Sensor *max_threshold_zone1_sensor_) { max_threshold_zone1_sensor = max_threshold_zone1_sensor_; }
void set_min_threshold_zone0_sensor(sensor::Sensor *min_threshold_zone0_sensor_) { min_threshold_zone0_sensor = min_threshold_zone0_sensor_; }
Expand All @@ -89,6 +92,7 @@ namespace esphome
void set_entry_exit_event_text_sensor(text_sensor::TextSensor *entry_exit_event_sensor_) { entry_exit_event_sensor = entry_exit_event_sensor_; }
void set_sensor_mode(int sensor_mode_) { sensor_mode = sensor_mode_; }
void getZoneDistance();
void sendCounter(uint16_t counter);
void recalibration();
bool handleSensorStatus();
uint16_t getDistance();
Expand All @@ -106,7 +110,7 @@ namespace esphome
protected:
VL53L1X_ULD distanceSensor;
sensor::Sensor *distance_sensor;
number::Number *people_counter;
sensor::Sensor *people_counter_sensor;
sensor::Sensor *max_threshold_zone0_sensor;
sensor::Sensor *max_threshold_zone1_sensor;
sensor::Sensor *min_threshold_zone0_sensor;
Expand All @@ -125,7 +129,6 @@ namespace esphome
void publishSensorConfiguration(int DIST_THRESHOLD_ARR[2], bool isMax);
int getOptimizedValues(int *values, int sum, int size);
int getSum(int *values, int size);
void updateCounter(int delta);
bool calibration_active_{false};
bool manual_active_{false};
bool roi_active_{false};
Expand Down
11 changes: 11 additions & 0 deletions components/roode/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from esphome.components import sensor
from esphome.const import (
ICON_ARROW_EXPAND_VERTICAL,
ICON_COUNTER,
ICON_NEW_BOX,
ICON_RULER,
STATE_CLASS_MEASUREMENT,
Expand All @@ -14,6 +15,7 @@
DEPENDENCIES = ["roode"]

CONF_DISTANCE = "distance_sensor"
CONF_PEOPLE_COUNTER = "people_counter_sensor"
CONF_MAX_THRESHOLD_ZONE0 = "max_threshold_zone0"
CONF_MAX_THRESHOLD_ZONE1 = "max_threshold_zone1"
CONF_MIN_THRESHOLD_ZONE0 = "min_threshold_zone0"
Expand All @@ -31,6 +33,12 @@
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_PEOPLE_COUNTER): sensor.sensor_schema(
icon=ICON_COUNTER,
unit_of_measurement=UNIT_EMPTY,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_MAX_THRESHOLD_ZONE0): sensor.sensor_schema(
icon="mdi:map-marker-distance",
unit_of_measurement="mm",
Expand Down Expand Up @@ -88,6 +96,9 @@ async def to_code(config):
if CONF_DISTANCE in config:
distance = await sensor.new_sensor(config[CONF_DISTANCE])
cg.add(var.set_distance_sensor(distance))
if CONF_PEOPLE_COUNTER in config:
count = await sensor.new_sensor(config[CONF_PEOPLE_COUNTER])
cg.add(var.set_people_counter_sensor(count))
if CONF_MAX_THRESHOLD_ZONE0 in config:
count = await sensor.new_sensor(config[CONF_MAX_THRESHOLD_ZONE0])
cg.add(var.set_max_threshold_zone0_sensor(count))
Expand Down
Loading

0 comments on commit ae47139

Please sign in to comment.