Minor tidy up of BME280 code (#6672)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Anton Viktorov 2024-05-06 04:19:25 +02:00 committed by GitHub
parent 833d31ef7a
commit 599dbf27e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 231 additions and 371 deletions

View File

@ -1 +1,108 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
CONF_HUMIDITY,
CONF_ID,
CONF_IIR_FILTER,
CONF_OVERSAMPLING,
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
UNIT_PERCENT,
)
CODEOWNERS = ["@esphome/core"]
bme280_ns = cg.esphome_ns.namespace("bme280_base")
BME280Oversampling = bme280_ns.enum("BME280Oversampling")
OVERSAMPLING_OPTIONS = {
"NONE": BME280Oversampling.BME280_OVERSAMPLING_NONE,
"1X": BME280Oversampling.BME280_OVERSAMPLING_1X,
"2X": BME280Oversampling.BME280_OVERSAMPLING_2X,
"4X": BME280Oversampling.BME280_OVERSAMPLING_4X,
"8X": BME280Oversampling.BME280_OVERSAMPLING_8X,
"16X": BME280Oversampling.BME280_OVERSAMPLING_16X,
}
BME280IIRFilter = bme280_ns.enum("BME280IIRFilter")
IIR_FILTER_OPTIONS = {
"OFF": BME280IIRFilter.BME280_IIR_FILTER_OFF,
"2X": BME280IIRFilter.BME280_IIR_FILTER_2X,
"4X": BME280IIRFilter.BME280_IIR_FILTER_4X,
"8X": BME280IIRFilter.BME280_IIR_FILTER_8X,
"16X": BME280IIRFilter.BME280_IIR_FILTER_16X,
}
CONFIG_SCHEMA_BASE = cv.Schema(
{
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
IIR_FILTER_OPTIONS, upper=True
),
}
).extend(cv.polling_component_schema("60s"))
async def to_code_base(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING]))
if pressure_config := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_config)
cg.add(var.set_pressure_sensor(sens))
cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING]))
if humidity_config := config.get(CONF_HUMIDITY):
sens = await sensor.new_sensor(humidity_config)
cg.add(var.set_humidity_sensor(sens))
cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING]))
cg.add(var.set_iir_filter(config[CONF_IIR_FILTER]))
return var

View File

@ -1,106 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
CONF_HUMIDITY,
CONF_ID,
CONF_IIR_FILTER,
CONF_OVERSAMPLING,
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
UNIT_PERCENT,
)
bme280_ns = cg.esphome_ns.namespace("bme280_base")
BME280Oversampling = bme280_ns.enum("BME280Oversampling")
OVERSAMPLING_OPTIONS = {
"NONE": BME280Oversampling.BME280_OVERSAMPLING_NONE,
"1X": BME280Oversampling.BME280_OVERSAMPLING_1X,
"2X": BME280Oversampling.BME280_OVERSAMPLING_2X,
"4X": BME280Oversampling.BME280_OVERSAMPLING_4X,
"8X": BME280Oversampling.BME280_OVERSAMPLING_8X,
"16X": BME280Oversampling.BME280_OVERSAMPLING_16X,
}
BME280IIRFilter = bme280_ns.enum("BME280IIRFilter")
IIR_FILTER_OPTIONS = {
"OFF": BME280IIRFilter.BME280_IIR_FILTER_OFF,
"2X": BME280IIRFilter.BME280_IIR_FILTER_2X,
"4X": BME280IIRFilter.BME280_IIR_FILTER_4X,
"8X": BME280IIRFilter.BME280_IIR_FILTER_8X,
"16X": BME280IIRFilter.BME280_IIR_FILTER_16X,
}
CONFIG_SCHEMA_BASE = cv.Schema(
{
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
IIR_FILTER_OPTIONS, upper=True
),
}
).extend(cv.polling_component_schema("60s"))
async def to_code(config, func=None):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
if func is not None:
await func(var, config)
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING]))
if pressure_config := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_config)
cg.add(var.set_pressure_sensor(sens))
cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING]))
if humidity_config := config.get(CONF_HUMIDITY):
sens = await sensor.new_sensor(humidity_config)
cg.add(var.set_humidity_sensor(sens))
cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING]))
cg.add(var.set_iir_filter(config[CONF_IIR_FILTER]))

