1
0
mirror of https://github.com/esphome/esphome.git synced 2024-10-06 02:40:56 +01:00

BH1750 Measurement time (#997)

* BH1750 Measurement time

Fixes https://github.com/esphome/feature-requests/issues/148

* lint

* add test

Co-authored-by: Otto Winter <otto@otto-winter.com>
Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
This commit is contained in:
rradar 2020-05-29 03:10:34 +01:00 committed by GitHub
parent 7e4d12f880
commit d1f4c2ab57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 1 deletions

View File

@ -7,6 +7,8 @@ namespace bh1750 {
static const char *TAG = "bh1750.sensor";
static const uint8_t BH1750_COMMAND_POWER_ON = 0b00000001;
static const uint8_t BH1750_COMMAND_MT_REG_HI = 0b01000000; // last 3 bits
static const uint8_t BH1750_COMMAND_MT_REG_LO = 0b01100000; // last 5 bits
void BH1750Sensor::setup() {
ESP_LOGCONFIG(TAG, "Setting up BH1750 '%s'...", this->name_.c_str());
@ -14,7 +16,13 @@ void BH1750Sensor::setup() {
this->mark_failed();
return;
}
uint8_t mtreg_hi = (this->measurement_time_ >> 5) & 0b111;
uint8_t mtreg_lo = (this->measurement_time_ >> 0) & 0b11111;
this->write_bytes(BH1750_COMMAND_MT_REG_HI | mtreg_hi, nullptr, 0);
this->write_bytes(BH1750_COMMAND_MT_REG_LO | mtreg_lo, nullptr, 0);
}
void BH1750Sensor::dump_config() {
LOG_SENSOR("", "BH1750", this);
LOG_I2C_DEVICE(this);
@ -59,6 +67,7 @@ void BH1750Sensor::update() {
this->set_timeout("illuminance", wait, [this]() { this->read_data_(); });
}
float BH1750Sensor::get_setup_priority() const { return setup_priority::DATA; }
void BH1750Sensor::read_data_() {
uint16_t raw_value;
@ -68,10 +77,12 @@ void BH1750Sensor::read_data_() {
}
float lx = float(raw_value) / 1.2f;
lx *= 69.0f / this->measurement_time_;
ESP_LOGD(TAG, "'%s': Got illuminance=%.1flx", this->get_name().c_str(), lx);
this->publish_state(lx);
this->status_clear_warning();
}
void BH1750Sensor::set_resolution(BH1750Resolution resolution) { this->resolution_ = resolution; }
} // namespace bh1750

View File

@ -28,6 +28,7 @@ class BH1750Sensor : public sensor::Sensor, public PollingComponent, public i2c:
* @param resolution The new resolution of the sensor.
*/
void set_resolution(BH1750Resolution resolution);
void set_measurement_time(uint8_t measurement_time) { measurement_time_ = measurement_time; }
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
@ -40,6 +41,7 @@ class BH1750Sensor : public sensor::Sensor, public PollingComponent, public i2c:
void read_data_();
BH1750Resolution resolution_{BH1750_RESOLUTION_0P5_LX};
uint8_t measurement_time_;
};
} // namespace bh1750

View File

@ -15,9 +15,11 @@ BH1750_RESOLUTIONS = {
BH1750Sensor = bh1750_ns.class_('BH1750Sensor', sensor.Sensor, cg.PollingComponent, i2c.I2CDevice)
CONF_MEASUREMENT_TIME = 'measurement_time'
CONFIG_SCHEMA = sensor.sensor_schema(UNIT_LUX, ICON_BRIGHTNESS_5, 1).extend({
cv.GenerateID(): cv.declare_id(BH1750Sensor),
cv.Optional(CONF_RESOLUTION, default=0.5): cv.enum(BH1750_RESOLUTIONS, float=True),
cv.Optional(CONF_MEASUREMENT_TIME, default=69): cv.int_range(min=31, max=254),
}).extend(cv.polling_component_schema('60s')).extend(i2c.i2c_device_schema(0x23))
@ -28,3 +30,4 @@ def to_code(config):
yield i2c.register_i2c_device(var, config)
cg.add(var.set_resolution(config[CONF_RESOLUTION]))
cg.add(var.set_measurement_time(config[CONF_MEASUREMENT_TIME]))

View File

@ -308,6 +308,7 @@ sensor:
retain: False
availability:
state_topic: livingroom/custom_state_topic
measurement_time: 31
- platform: bme280
temperature:
name: "Outside Temperature"
@ -1169,7 +1170,6 @@ light:
if (initial_run) {
it[0] = current_color;
}
- automation:
name: Custom Effect
sequence: