Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Allow sample rate overrides for T/P/H sensors (#41)
Browse files Browse the repository at this point in the history
This allows for the common request of being able to run the
gas-dependant sensors/calculations in ULP mode with more frequent
LP temperature/pressure/humidity readings.

In that setup the heater only runs every 5 minutes reducing
consumption but still allowing highly reactive T/P/H readings.

Other combinations are also possible but they'll not be so useful.

If no per-sensor overrides are given then they will run at the sample
rate configured at platform level.

Also included in this commit are some tidy-ups to:
- improve code readability and formatting
- clear up a compile time warning relating to gate-keeper define
- add a couple of extra sensor status safety checks
- specify sensor device classes
  • Loading branch information
trvrnrth authored May 4, 2021
1 parent df37a76 commit fafc70d
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 186 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ bme680_bsec:

# Sample rate
# -----------
# This controls the sampling rate for gas-dependant sensors and will govern the interval at which the sensor heater is operated.
# By default this rate will also be used for temperature, pressure and humidity sensors but these can be overridden on a per-sensor level if required.
# Available options:
# - lp (low power - samples every 3 seconds)
# - ulp (ultra low power - samples every 5 minutes)
# Default: lp
sample_rate: lp
sample_rate: ulp

# Interval at which to save BSEC state
# ------------------------------------
Expand All @@ -73,16 +75,19 @@ sensor:
temperature:
# Temperature in °C
name: "BME680 Temperature"
sample_rate: lp
filters:
- median
pressure:
# Pressure in hPa
name: "BME680 Pressure"
sample_rate: lp
filters:
- median
humidity:
# Relative humidity %
name: "BME680 Humidity"
sample_rate: lp
filters:
- median
gas_resistance:
Expand Down
66 changes: 38 additions & 28 deletions bme680_bsec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,49 @@
from esphome.components import i2c
from esphome.const import CONF_ID

CODEOWNERS = ['@trvrnrth']
DEPENDENCIES = ['i2c']
AUTO_LOAD = ['sensor', 'text_sensor']
CODEOWNERS = ["@trvrnrth"]
DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "text_sensor"]

CONF_BME680_BSEC_ID = 'bme680_bsec_id'
CONF_TEMPERATURE_OFFSET = 'temperature_offset'
CONF_IAQ_MODE = 'iaq_mode'
CONF_SAMPLE_RATE = 'sample_rate'
CONF_STATE_SAVE_INTERVAL = 'state_save_interval'
CONF_BME680_BSEC_ID = "bme680_bsec_id"
CONF_TEMPERATURE_OFFSET = "temperature_offset"
CONF_IAQ_MODE = "iaq_mode"
CONF_SAMPLE_RATE = "sample_rate"
CONF_STATE_SAVE_INTERVAL = "state_save_interval"

bme680_bsec_ns = cg.esphome_ns.namespace('bme680_bsec')
bme680_bsec_ns = cg.esphome_ns.namespace("bme680_bsec")

IAQMode = bme680_bsec_ns.enum('IAQMode')
IAQMode = bme680_bsec_ns.enum("IAQMode")
IAQ_MODE_OPTIONS = {
'STATIC': IAQMode.IAQ_MODE_STATIC,
'MOBILE': IAQMode.IAQ_MODE_MOBILE,
"STATIC": IAQMode.IAQ_MODE_STATIC,
"MOBILE": IAQMode.IAQ_MODE_MOBILE,
}

SampleRate = bme680_bsec_ns.enum('SampleRate')
SampleRate = bme680_bsec_ns.enum("SampleRate")
SAMPLE_RATE_OPTIONS = {
'LP': SampleRate.SAMPLE_RATE_LP,
'ULP': SampleRate.SAMPLE_RATE_ULP,
"LP": SampleRate.SAMPLE_RATE_LP,
"ULP": SampleRate.SAMPLE_RATE_ULP,
}

BME680BSECComponent = bme680_bsec_ns.class_('BME680BSECComponent', cg.Component, i2c.I2CDevice)
BME680BSECComponent = bme680_bsec_ns.class_(
"BME680BSECComponent", cg.Component, i2c.I2CDevice
)

CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(BME680BSECComponent),
cv.Optional(CONF_TEMPERATURE_OFFSET, default=0): cv.temperature,
cv.Optional(CONF_IAQ_MODE, default='STATIC'):
cv.enum(IAQ_MODE_OPTIONS, upper=True),
cv.Optional(CONF_SAMPLE_RATE, default='LP'):
cv.enum(SAMPLE_RATE_OPTIONS, upper=True),
cv.Optional(CONF_STATE_SAVE_INTERVAL, default='6hours'): cv.positive_time_period_minutes,
}).extend(i2c.i2c_device_schema(0x76))
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(BME680BSECComponent),
cv.Optional(CONF_TEMPERATURE_OFFSET, default=0): cv.temperature,
cv.Optional(CONF_IAQ_MODE, default="STATIC"): cv.enum(
IAQ_MODE_OPTIONS, upper=True
),
cv.Optional(CONF_SAMPLE_RATE, default="LP"): cv.enum(
SAMPLE_RATE_OPTIONS, upper=True
),
cv.Optional(
CONF_STATE_SAVE_INTERVAL, default="6hours"
): cv.positive_time_period_minutes,
}
).extend(i2c.i2c_device_schema(0x76))


def to_code(config):
Expand All @@ -48,7 +56,9 @@ def to_code(config):
cg.add(var.set_temperature_offset(config[CONF_TEMPERATURE_OFFSET]))
cg.add(var.set_iaq_mode(config[CONF_IAQ_MODE]))
cg.add(var.set_sample_rate(config[CONF_SAMPLE_RATE]))
cg.add(var.set_state_save_interval(config[CONF_STATE_SAVE_INTERVAL].total_milliseconds))
cg.add(
var.set_state_save_interval(config[CONF_STATE_SAVE_INTERVAL].total_milliseconds)
)

cg.add_build_flag('-DUSING_BSEC')
cg.add_library('BSEC Software Library', '1.6.1480')
cg.add_define("USE_BSEC")
cg.add_library("BSEC Software Library", "1.6.1480")
Loading

0 comments on commit fafc70d

Please sign in to comment.