mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-04 20:09:35 +02:00
mcp3008: Tidy up and fix auto load bug (#5842)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor, voltage_sampler
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_NUMBER,
|
||||
UNIT_VOLT,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
)
|
||||
|
||||
from .. import mcp3008_ns, MCP3008
|
||||
|
||||
AUTO_LOAD = ["voltage_sampler"]
|
||||
|
||||
DEPENDENCIES = ["mcp3008"]
|
||||
|
||||
MCP3008Sensor = mcp3008_ns.class_(
|
||||
"MCP3008Sensor",
|
||||
sensor.Sensor,
|
||||
cg.PollingComponent,
|
||||
voltage_sampler.VoltageSampler,
|
||||
cg.Parented.template(MCP3008),
|
||||
)
|
||||
CONF_REFERENCE_VOLTAGE = "reference_voltage"
|
||||
CONF_MCP3008_ID = "mcp3008_id"
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(
|
||||
MCP3008Sensor,
|
||||
unit_of_measurement=UNIT_VOLT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(CONF_MCP3008_ID): cv.use_id(MCP3008),
|
||||
cv.Required(CONF_NUMBER): cv.int_,
|
||||
cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage,
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_parented(var, config[CONF_MCP3008_ID])
|
||||
await cg.register_component(var, config)
|
||||
await sensor.register_sensor(var, config)
|
||||
|
||||
cg.add(var.set_pin(config[CONF_NUMBER]))
|
||||
cg.add(var.set_reference_voltage(config[CONF_REFERENCE_VOLTAGE]))
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "mcp3008_sensor.h"
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mcp3008 {
|
||||
|
||||
static const char *const TAG = "mcp3008.sensor";
|
||||
|
||||
float MCP3008Sensor::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
void MCP3008Sensor::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "MCP3008Sensor:");
|
||||
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
|
||||
ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
|
||||
}
|
||||
|
||||
float MCP3008Sensor::sample() {
|
||||
float value_v = this->parent_->read_data(pin_);
|
||||
value_v = (value_v * this->reference_voltage_);
|
||||
return value_v;
|
||||
}
|
||||
|
||||
void MCP3008Sensor::update() { this->publish_state(this->sample()); }
|
||||
|
||||
} // namespace mcp3008
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/voltage_sampler/voltage_sampler.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
#include "../mcp3008.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mcp3008 {
|
||||
|
||||
class MCP3008Sensor : public PollingComponent,
|
||||
public sensor::Sensor,
|
||||
public voltage_sampler::VoltageSampler,
|
||||
public Parented<MCP3008> {
|
||||
public:
|
||||
void set_reference_voltage(float reference_voltage) { this->reference_voltage_ = reference_voltage; }
|
||||
void set_pin(uint8_t pin) { this->pin_ = pin; }
|
||||
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
float sample() override;
|
||||
|
||||
protected:
|
||||
uint8_t pin_;
|
||||
float reference_voltage_;
|
||||
};
|
||||
|
||||
} // namespace mcp3008
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user