Skip to content

Commit

Permalink
Merge pull request #68 from Lyr3x/make-distance-sensor-optional
Browse files Browse the repository at this point in the history
Improve sensor creation, initialization and measurement reads
  • Loading branch information
Lyr3x authored Dec 18, 2021
2 parents 63a76c8 + 860647b commit 9add55f
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 326 deletions.
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

0 comments on commit 9add55f

Please sign in to comment.