mirror of
https://github.com/esphome/esphome.git
synced 2024-12-31 18:07:48 +01:00
Created CO2 sensor
This commit is contained in:
parent
07fb218412
commit
d779f1e929
49
esphome/components/duco/sensor/__init__.py
Normal file
49
esphome/components/duco/sensor/__init__.py
Normal file
@ -0,0 +1,49 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import sensor
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_CO2,
|
||||
CONF_ID,
|
||||
DEVICE_CLASS_CARBON_DIOXIDE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_PARTS_PER_MILLION,
|
||||
)
|
||||
|
||||
from .. import CONF_DUCO_ID, DUCO_COMPONENT_SCHEMA
|
||||
|
||||
DEPENDENCIES = ["duco"]
|
||||
CODEOWNERS = ["@kokx"]
|
||||
|
||||
duco_ns = cg.esphome_ns.namespace("duco")
|
||||
DucoCo2Sensor = duco_ns.class_("DucoCo2Sensor", cg.PollingComponent, sensor.Sensor)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_CO2): cv.ensure_list(
|
||||
sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_PARTS_PER_MILLION,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_CARBON_DIOXIDE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(DucoCo2Sensor),
|
||||
cv.Required(CONF_ADDRESS): cv.int_range(0, 68),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
)
|
||||
}
|
||||
).extend(DUCO_COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
parent = await cg.get_variable(config[CONF_DUCO_ID])
|
||||
for co2_sensor_config in config[CONF_CO2]:
|
||||
sensvar = cg.new_Pvariable(co2_sensor_config[CONF_ID])
|
||||
await cg.register_component(sensvar, co2_sensor_config)
|
||||
await sensor.register_sensor(sensvar, co2_sensor_config)
|
||||
cg.add(sensvar.set_parent(parent))
|
||||
cg.add(sensvar.set_address(co2_sensor_config[CONF_ADDRESS]))
|
37
esphome/components/duco/sensor/sensor.cpp
Normal file
37
esphome/components/duco/sensor/sensor.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include "sensor.h"
|
||||
#include "../duco.h"
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace duco {
|
||||
|
||||
static const char *const TAG = "duco sensor";
|
||||
|
||||
void DucoCo2Sensor::setup() {}
|
||||
|
||||
void DucoCo2Sensor::update() {
|
||||
// ask for current mode
|
||||
ESP_LOGD(TAG, "CO2: Get sensor information");
|
||||
|
||||
std::vector<uint8_t> message = {0x01, address_, 0x00, 0x49, 0x04};
|
||||
this->parent_->send(0x10, message, this);
|
||||
}
|
||||
|
||||
float DucoCo2Sensor::get_setup_priority() const {
|
||||
// After DUCO
|
||||
return setup_priority::BUS - 2.0f;
|
||||
}
|
||||
|
||||
void DucoCo2Sensor::receive_response(std::vector<uint8_t> message) {
|
||||
if (message[1] == 0x12) {
|
||||
uint16_t co2_value = (message[8] << 8) + message[7];
|
||||
publish_state(co2_value);
|
||||
|
||||
this->parent_->stop_waiting(message[2]);
|
||||
}
|
||||
}
|
||||
|
||||
void DucoCo2Sensor::set_address(uint8_t address) { this->address_ = address; }
|
||||
|
||||
} // namespace duco
|
||||
} // namespace esphome
|
27
esphome/components/duco/sensor/sensor.h
Normal file
27
esphome/components/duco/sensor/sensor.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "../duco.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace duco {
|
||||
|
||||
class DucoCo2Sensor : public DucoDevice, public PollingComponent, public sensor::Sensor {
|
||||
public:
|
||||
void setup() override;
|
||||
void update() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
void receive_response(std::vector<uint8_t> message) override;
|
||||
|
||||
void set_address(uint8_t address);
|
||||
|
||||
protected:
|
||||
uint8_t address_;
|
||||
};
|
||||
|
||||
} // namespace duco
|
||||
} // namespace esphome
|
Loading…
Reference in New Issue
Block a user