From 8d08c8213229da6d6d99b6bbdd85055aa1c386a2 Mon Sep 17 00:00:00 2001 From: Stephen Tierney Date: Mon, 1 Feb 2021 21:43:50 +1100 Subject: [PATCH] Fix more linting errors --- esphome/components/ltr390/ltr390.cpp | 100 +++++++++++++-------------- esphome/components/ltr390/ltr390.h | 8 +-- esphome/components/ltr390/sensor.py | 4 +- 3 files changed, 54 insertions(+), 58 deletions(-) diff --git a/esphome/components/ltr390/ltr390.cpp b/esphome/components/ltr390/ltr390.cpp index b1d0ce2f62..0b39d0c1fb 100644 --- a/esphome/components/ltr390/ltr390.cpp +++ b/esphome/components/ltr390/ltr390.cpp @@ -49,9 +49,7 @@ ltr390_mode_t LTR390Component::get_mode_(void) { return (ltr390_mode_t)(int) crtl_value[LTR390_CTRL_MODE]; } -void LTR390Component::set_gain_(ltr390_gain_t gain) { - *this->gain_reg_ = gain; -} +void LTR390Component::set_gain_(ltr390_gain_t gain) { *this->gain_reg_ = gain; } ltr390_gain_t LTR390Component::get_gain_(void) { std::bitset<8> gain_value(this->gain_reg_->get()); @@ -73,7 +71,7 @@ void LTR390Component::set_resolution_(ltr390_resolution_t res) { ltr390_resolution_t LTR390Component::get_resolution_(void) { std::bitset<8> res_value(this->res_reg_->get()); - std::bitset<3> output_value (0); + std::bitset<3> output_value(0); for (int i = 0; i < 3; i++) { output_value[i] = res_value[4 + i]; } @@ -111,6 +109,50 @@ uint32_t LTR390Component::read_sensor_data_(ltr390_mode_t mode) { return little_endian_bytes_to_int(buffer, num_bytes); } +void LTR390Component::read_als_() { + uint32_t als = this->read_sensor_data(LTR390_MODE_ALS); + + if (this->light_sensor_ != nullptr) { + float lux = (0.6 * als) / (gain_values_[this->gain_] * resolution_values_[this->res_]) * this->wfac_; + this->light_sensor_->publish_state(lux); + } + + if (this->als_sensor_ != nullptr) { + this->als_sensor_->publish_state(als); + } +} + +void LTR390Component::read_uvs_() { + uint32_t uv = this->read_sensor_data(LTR390_MODE_UVS); + + if (this->uvi_sensor_ != nullptr) { + this->uvi_sensor_->publish_state(uv / LTR390_SENSITIVITY * this->wfac_); + } + + if (this->uv_sensor_ != nullptr) { + this->uv_sensor_->publish_state(uv); + } +} + +void LTR390Component::read_mode_(int mode_index) { + // Set mode + this->set_mode(std::get<0>(this->mode_funcs_->at(mode_index))); + + // After the sensor integration time do the following + this->set_timeout(resolution_values_[this->res_] * 100, [this, mode_index]() { + // Read from the sensor + std::get<1>(this->mode_funcs_->at(mode_index))(); + + // If there are more modes to read then begin the next + // otherwise stop + if (mode_index + 1 < this->mode_funcs_->size()) { + this->read_mode(mode_index + 1); + } else { + this->reading = false; + } + }); +} + void LTR390Component::setup() { ESP_LOGCONFIG(TAG, "Setting up ltr390..."); @@ -151,57 +193,9 @@ void LTR390Component::setup() { } } -void LTR390Component::dump_config() { - LOG_I2C_DEVICE(this); -} - -void LTR390Component::read_als_() { - uint32_t als = this->read_sensor_data(LTR390_MODE_ALS); - - if (this->light_sensor_ != nullptr) { - float lux = (0.6 * als) / (gain_values_[this->gain_] * resolution_values_[this->res_]) * this->wfac_; - this->light_sensor_->publish_state(lux); - } - - if (this->als_sensor_ != nullptr) { - this->als_sensor_->publish_state(als); - } -} - -void LTR390Component::read_uvs_() { - uint32_t uv = this->read_sensor_data(LTR390_MODE_UVS); - - if (this->uvi_sensor_ != nullptr) { - this->uvi_sensor_->publish_state(uv / LTR390_SENSITIVITY * this->wfac_); - } - - if (this->uv_sensor_ != nullptr) { - this->uv_sensor_->publish_state(uv); - } -} - -void LTR390Component::read_mode_(int mode_index) { - - // Set mode - this->set_mode(std::get<0>(this->mode_funcs_->at(mode_index))); - - // After the sensor integration time do the following - this->set_timeout(resolution_values_[this->res_] * 100, [this, mode_index]() { - // Read from the sensor - std::get<1>(this->mode_funcs_->at(mode_index))(); - - // If there are more modes to read then begin the next - // otherwise stop - if (mode_index + 1 < this->mode_funcs_->size()) { - this->read_mode(mode_index + 1); - } else { - this->reading = false; - } - }); -} +void LTR390Component::dump_config() { LOG_I2C_DEVICE(this); } void LTR390Component::update() { - if (!this->reading) { this->reading = true; this->read_mode(0); diff --git a/esphome/components/ltr390/ltr390.h b/esphome/components/ltr390/ltr390.h index f435b68682..3c82ace55a 100644 --- a/esphome/components/ltr390/ltr390.h +++ b/esphome/components/ltr390/ltr390.h @@ -52,11 +52,11 @@ enum ltr390_resolution_t { }; class LTR390Component : public PollingComponent, public i2c::I2CDevice { -public: + public: + float get_setup_priority() const override { return setup_priority::DATA; } void setup() override; void dump_config() override; void update() override; - float get_setup_priority() const override { return setup_priority::DATA; } void set_gain_value(ltr390_gain_t gain) { this->gain_ = gain; } void set_res_value(ltr390_resolution_t res) { this->res_ = res; } @@ -67,7 +67,7 @@ public: void set_uvi_sensor(sensor::Sensor *uvi_sensor) { this->uvi_sensor_ = uvi_sensor; } void set_uv_sensor(sensor::Sensor *uv_sensor) { this->uv_sensor_ = uv_sensor; } -protected: + protected: bool enabled_(); void enable_(bool en); @@ -92,7 +92,7 @@ protected: std::atomic reading_; - std::vector< std::tuple< ltr390_mode_t, std::function > > *mode_funcs_; + std::vector > > *mode_funcs_; i2c::I2CRegister *ctrl_reg_; i2c::I2CRegister *status_reg_; diff --git a/esphome/components/ltr390/sensor.py b/esphome/components/ltr390/sensor.py index 24ce24fa11..576380e633 100644 --- a/esphome/components/ltr390/sensor.py +++ b/esphome/components/ltr390/sensor.py @@ -1,7 +1,8 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c, sensor -from esphome.const import CONF_ID, CONF_GAIN, CONF_LIGHT, CONF_RESOLUTION, UNIT_LUX, ICON_BRIGHTNESS_5 +from esphome.const import CONF_ID, CONF_GAIN, CONF_LIGHT, CONF_RESOLUTION, + UNIT_LUX, ICON_BRIGHTNESS_5 DEPENDENCIES = ['i2c'] @@ -58,6 +59,7 @@ TYPES = { CONF_UV: 'set_uv_sensor', } + def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config)