Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sensor creation, initialization and measurement reads #68

Merged
merged 31 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fef4b3c
Add use_presence_sensor option
Lyr3x Dec 15, 2021
fc944d6
Ensure CONF_USE_PRESENCE generation
Lyr3x Dec 15, 2021
66e2259
Move option to correct scope
Lyr3x Dec 15, 2021
8899764
Add use_distance_sensor(false) option
Lyr3x Dec 15, 2021
a0dc113
Make distance sensor diagnostic
Lyr3x Dec 15, 2021
b112287
Set ref tag to master
Lyr3x Dec 15, 2021
4207a17
Reduce sample size to 2
Lyr3x Dec 15, 2021
bfdd4ec
Switch formatter to black
Lyr3x Dec 15, 2021
f048426
Format sensor.py and text_sensor.py
Lyr3x Dec 15, 2021
4b5cfc6
Format with black
Lyr3x Dec 16, 2021
5c5fb56
Fix validation for binary sensor
Lyr3x Dec 16, 2021
e318dcd
Fix false init of sensors and let ESPHome manage sensor creation
Lyr3x Dec 16, 2021
c6b534c
Bum sample size to 10 again
Lyr3x Dec 16, 2021
58ed9e7
Reduce to 5
Lyr3x Dec 16, 2021
0372781
Set array size to 2 again
Lyr3x Dec 16, 2021
a7664d3
Disable sampling
Lyr3x Dec 16, 2021
8f26340
Add feed_wdt calls
Lyr3x Dec 17, 2021
996be2b
Add additional wdt
Lyr3x Dec 17, 2021
4139b3e
Fix duplicated timing setting and add missing delay
Lyr3x Dec 17, 2021
69c8bd5
Add use_sampling config
Lyr3x Dec 18, 2021
ea897c2
Fix sensor readings
Lyr3x Dec 18, 2021
738ab75
Set ROI correct
Lyr3x Dec 18, 2021
f842faa
Remove double delay
Lyr3x Dec 18, 2021
7a0bdd7
Set correct delay
Lyr3x Dec 18, 2021
8132a05
Fix sensor mode setting
Lyr3x Dec 18, 2021
d95173b
Try to fix measurement
Lyr3x Dec 18, 2021
6a8ef80
Comment in sampling again
Lyr3x Dec 18, 2021
59088cc
Cleanup
Lyr3x Dec 18, 2021
70d184d
Set start stop commands
Lyr3x Dec 18, 2021
49d1164
Fix the stopContinuous bug
Lyr3x Dec 18, 2021
860647b
Set new VERSION
Lyr3x Dec 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"ostream": "cpp",
"istream": "cpp"
},
"python.formatting.provider": "yapf"
"python.formatting.provider": "black"
}
129 changes: 68 additions & 61 deletions components/roode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from esphome.components import sensor
from esphome.const import CONF_ID, STATE_CLASS_MEASUREMENT, UNIT_EMPTY, UNIT_METER


# DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "binary_sensor", "text_sensor"]
MULTI_CONF = True
Expand All @@ -13,9 +14,9 @@
roode_ns = cg.esphome_ns.namespace("roode")
Roode = roode_ns.class_("Roode", cg.PollingComponent)

