Skip to content

Commit

Permalink
Merge pull request #86 from CarsonF/number
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyr3x committed Jan 3, 2022
2 parents 2eda0a9 + c21aab5 commit 4a64a17
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 100 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ roode:
# manual_threshold: 1300
# timing_budget: 100
invert_direction: true
restore_values: false
number:
- platform: roode
people_counter:
name: People Count
```

### Configuration variables
Expand All @@ -105,7 +109,6 @@ roode:
- 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 @@ -125,9 +128,6 @@ 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
35 changes: 35 additions & 0 deletions components/persisted_number/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional, OrderedDict

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import number
from esphome.const import (
CONF_ID,
CONF_RESTORE_VALUE,
)

PersistedNumber = number.number_ns.class_(
"PersistedNumber", number.Number, cg.Component
)

PERSISTED_NUMBER_SCHEMA = number.NUMBER_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(PersistedNumber),
cv.Optional(CONF_RESTORE_VALUE, default=True): cv.boolean,
}
)


async def new_persisted_number(
config: OrderedDict,
min_value: float,
max_value: float,
step: Optional[float] = None,
):
var = await number.new_number(
config, min_value=min_value, max_value=max_value, step=step
)
await cg.register_component(var, config)
if CONF_RESTORE_VALUE in config:
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
return var
31 changes: 31 additions & 0 deletions components/persisted_number/persisted_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "persisted_number.h"
#include "esphome/core/log.h"

namespace esphome {
namespace number {

auto PersistedNumber::control(float newValue) -> void {
this->publish_state(newValue);
if (this->restore_value_) {
this->pref_.save(&newValue);
}
}

auto PersistedNumber::setup() -> void {
float value;
if (!this->restore_value_) {
value = this->traits.get_min_value();
} else {
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
if (this->pref_.load(&value)) {
ESP_LOGI("number", "'%s': Restored state %f", this->get_name().c_str(), value);
} else {
ESP_LOGI("number", "'%s': No previous state found", this->get_name().c_str());
value = this->traits.get_min_value();
}
}
this->publish_state(value);
}

}
}
24 changes: 24 additions & 0 deletions components/persisted_number/persisted_number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "esphome/components/number/number.h"
#include "esphome/core/component.h"
#include "esphome/core/preferences.h"

namespace esphome {
namespace number {

class PersistedNumber : public number::Number, public Component {
public:
float get_setup_priority() const override { return setup_priority::HARDWARE; }
void set_restore_value(bool restore) { this->restore_value_ = restore; }
void setup() override;

protected:
void control(float value) override;

bool restore_value_{false};
ESPPreferenceObject pref_;
};

} // namespace roode
} // namespace esphome
12 changes: 2 additions & 10 deletions components/roode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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"]
AUTO_LOAD = ["sensor", "binary_sensor", "text_sensor", "number"]
MULTI_CONF = True

CONF_ROODE_ID = "roode_id"
Expand All @@ -30,7 +25,6 @@
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 @@ -44,16 +38,15 @@
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 @@ -165,7 +158,6 @@ 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
39 changes: 39 additions & 0 deletions components/roode/number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import OrderedDict

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ICON, CONF_MAX_VALUE
from esphome.cpp_generator import MockObj

from ..persisted_number import PERSISTED_NUMBER_SCHEMA, new_persisted_number
from . import Roode, CONF_ROODE_ID

DEPENDENCIES = ["roode"]
AUTO_LOAD = ["number", "persisted_number"]

CONF_PEOPLE_COUNTER = "people_counter"

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_ROODE_ID): cv.use_id(Roode),
cv.Optional(CONF_PEOPLE_COUNTER): PERSISTED_NUMBER_SCHEMA.extend(
{
cv.Optional(CONF_ICON, default="mdi:counter"): cv.icon, # new default
cv.Optional(CONF_MAX_VALUE, 10): cv.int_range(1, 255),
}
),
}
)


async def setup_people_counter(config: OrderedDict, hub: MockObj):
counter = await new_persisted_number(
config, min_value=0, step=1, max_value=config[CONF_MAX_VALUE]
)
cg.add(hub.set_people_counter(counter))


async def to_code(config: OrderedDict):
hub = await cg.get_variable(config[CONF_ROODE_ID])
if CONF_PEOPLE_COUNTER in config:
await setup_people_counter(config[CONF_PEOPLE_COUNTER], hub)
42 changes: 10 additions & 32 deletions components/roode/roode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace esphome
{
version_sensor->publish_state(VERSION);
}
EEPROM.begin(EEPROM_SIZE);
Wire.begin();
Wire.setClock(400000);

Expand Down Expand Up @@ -89,15 +88,6 @@ 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 @@ -297,14 +287,10 @@ 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 @@ -313,9 +299,8 @@ 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 @@ -355,21 +340,14 @@ namespace esphome
}
}
}

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

if (restore_values_)
void Roode::updateCounter(int delta) {
if (this->people_counter == nullptr)
{
EEPROM.write(100, peopleCounter);
EEPROM.commit();
return;
}
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: 3 additions & 6 deletions components/roode/roode.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#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 @@ -17,7 +15,6 @@ 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 @@ -79,7 +76,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_sensor(sensor::Sensor *people_counter_sensor_) { people_counter_sensor = people_counter_sensor_; }
void set_people_counter(number::Number *counter) { this->people_counter = counter; }
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 @@ -92,7 +89,6 @@ 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 @@ -110,7 +106,7 @@ namespace esphome
protected:
VL53L1X_ULD distanceSensor;
sensor::Sensor *distance_sensor;
sensor::Sensor *people_counter_sensor;
number::Number *people_counter;
sensor::Sensor *max_threshold_zone0_sensor;
sensor::Sensor *max_threshold_zone1_sensor;
sensor::Sensor *min_threshold_zone0_sensor;
Expand All @@ -129,6 +125,7 @@ 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: 0 additions & 11 deletions components/roode/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -15,7 +14,6 @@
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 @@ -33,12 +31,6 @@
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 @@ -96,9 +88,6 @@ 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

0 comments on commit 4a64a17

Please sign in to comment.