Hi,
The issue is related to the I2C priority which is part of the BUS priority and defined as the highest (1000).
IMO any switch initialization with the on_boot (see the code below), with a priority higher than 800 will be overwritten during the boot sequence. Each hardware part is initialized following the priority order defined from the highest to the lowest. And in priority 800 all GPIO switch will be initialized, overwriting any setting previously made with the additional “on_boot” sequence above priority 800.
esphome:
name: myproject
friendly_name: thermostat
on_boot:
priority: 1001
then:
- switch.turn_on: bme280_power_on
I don’t know why the BUS priority has been set to the highest. I’m sure there is a good explanation but I don’t see it.
In no case the solution below is a definitive solution but it allows me to achieve my goal which is to control the power supply of my BME280 measurement module and thus to reduce my consumption to a minimum (my project works with a battery )
So, to get around this and to be able to wakeup from deep sleep + enable my BME280 with one GPIO before the I2C bus initialization, I decided to use my own I2C component modified with a lower priority. I proceed in this way:
In my yaml file, I have added the following to tell the compiler to use any component located in my_components local folder instead of the original one:
external_components:
- source:
type: local
path: my_components
my_components is a local folder located at the same level as my yaml file.
Next I copied the original component folder I2C into my_components.
Once copied locally, I edited the i2c_bus_arduino.h file and searched for the line
float get_setup_priority() const override { return setup_priority::BUS; }
line 24 in my case.
- I duplicated line 24 and commented it
- In the duplicated line 25, I replaced the priority level “BUS” with “DATA”, which corresponds to a priority level of 600 (lower than the switch priority of 800). See the modified code below
- And saved
class ArduinoI2CBus : public I2CBus, public Component {
public:
void setup() override;
void dump_config() override;
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override;
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override;
// float get_setup_priority() const override { return setup_priority::BUS; }
float get_setup_priority() const override { return setup_priority::DATA; }
Each time I compile, my modified file is taken in account and the I2C has a priority of 600 lower than the switch priority of 800. Now, no problem to set a priority in between like 700 in my on_boot to enable my BM280 power supply with a GPIO switch. Next the I2C bus with the priority 600 will start its initialization w/o any issue.
esphome:
name: myproject
friendly_name: thermostat
on_boot:
priority: 700
then:
- switch.turn_on: bme280_power_on
Again this is my own workaround before a official solution is documented.
Hope it helps