diff --git a/esphome/components/ads1115/ads1115.cpp b/esphome/components/ads1115/ads1115.cpp index a6fd2d76cd..924cd1e3e4 100644 --- a/esphome/components/ads1115/ads1115.cpp +++ b/esphome/components/ads1115/ads1115.cpp @@ -60,7 +60,7 @@ void ADS1115Component::setup() { for (auto *sensor : this->sensors_) { this->set_interval(sensor->get_name(), sensor->update_interval(), [this, sensor] { - this->request_measurement_(sensor); + this->request_measurement(sensor); }); } } @@ -78,7 +78,7 @@ void ADS1115Component::dump_config() { } } float ADS1115Component::get_setup_priority() const { return setup_priority::DATA; } -float ADS1115Component::request_measurement_(ADS1115Sensor *sensor) { +float ADS1115Component::request_measurement(ADS1115Sensor *sensor) { uint16_t config; if (!this->read_byte_16(ADS1115_REGISTER_CONFIG, &config)) { this->status_set_warning(); @@ -154,10 +154,10 @@ void ADS1115Sensor::set_multiplexer(ADS1115Multiplexer multiplexer) { this->mult uint8_t ADS1115Sensor::get_gain() const { return this->gain_; } void ADS1115Sensor::set_gain(ADS1115Gain gain) { this->gain_ = gain; } float ADS1115Sensor::sample() { - return this->parent_->request_measurement_(this); + return this->parent_->request_measurement(this); } void ADS1115Sensor::update() { - float v = this->parent_->request_measurement_(this); + float v = this->parent_->request_measurement(this); if (!isnan(v)) { ESP_LOGD(TAG, "'%s': Got Voltage=%fV", this->get_name().c_str(), v); this->publish_state(v); diff --git a/esphome/components/ads1115/ads1115.h b/esphome/components/ads1115/ads1115.h index 28efc1f5ba..f9d00f9b19 100644 --- a/esphome/components/ads1115/ads1115.h +++ b/esphome/components/ads1115/ads1115.h @@ -40,7 +40,7 @@ class ADS1115Component : public Component, public i2c::I2CDevice { float get_setup_priority() const override; /// Helper method to request a measurement from a sensor. - float request_measurement_(ADS1115Sensor *sensor); + float request_measurement(ADS1115Sensor *sensor); protected: std::vector sensors_; diff --git a/esphome/components/bh1750/sensor.py b/esphome/components/bh1750/sensor.py index 27ee3d1b85..b3ce0eaf88 100644 --- a/esphome/components/bh1750/sensor.py +++ b/esphome/components/bh1750/sensor.py @@ -13,7 +13,7 @@ BH1750_RESOLUTIONS = { 0.5: BH1750Resolution.BH1750_RESOLUTION_0P5_LX, } -BH1750Sensor = bh1750_ns.class_('BH1750Sensor', sensor.PollingSensorComponent, i2c.I2CDevice) +BH1750Sensor = bh1750_ns.class_('BH1750Sensor', sensor.Sensor, cg.PollingComponent, i2c.I2CDevice) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_LUX, ICON_BRIGHTNESS_5, 1).extend({ cv.GenerateID(): cv.declare_id(BH1750Sensor), diff --git a/esphome/components/duty_cycle/sensor.py b/esphome/components/duty_cycle/sensor.py index d60ff0d8be..51d99aae6a 100644 --- a/esphome/components/duty_cycle/sensor.py +++ b/esphome/components/duty_cycle/sensor.py @@ -5,7 +5,7 @@ from esphome.components import sensor from esphome.const import CONF_ID, CONF_PIN, UNIT_PERCENT, ICON_PERCENT duty_cycle_ns = cg.esphome_ns.namespace('duty_cycle') -DutyCycleSensor = duty_cycle_ns.class_('DutyCycleSensor', sensor.PollingSensorComponent) +DutyCycleSensor = duty_cycle_ns.class_('DutyCycleSensor', sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_PERCENT, ICON_PERCENT, 1).extend({ cv.GenerateID(): cv.declare_id(DutyCycleSensor), diff --git a/esphome/components/esp32_hall/esp32_hall.h b/esphome/components/esp32_hall/esp32_hall.h index 9727d45aa6..040280fff3 100644 --- a/esphome/components/esp32_hall/esp32_hall.h +++ b/esphome/components/esp32_hall/esp32_hall.h @@ -8,11 +8,8 @@ namespace esphome { namespace esp32_hall { -class ESP32HallSensor : public sensor::PollingSensorComponent { +class ESP32HallSensor : public sensor::Sensor, public PollingComponent { public: - explicit ESP32HallSensor(const std::string &name, uint32_t update_interval) - : sensor::PollingSensorComponent(name, update_interval) {} - void dump_config() override; void update() override; diff --git a/esphome/components/esp32_hall/sensor.py b/esphome/components/esp32_hall/sensor.py index 81a90c8c10..ec24f1aab6 100644 --- a/esphome/components/esp32_hall/sensor.py +++ b/esphome/components/esp32_hall/sensor.py @@ -1,21 +1,19 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor -from esphome.const import CONF_ID, CONF_NAME, CONF_UPDATE_INTERVAL, ESP_PLATFORM_ESP32, \ - UNIT_MICROTESLA, ICON_MAGNET +from esphome.const import CONF_ID, ESP_PLATFORM_ESP32, UNIT_MICROTESLA, ICON_MAGNET ESP_PLATFORMS = [ESP_PLATFORM_ESP32] esp32_hall_ns = cg.esphome_ns.namespace('esp32_hall') -ESP32HallSensor = esp32_hall_ns.class_('ESP32HallSensor', sensor.PollingSensorComponent) +ESP32HallSensor = esp32_hall_ns.class_('ESP32HallSensor', sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_MICROTESLA, ICON_MAGNET, 1).extend({ cv.GenerateID(): cv.declare_id(ESP32HallSensor), - cv.Optional(CONF_UPDATE_INTERVAL, default='60s'): cv.update_interval, -}).extend(cv.COMPONENT_SCHEMA) +}).extend(cv.polling_component_schema('60s')) def to_code(config): - var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], config[CONF_UPDATE_INTERVAL]) + var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield sensor.register_sensor(var, config) diff --git a/esphome/components/hx711/sensor.py b/esphome/components/hx711/sensor.py index bc22397065..2fc333a243 100644 --- a/esphome/components/hx711/sensor.py +++ b/esphome/components/hx711/sensor.py @@ -5,7 +5,7 @@ from esphome.components import sensor from esphome.const import CONF_CLK_PIN, CONF_GAIN, CONF_ID, ICON_SCALE hx711_ns = cg.esphome_ns.namespace('hx711') -HX711Sensor = hx711_ns.class_('HX711Sensor', sensor.PollingSensorComponent) +HX711Sensor = hx711_ns.class_('HX711Sensor', sensor.Sensor, cg.PollingComponent) CONF_DOUT_PIN = 'dout_pin' diff --git a/esphome/components/light/automation.py b/esphome/components/light/automation.py index 42a8319728..629073707e 100644 --- a/esphome/components/light/automation.py +++ b/esphome/components/light/automation.py @@ -128,7 +128,10 @@ def light_addressable_set_to_code(config, action_id, template_arg, args): if CONF_RANGE_TO in config: templ = yield cg.templatable(config[CONF_RANGE_TO], args, cg.int32) cg.add(var.set_range_to(templ)) - rgbw_to_exp = lambda x: int(round(x * 255)) + + def rgbw_to_exp(x): + return int(round(x * 255)) + if CONF_RED in config: templ = yield cg.templatable(config[CONF_RED], args, cg.uint8, to_exp=rgbw_to_exp) cg.add(var.set_red(templ)) diff --git a/esphome/components/max31855/sensor.py b/esphome/components/max31855/sensor.py index 7178488ebb..b4d9f82b03 100644 --- a/esphome/components/max31855/sensor.py +++ b/esphome/components/max31855/sensor.py @@ -4,7 +4,8 @@ from esphome.components import sensor, spi from esphome.const import CONF_ID, ICON_THERMOMETER, UNIT_CELSIUS max31855_ns = cg.esphome_ns.namespace('max31855') -MAX31855Sensor = max31855_ns.class_('MAX31855Sensor', sensor.PollingSensorComponent, spi.SPIDevice) +MAX31855Sensor = max31855_ns.class_('MAX31855Sensor', sensor.Sensor, cg.PollingComponent, + spi.SPIDevice) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1).extend({ cv.GenerateID(): cv.declare_id(MAX31855Sensor), diff --git a/esphome/components/max6675/sensor.py b/esphome/components/max6675/sensor.py index af089614f0..59d24a5283 100644 --- a/esphome/components/max6675/sensor.py +++ b/esphome/components/max6675/sensor.py @@ -4,7 +4,7 @@ from esphome.components import sensor, spi from esphome.const import CONF_ID, ICON_THERMOMETER, UNIT_CELSIUS max6675_ns = cg.esphome_ns.namespace('max6675') -MAX6675Sensor = max6675_ns.class_('MAX6675Sensor', sensor.PollingSensorComponent, +MAX6675Sensor = max6675_ns.class_('MAX6675Sensor', sensor.Sensor, cg.PollingComponent, spi.SPIDevice) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1).extend({ diff --git a/esphome/components/neopixelbus/neopixelbus_light.h b/esphome/components/neopixelbus/neopixelbus_light.h index 86ae21ddd0..68d99fb374 100644 --- a/esphome/components/neopixelbus/neopixelbus_light.h +++ b/esphome/components/neopixelbus/neopixelbus_light.h @@ -152,7 +152,7 @@ class NeoPixelRGBLightOutput : public NeoPixelBusLightOutputBasecontroller_->Pixels() + 3ULL * index; return light::ESPColorView(base + this->rgb_offsets_[0], base + this->rgb_offsets_[1], base + this->rgb_offsets_[2], nullptr, this->effect_data_ + index, &this->correction_); @@ -171,7 +171,7 @@ class NeoPixelRGBWLightOutput : public NeoPixelBusLightOutputBasecontroller_->Pixels() + 4ULL * index; return light::ESPColorView(base + this->rgb_offsets_[0], base + this->rgb_offsets_[1], base + this->rgb_offsets_[2], base + this->rgb_offsets_[3], this->effect_data_ + index, &this->correction_); diff --git a/esphome/components/partition/light_partition.h b/esphome/components/partition/light_partition.h index 398cbc6664..b68e3404f1 100644 --- a/esphome/components/partition/light_partition.h +++ b/esphome/components/partition/light_partition.h @@ -37,7 +37,23 @@ class PartitionLightOutput : public light::AddressableLight, public Component { auto &last_seg = this->segments_[this->segments_.size() - 1]; return last_seg.get_dst_offset() + last_seg.get_size(); } - light::ESPColorView operator[](int32_t index) const override { + void clear_effect_data() override { + for (auto &seg : this->segments_) { + seg.get_src()->clear_effect_data(); + } + } + light::LightTraits get_traits() override { return this->segments_[0].get_src()->get_traits(); } + void loop() override { + if (this->should_show_()) { + for (auto seg : this->segments_) { + seg.get_src()->schedule_show(); + } + this->mark_shown_(); + } + } + + protected: + light::ESPColorView get_view_internal(int32_t index) const override { uint32_t lo = 0; uint32_t hi = this->segments_.size() - 1; while (lo < hi) { @@ -61,22 +77,7 @@ class PartitionLightOutput : public light::AddressableLight, public Component { view.raw_set_color_correction(&this->correction_); return view; } - void clear_effect_data() override { - for (auto &seg : this->segments_) { - seg.get_src()->clear_effect_data(); - } - } - light::LightTraits get_traits() override { return this->segments_[0].get_src()->get_traits(); } - void loop() override { - if (this->should_show_()) { - for (auto seg : this->segments_) { - seg.get_src()->schedule_show(); - } - this->mark_shown_(); - } - } - protected: std::vector segments_; }; diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index 6fbdb00945..636147c138 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -18,7 +18,7 @@ COUNT_MODES = { COUNT_MODE_SCHEMA = cv.enum(COUNT_MODES, upper=True) PulseCounterSensor = pulse_counter_ns.class_('PulseCounterSensor', - sensor.PollingSensorComponent) + sensor.Sensor, cg.PollingComponent) def validate_internal_filter(value): diff --git a/esphome/components/pulse_width/sensor.py b/esphome/components/pulse_width/sensor.py index 42d1b0dbaa..0227adffce 100644 --- a/esphome/components/pulse_width/sensor.py +++ b/esphome/components/pulse_width/sensor.py @@ -6,7 +6,7 @@ from esphome.const import CONF_ID, CONF_PIN, UNIT_SECOND, ICON_TIMER pulse_width_ns = cg.esphome_ns.namespace('pulse_width') -PulseWidthSensor = pulse_width_ns.class_('PulseWidthSensor', sensor.PollingSensorComponent) +PulseWidthSensor = pulse_width_ns.class_('PulseWidthSensor', sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_SECOND, ICON_TIMER, 3).extend({ cv.GenerateID(): cv.declare_id(PulseWidthSensor), diff --git a/esphome/components/template/sensor/__init__.py b/esphome/components/template/sensor/__init__.py index 1e9fe9cdee..788520877a 100644 --- a/esphome/components/template/sensor/__init__.py +++ b/esphome/components/template/sensor/__init__.py @@ -5,7 +5,7 @@ from esphome.components import sensor from esphome.const import CONF_ID, CONF_LAMBDA, CONF_STATE, UNIT_EMPTY, ICON_EMPTY from .. import template_ns -TemplateSensor = template_ns.class_('TemplateSensor', sensor.PollingSensorComponent) +TemplateSensor = template_ns.class_('TemplateSensor', sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_EMPTY, ICON_EMPTY, 1).extend({ cv.GenerateID(): cv.declare_id(TemplateSensor), diff --git a/esphome/components/tsl2561/sensor.py b/esphome/components/tsl2561/sensor.py index 5171884a14..e2e333cc81 100644 --- a/esphome/components/tsl2561/sensor.py +++ b/esphome/components/tsl2561/sensor.py @@ -27,7 +27,8 @@ def validate_integration_time(value): return cv.enum(INTEGRATION_TIMES, int=True)(value) -TSL2561Sensor = tsl2561_ns.class_('TSL2561Sensor', sensor.PollingSensorComponent, i2c.I2CDevice) +TSL2561Sensor = tsl2561_ns.class_('TSL2561Sensor', sensor.Sensor, cg.PollingComponent, + i2c.I2CDevice) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_LUX, ICON_BRIGHTNESS_5, 1).extend({ cv.GenerateID(): cv.declare_id(TSL2561Sensor), diff --git a/esphome/components/ultrasonic/sensor.py b/esphome/components/ultrasonic/sensor.py index fa3934853c..e4364f271c 100644 --- a/esphome/components/ultrasonic/sensor.py +++ b/esphome/components/ultrasonic/sensor.py @@ -9,7 +9,7 @@ CONF_PULSE_TIME = 'pulse_time' ultrasonic_ns = cg.esphome_ns.namespace('ultrasonic') UltrasonicSensorComponent = ultrasonic_ns.class_('UltrasonicSensorComponent', - sensor.PollingSensorComponent) + sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_METER, ICON_ARROW_EXPAND_VERTICAL, 2).extend({ cv.GenerateID(): cv.declare_id(UltrasonicSensorComponent), diff --git a/esphome/components/uptime/sensor.py b/esphome/components/uptime/sensor.py index e4f78c6411..1dacc99653 100644 --- a/esphome/components/uptime/sensor.py +++ b/esphome/components/uptime/sensor.py @@ -4,7 +4,7 @@ from esphome.components import sensor from esphome.const import CONF_ID, UNIT_SECOND, ICON_TIMER uptime_ns = cg.esphome_ns.namespace('uptime') -UptimeSensor = uptime_ns.class_('UptimeSensor', sensor.PollingSensorComponent) +UptimeSensor = uptime_ns.class_('UptimeSensor', sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_SECOND, ICON_TIMER, 0).extend({ cv.GenerateID(): cv.declare_id(UptimeSensor), diff --git a/esphome/components/wifi_signal/sensor.py b/esphome/components/wifi_signal/sensor.py index e53daede1f..1cc58009af 100644 --- a/esphome/components/wifi_signal/sensor.py +++ b/esphome/components/wifi_signal/sensor.py @@ -5,7 +5,7 @@ from esphome.const import CONF_ID, ICON_WIFI, UNIT_DECIBEL DEPENDENCIES = ['wifi'] wifi_signal_ns = cg.esphome_ns.namespace('wifi_signal') -WiFiSignalSensor = wifi_signal_ns.class_('WiFiSignalSensor', sensor.PollingSensorComponent) +WiFiSignalSensor = wifi_signal_ns.class_('WiFiSignalSensor', sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_DECIBEL, ICON_WIFI, 0).extend({ cv.GenerateID(): cv.declare_id(WiFiSignalSensor), diff --git a/script/build_compile_commands.py b/script/build_compile_commands.py index 91eb75bd00..e6d761771f 100755 --- a/script/build_compile_commands.py +++ b/script/build_compile_commands.py @@ -7,6 +7,7 @@ import sys root_path = os.path.abspath(os.path.normpath(os.path.join(__file__, '..', '..'))) basepath = os.path.join(root_path, 'esphome') +temp_header_file = os.path.join(root_path, '.temp-clang-tidy.cpp') def walk_files(path): @@ -24,6 +25,24 @@ def shlex_quote(s): return u"'" + s.replace(u"'", u"'\"'\"'") + u"'" +def build_all_include(): + # Build a cpp file that includes all header files in this repo. + # Otherwise header-only integrations would not be tested by clang-tidy + headers = [] + for path in walk_files(basepath): + filetypes = ('.h',) + ext = os.path.splitext(path)[1] + if ext in filetypes: + path = os.path.relpath(path, root_path) + include_p = path.replace(os.path.sep, '/') + headers.append('#include "{}"'.format(include_p)) + headers.sort() + headers.append('') + content = '\n'.join(headers) + with codecs.open(temp_header_file, 'w', encoding='utf-8') as f: + f.write(content) + + def build_compile_commands(): gcc_flags_json = os.path.join(root_path, '.gcc-flags.json') if not os.path.isfile(gcc_flags_json): @@ -52,6 +71,7 @@ def build_compile_commands(): ext = os.path.splitext(path)[1] if ext in filetypes: source_files.append(os.path.abspath(path)) + source_files.append(temp_header_file) source_files.sort() compile_commands = [{ 'directory': root_path, @@ -71,6 +91,7 @@ def build_compile_commands(): def main(): + build_all_include() build_compile_commands() print("Done.") diff --git a/script/clang-tidy.py b/script/clang-tidy.py index 5b6129b35a..7187c16454 100755 --- a/script/clang-tidy.py +++ b/script/clang-tidy.py @@ -264,8 +264,6 @@ def main(): print('Ctrl-C detected, goodbye.') if tmpdir: shutil.rmtree(tmpdir) - if os.path.exists(temp_header_file): - os.remove(temp_header_file) os.kill(0, 9) if args.fix and failed_files: @@ -274,12 +272,8 @@ def main(): subprocess.call(['clang-apply-replacements-7', tmpdir]) except: print('Error applying fixes.\n', file=sys.stderr) - if os.path.exists(temp_header_file): - os.remove(temp_header_file) raise - if os.path.exists(temp_header_file): - os.remove(temp_header_file) sys.exit(return_code)