Skip to content

Commit

Permalink
Add temperature for QMC5883L (esphome#6456)
Browse files Browse the repository at this point in the history
  • Loading branch information
tronikos authored Apr 3, 2024
1 parent be8d188 commit 96f4c70
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 1 deletion.
16 changes: 15 additions & 1 deletion esphome/components/qmc5883l/qmc5883l.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void QMC5883LComponent::dump_config() {
LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
LOG_SENSOR(" ", "Heading", this->heading_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
}
float QMC5883LComponent::get_setup_priority() const { return setup_priority::DATA; }
void QMC5883LComponent::update() {
Expand Down Expand Up @@ -123,7 +124,18 @@ void QMC5883LComponent::update() {
heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
}

ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f° status=%u", x, y, z, heading, status);
float temp = NAN;
if (this->temperature_sensor_ != nullptr) {
uint16_t raw_temp;
if (!this->read_byte_16_(QMC5883L_REGISTER_TEMPERATURE_LSB, &raw_temp)) {
this->status_set_warning();
return;
}
temp = int16_t(raw_temp) * 0.01f;
}

ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f° temperature=%0.01f°C status=%u", x, y, z, heading,
temp, status);

if (this->x_sensor_ != nullptr)
this->x_sensor_->publish_state(x);
Expand All @@ -133,6 +145,8 @@ void QMC5883LComponent::update() {
this->z_sensor_->publish_state(z);
if (this->heading_sensor_ != nullptr)
this->heading_sensor_->publish_state(heading);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temp);
}

bool QMC5883LComponent::read_byte_16_(uint8_t a_register, uint16_t *data) {
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/qmc5883l/qmc5883l.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class QMC5883LComponent : public PollingComponent, public i2c::I2CDevice {
void set_y_sensor(sensor::Sensor *y_sensor) { y_sensor_ = y_sensor; }
void set_z_sensor(sensor::Sensor *z_sensor) { z_sensor_ = z_sensor; }
void set_heading_sensor(sensor::Sensor *heading_sensor) { heading_sensor_ = heading_sensor; }
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }

protected:
QMC5883LDatarate datarate_{QMC5883L_DATARATE_10_HZ};
Expand All @@ -49,6 +50,7 @@ class QMC5883LComponent : public PollingComponent, public i2c::I2CDevice {
sensor::Sensor *y_sensor_{nullptr};
sensor::Sensor *z_sensor_{nullptr};
sensor::Sensor *heading_sensor_{nullptr};
sensor::Sensor *temperature_sensor_{nullptr};
enum ErrorCode {
NONE = 0,
COMMUNICATION_FAILED,
Expand Down
13 changes: 13 additions & 0 deletions esphome/components/qmc5883l/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
CONF_FIELD_STRENGTH_X,
CONF_FIELD_STRENGTH_Y,
CONF_FIELD_STRENGTH_Z,
CONF_TEMPERATURE,
CONF_ID,
CONF_OVERSAMPLING,
CONF_RANGE,
DEVICE_CLASS_TEMPERATURE,
ICON_MAGNET,
STATE_CLASS_MEASUREMENT,
UNIT_MICROTESLA,
UNIT_CELSIUS,
UNIT_DEGREES,
ICON_SCREEN_ROTATION,
CONF_UPDATE_INTERVAL,
Expand Down Expand Up @@ -79,6 +82,12 @@ def validate_enum_bound(value):
icon=ICON_SCREEN_ROTATION,
accuracy_decimals=1,
)
temperature_schema = sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
)

CONFIG_SCHEMA = (
cv.Schema(
Expand All @@ -95,6 +104,7 @@ def validate_enum_bound(value):
cv.Optional(CONF_FIELD_STRENGTH_Y): field_strength_schema,
cv.Optional(CONF_FIELD_STRENGTH_Z): field_strength_schema,
cv.Optional(CONF_HEADING): heading_schema,
cv.Optional(CONF_TEMPERATURE): temperature_schema,
}
)
.extend(cv.polling_component_schema("60s"))
Expand Down Expand Up @@ -131,3 +141,6 @@ async def to_code(config):
if CONF_HEADING in config:
sens = await sensor.new_sensor(config[CONF_HEADING])
cg.add(var.set_heading_sensor(sens))
if CONF_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature_sensor(sens))
2 changes: 2 additions & 0 deletions tests/components/qmc5883l/test.esp32-c3-idf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sensor:
name: QMC5883L Field Strength Z
heading:
name: QMC5883L Heading
temperature:
name: QMC5883L Temperature
range: 800uT
oversampling: 256x
update_interval: 15s
2 changes: 2 additions & 0 deletions tests/components/qmc5883l/test.esp32-c3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sensor:
name: QMC5883L Field Strength Z
heading:
name: QMC5883L Heading
temperature:
name: QMC5883L Temperature
range: 800uT
oversampling: 256x
update_interval: 15s
2 changes: 2 additions & 0 deletions tests/components/qmc5883l/test.esp32-idf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sensor:
name: QMC5883L Field Strength Z
heading:
name: QMC5883L Heading
temperature:
name: QMC5883L Temperature
range: 800uT
oversampling: 256x
update_interval: 15s
2 changes: 2 additions & 0 deletions tests/components/qmc5883l/test.esp32.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sensor:
name: QMC5883L Field Strength Z
heading:
name: QMC5883L Heading
temperature:
name: QMC5883L Temperature
range: 800uT
oversampling: 256x
update_interval: 15s
2 changes: 2 additions & 0 deletions tests/components/qmc5883l/test.esp8266.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sensor:
name: QMC5883L Field Strength Z
heading:
name: QMC5883L Heading
temperature:
name: QMC5883L Temperature
range: 800uT
oversampling: 256x
update_interval: 15s
2 changes: 2 additions & 0 deletions tests/components/qmc5883l/test.rp2040.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sensor:
name: QMC5883L Field Strength Z
heading:
name: QMC5883L Heading
temperature:
name: QMC5883L Temperature
range: 800uT
oversampling: 256x
update_interval: 15s

0 comments on commit 96f4c70

Please sign in to comment.