CONF_ROI_HEIGHT = 'roi_height'
CONF_ROI_WIDTH = 'roi_width'
CONF_ADVISED_SENSOR_ORIENTATION = 'advised_sensor_orientation'
CONF_ROI_HEIGHT = "roi_height"
CONF_ROI_WIDTH = "roi_width"
CONF_ADVISED_SENSOR_ORIENTATION = "advised_sensor_orientation"
CONF_CALIBRATION = "calibration"
CONF_ROI_CALIBRATION = "roi_calibration"
CONF_INVERT_DIRECTION = "invert_direction"
Expand All @@ -30,66 +31,72 @@
CONF_MANUAL_ACTIVE = "manual_active"
CONF_CALIBRATION_ACTIVE = "calibration_active"
CONF_TIMING_BUDGET = "timing_budget"
CONF_USE_SAMPLING = "use_sampling"
TYPES = [
CONF_RESTORE_VALUES, CONF_INVERT_DIRECTION,
CONF_ADVISED_SENSOR_ORIENTATION, CONF_I2C_ADDRESS
CONF_RESTORE_VALUES,
CONF_INVERT_DIRECTION,
CONF_ADVISED_SENSOR_ORIENTATION,
CONF_I2C_ADDRESS,
CONF_USE_SAMPLING,
]
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.Exclusive(
CONF_CALIBRATION, "mode", f"Only one mode, {CONF_MANUAL} or {CONF_CALIBRATION} is usable"):
cv.Schema({
cv.Optional(CONF_CALIBRATION_ACTIVE, default='true'):
cv.boolean,
cv.Optional(CONF_MAX_THRESHOLD_PERCENTAGE, default=85):
cv.int_range(min=50, max=100),
cv.Optional(CONF_MIN_THRESHOLD_PERCENTAGE, default=0):
cv.int_range(min=0, max=100),
cv.Optional(CONF_ROI_CALIBRATION, default='false'):
cv.boolean,
}),
cv.Exclusive(
CONF_MANUAL, "mode", f"Only one mode, {CONF_MANUAL} or {CONF_CALIBRATION} is usable"):
cv.Schema({
cv.Optional(CONF_MANUAL_ACTIVE, default='true'):
cv.boolean,
cv.Optional(CONF_TIMING_BUDGET, default=10):
cv.int_range(min=10, max=1000),
cv.Inclusive(
CONF_SENSOR_MODE,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
):
cv.int_range(min=-1, max=2),
cv.Inclusive(
CONF_ROI_HEIGHT,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
):
cv.int_range(min=4, max=16),
cv.Inclusive(
CONF_ROI_WIDTH,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
):
cv.int_range(min=4, max=16),
cv.Inclusive(
CONF_MANUAL_THRESHOLD,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
):
cv.int_range(min=40, max=4000),
}),
}).extend(cv.polling_component_schema("100ms")))
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_USE_SAMPLING, default="false"): cv.boolean,
cv.Optional(CONF_I2C_ADDRESS, default=0x29): cv.uint8_t,
cv.Exclusive(
CONF_CALIBRATION,
"mode",
f"Only one mode, {CONF_MANUAL} or {CONF_CALIBRATION} is usable",
): cv.Schema(
{
cv.Optional(CONF_CALIBRATION_ACTIVE, default="true"): cv.boolean,
cv.Optional(CONF_MAX_THRESHOLD_PERCENTAGE, default=85): cv.int_range(
min=50, max=100
),
cv.Optional(CONF_MIN_THRESHOLD_PERCENTAGE, default=0): cv.int_range(
min=0, max=100
),
cv.Optional(CONF_ROI_CALIBRATION, default="false"): cv.boolean,
}
),
cv.Exclusive(
CONF_MANUAL,
"mode",
f"Only one mode, {CONF_MANUAL} or {CONF_CALIBRATION} is usable",
): cv.Schema(
{
cv.Optional(CONF_MANUAL_ACTIVE, default="true"): cv.boolean,
cv.Optional(CONF_TIMING_BUDGET, default=10): cv.int_range(
min=10, max=1000
),
cv.Inclusive(
CONF_SENSOR_MODE,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
): cv.int_range(min=-1, max=2),
cv.Inclusive(
CONF_ROI_HEIGHT,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
): cv.int_range(min=4, max=16),
cv.Inclusive(
CONF_ROI_WIDTH,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
): cv.int_range(min=4, max=16),
cv.Inclusive(
CONF_MANUAL_THRESHOLD,
"manual_mode",
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
): cv.int_range(min=40, max=4000),
}
),
}
).extend(cv.polling_component_schema("100ms"))


async def setup_conf(config, key, hub):
Expand Down
39 changes: 28 additions & 11 deletions components/roode/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
import esphome.final_validate as fv
from esphome.const import (
CONF_ID,
CONF_DEVICE_CLASS,
Expand All @@ -10,18 +11,34 @@

DEPENDENCIES = ["roode"]

CONF_PRESENCE = 'presence_sensor'
CONF_PRESENCE = "presence_sensor"
TYPES = [CONF_PRESENCE]

CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(CONF_ROODE_ID):
cv.use_id(Roode),
cv.Optional(CONF_PRESENCE):
binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID():
cv.declare_id(binary_sensor.BinarySensor),
}),
})
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_ROODE_ID): cv.use_id(Roode),
cv.Optional(CONF_PRESENCE): binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(binary_sensor.BinarySensor),
}
),
}
)


# def validate_can_use_presence(value):
# main = fv.full_config.get()["roode"][0]
# presence_sensor = main.get(CONF_USE_PRESENCE)
# print(presence_sensor)
# if presence_sensor == False:
# raise cv.Invalid("Presence sensor is not enabled")
# else:
# return presence_sensor


# FINAL_VALIDATE_SCHEMA = cv.Schema(
# {cv.Optional(CONF_PRESENCE): validate_can_use_presence}, extra=cv.ALLOW_EXTRA
# )


async def setup_conf(config, key, hub):
Expand All @@ -35,4 +52,4 @@ async def setup_conf(config, key, hub):
async def to_code(config):
hub = await cg.get_variable(config[CONF_ROODE_ID])
for key in TYPES:
await setup_conf(config, key, hub)
await setup_conf(config, key, hub)
Loading