Use core constants for sample duration on bh1750 (#1764)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Yuval Aboulafia 2021-05-12 01:16:59 +03:00 committed by GitHub
parent a546677b08
commit cdc9c99d40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 8 deletions

View file

@ -17,8 +17,8 @@ void BH1750Sensor::setup() {
return; return;
} }
uint8_t mtreg_hi = (this->measurement_time_ >> 5) & 0b111; uint8_t mtreg_hi = (this->measurement_duration_ >> 5) & 0b111;
uint8_t mtreg_lo = (this->measurement_time_ >> 0) & 0b11111; uint8_t mtreg_lo = (this->measurement_duration_ >> 0) & 0b11111;
this->write_bytes(BH1750_COMMAND_MT_REG_HI | mtreg_hi, nullptr, 0); this->write_bytes(BH1750_COMMAND_MT_REG_HI | mtreg_hi, nullptr, 0);
this->write_bytes(BH1750_COMMAND_MT_REG_LO | mtreg_lo, nullptr, 0); this->write_bytes(BH1750_COMMAND_MT_REG_LO | mtreg_lo, nullptr, 0);
} }
@ -77,7 +77,7 @@ void BH1750Sensor::read_data_() {
} }
float lx = float(raw_value) / 1.2f; float lx = float(raw_value) / 1.2f;
lx *= 69.0f / this->measurement_time_; lx *= 69.0f / this->measurement_duration_;
ESP_LOGD(TAG, "'%s': Got illuminance=%.1flx", this->get_name().c_str(), lx); ESP_LOGD(TAG, "'%s': Got illuminance=%.1flx", this->get_name().c_str(), lx);
this->publish_state(lx); this->publish_state(lx);
this->status_clear_warning(); this->status_clear_warning();

View file

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

View file

@ -7,6 +7,7 @@ from esphome.const import (
DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_ILLUMINANCE,
ICON_EMPTY, ICON_EMPTY,
UNIT_LUX, UNIT_LUX,
CONF_MEASUREMENT_DURATION,
) )
DEPENDENCIES = ["i2c"] DEPENDENCIES = ["i2c"]
@ -32,9 +33,12 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_RESOLUTION, default=0.5): cv.enum( cv.Optional(CONF_RESOLUTION, default=0.5): cv.enum(
BH1750_RESOLUTIONS, float=True BH1750_RESOLUTIONS, float=True
), ),
cv.Optional(CONF_MEASUREMENT_TIME, default=69): cv.int_range( cv.Optional(CONF_MEASUREMENT_DURATION, default=69): cv.int_range(
min=31, max=254 min=31, max=254
), ),
cv.Optional(CONF_MEASUREMENT_TIME): cv.invalid(
"The 'measurement_time' option has been replaced with 'measurement_duration' in 1.18.0"
),
} }
) )
.extend(cv.polling_component_schema("60s")) .extend(cv.polling_component_schema("60s"))
@ -49,4 +53,4 @@ def to_code(config):
yield i2c.register_i2c_device(var, config) yield i2c.register_i2c_device(var, config)
cg.add(var.set_resolution(config[CONF_RESOLUTION])) cg.add(var.set_resolution(config[CONF_RESOLUTION]))
cg.add(var.set_measurement_time(config[CONF_MEASUREMENT_TIME])) cg.add(var.set_measurement_duration(config[CONF_MEASUREMENT_DURATION]))

View file

@ -412,7 +412,7 @@ sensor:
retain: False retain: False
availability: availability:
state_topic: livingroom/custom_state_topic state_topic: livingroom/custom_state_topic
measurement_time: 31 measurement_duration: 31
- platform: bme280 - platform: bme280
temperature: temperature:
name: 'Outside Temperature' name: 'Outside Temperature'