View File

@ -1,9 +1,10 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c
from ..bme280_base.sensor import to_code as to_code_base, cv, CONFIG_SCHEMA_BASE
from ..bme280_base import to_code_base, CONFIG_SCHEMA_BASE
DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["bme280_base"]
DEPENDENCIES = ["i2c"]
bme280_ns = cg.esphome_ns.namespace("bme280_i2c")
BME280I2CComponent = bme280_ns.class_(
@ -16,4 +17,5 @@ CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(
async def to_code(config):
await to_code_base(config, func=i2c.register_i2c_device)
var = await to_code_base(config)
await i2c.register_i2c_device(var, config)

View File

@ -1 +0,0 @@
CODEOWNERS = ["@apbodrov"]

View File

@ -4,19 +4,19 @@
#include "bme280_spi.h"
#include <esphome/components/bme280_base/bme280_base.h>
int set_bit(uint8_t num, int position) {
namespace esphome {
namespace bme280_spi {
uint8_t set_bit(uint8_t num, int position) {
int mask = 1 << position;
return num | mask;
}
int clear_bit(uint8_t num, int position) {
uint8_t clear_bit(uint8_t num, int position) {
int mask = 1 << position;
return num & ~mask;
}
namespace esphome {
namespace bme280_spi {
void BME280SPIComponent::setup() {
this->spi_setup();
BME280Component::setup();
@ -30,34 +30,33 @@ void BME280SPIComponent::setup() {
bool BME280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
this->enable();
// cause: *data = this->delegate_->transfer(tmp) doesnt work
this->delegate_->transfer(set_bit(a_register, 7));
*data = this->delegate_->transfer(0);
this->transfer_byte(set_bit(a_register, 7));
*data = this->transfer_byte(0);
this->disable();
return true;
}
bool BME280SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
this->enable();
this->delegate_->transfer(clear_bit(a_register, 7));
this->delegate_->transfer(data);
this->transfer_byte(clear_bit(a_register, 7));
this->transfer_byte(data);
this->disable();
return true;
}
bool BME280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
this->enable();
this->delegate_->transfer(set_bit(a_register, 7));
this->delegate_->read_array(data, len);
this->transfer_byte(set_bit(a_register, 7));
this->read_array(data, len);
this->disable();
return true;
}
bool BME280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
this->enable();
this->delegate_->transfer(set_bit(a_register, 7));
((uint8_t *) data)[1] = this->delegate_->transfer(0);
((uint8_t *) data)[0] = this->delegate_->transfer(0);
this->transfer_byte(set_bit(a_register, 7));
((uint8_t *) data)[1] = this->transfer_byte(0);
((uint8_t *) data)[0] = this->transfer_byte(0);
this->disable();
return true;
}

View File

@ -1,13 +1,11 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import spi
from esphome.components.bme280_base.sensor import (
to_code as to_code_base,
cv,
CONFIG_SCHEMA_BASE,
)
from ..bme280_base import to_code_base, CONFIG_SCHEMA_BASE
DEPENDENCIES = ["spi"]
AUTO_LOAD = ["bme280_base"]
CODEOWNERS = ["@apbodrov"]
DEPENDENCIES = ["spi"]
bme280_spi_ns = cg.esphome_ns.namespace("bme280_spi")
@ -21,4 +19,5 @@ CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(spi.spi_device_schema()).extend(
async def to_code(config):
await to_code_base(config, func=spi.register_spi_device)
var = await to_code_base(config)
await spi.register_spi_device(var, config)

View File

@ -0,0 +1,19 @@
i2c:
- id: i2c_bme280
scl: ${scl_pin}
sda: ${sda_pin}
sensor:
- platform: bme280_i2c
i2c_id: i2c_bme280
address: 0x76
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s

View File

@ -1,18 +1,5 @@
i2c:
- id: i2c_bme280
scl: 5
sda: 4
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
sensor:
- platform: bme280_i2c
address: 0x76
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,18 +1,5 @@
i2c:
- id: i2c_bme280
scl: 5
sda: 4
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
sensor:
- platform: bme280_i2c
address: 0x76
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,18 +1,5 @@
i2c:
- id: i2c_bme280
scl: 16
sda: 17
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
sensor:
- platform: bme280_i2c
address: 0x76
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,18 +1,5 @@
i2c:
- id: i2c_bme280
scl: 16
sda: 17
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
sensor:
- platform: bme280_i2c
address: 0x76
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,18 +1,5 @@
i2c:
- id: i2c_bme280
scl: 5
sda: 4
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
sensor:
- platform: bme280_i2c
address: 0x76
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,18 +1,5 @@
i2c:
- id: i2c_bme280
scl: 5
sda: 4
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
sensor:
- platform: bme280_i2c
address: 0x76
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -0,0 +1,20 @@
spi:
- id: spi_bme280
clk_pin: ${clk_pin}
mosi_pin: ${mosi_pin}
miso_pin: ${miso_pin}
sensor:
- platform: bme280_spi
spi_id: spi_bme280
cs_pin: ${cs_pin}
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s

View File

@ -1,19 +1,7 @@
spi:
- id: spi_bme280
clk_pin: 6
mosi_pin: 7
miso_pin: 5
substitutions:
clk_pin: GPIO6
mosi_pin: GPIO7
miso_pin: GPIO5
cs_pin: GPIO8
sensor:
- platform: bme280_spi
cs_pin: 8
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,19 +1,7 @@
spi:
- id: spi_bme280
clk_pin: 6
mosi_pin: 7
miso_pin: 5
substitutions:
clk_pin: GPIO6
mosi_pin: GPIO7
miso_pin: GPIO5
cs_pin: GPIO8
sensor:
- platform: bme280_spi
cs_pin: 8
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,19 +1,7 @@
spi:
- id: spi_bme280
clk_pin: 16
mosi_pin: 17
miso_pin: 15
substitutions:
clk_pin: GPIO16
mosi_pin: GPIO17
miso_pin: GPIO15
cs_pin: GPIO5
sensor:
- platform: bme280_spi
cs_pin: 12
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,19 +1,7 @@
spi:
- id: spi_bme280
clk_pin: 16
mosi_pin: 17
miso_pin: 15
substitutions:
clk_pin: GPIO16
mosi_pin: GPIO17
miso_pin: GPIO15
cs_pin: GPIO5
sensor:
- platform: bme280_spi
cs_pin: 12
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,19 +1,7 @@
spi:
- id: spi_bme280
clk_pin: 14
mosi_pin: 13
miso_pin: 12
substitutions:
clk_pin: GPIO14
mosi_pin: GPIO13
miso_pin: GPIO12
cs_pin: GPIO15
sensor:
- platform: bme280_spi
cs_pin: 15
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -1,19 +1,7 @@
spi:
- id: spi_bme280
clk_pin: 2
mosi_pin: 3
miso_pin: 4
substitutions:
clk_pin: GPIO2
mosi_pin: GPIO3
miso_pin: GPIO4
cs_pin: GPIO5
sensor:
- platform: bme280_spi
cs_pin: 6
temperature:
id: bme280_temperature
name: BME280 Temperature
humidity:
id: bme280_humidity
name: BME280 Humidity
pressure:
id: bme280_pressure
name: BME280 Pressure
update_interval: 15s
<<: !include common.yaml

View File

@ -705,35 +705,6 @@ sensor:
update_interval: 30s
mode: low_power
i2c_id: i2c_bus
- platform: bme280_i2c
temperature:
name: Outside Temperature
oversampling: 16x
pressure:
name: Outside Pressure
oversampling: none
humidity:
name: Outside Humidity
oversampling: 8x
address: 0x77
iir_filter: 16x
update_interval: 15s
i2c_id: i2c_bus
- platform: bme280_spi
temperature:
name: Outside Temperature
oversampling: 16x
pressure:
name: Outside Pressure
oversampling: none
humidity:
name: Outside Humidity
oversampling: 8x
cs_pin:
allow_other_uses: true
number: GPIO23
iir_filter: 16x
update_interval: 15s
- platform: bme680
temperature:
name: Outside Temperature