Skip to content

Commit

Permalink
BH1750 Measurement time (#997)
Browse files Browse the repository at this point in the history
* BH1750 Measurement time

Fixes esphome/feature-requests#148

* lint

* add test

Co-authored-by: Otto Winter <otto@otto-winter.com>
Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
  • Loading branch information
3 people committed May 29, 2020
1 parent 7e4d12f commit d1f4c2a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions esphome/components/bh1750/bh1750.cpp
Expand Up @@ -7,14 +7,22 @@ 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());
if (!this->write_bytes(BH1750_COMMAND_POWER_ON, nullptr, 0)) {
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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/bh1750/bh1750.h
Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions esphome/components/bh1750/sensor.py
Expand Up @@ -15,9 +15,11 @@

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))


Expand All @@ -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]))
2 changes: 1 addition & 1 deletion tests/test1.yaml
Expand Up @@ -308,6 +308,7 @@ sensor:
retain: False
availability:
state_topic: livingroom/custom_state_topic
measurement_time: 31
- platform: bme280
temperature:
name: "Outside Temperature"
Expand Down Expand Up @@ -1169,7 +1170,6 @@ light:
if (initial_run) {
it[0] = current_color;
}
- automation:
name: Custom Effect
sequence:
Expand Down

0 comments on commit d1f4c2a

Please sign in to comment.