1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-14 06:38:17 +00:00

add model a02yytw (uart controlled)

This commit is contained in:
Alexandre Zia 2025-01-07 11:52:01 -05:00
parent 387bde665e
commit 4b0261fe71
No known key found for this signature in database
12 changed files with 137 additions and 99 deletions

View File

@ -12,7 +12,7 @@ esphome/core/* @esphome/core
# Integrations
esphome/components/a01nyub/* @MrSuicideParrot
esphome/components/a02yyuw/* @TH-Braemer
esphome/components/a02yyuw/* @TH-Braemer @alexandrezia
esphome/components/absolute_humidity/* @DAVe3283
esphome/components/ac_dimmer/* @glmnet
esphome/components/adc/* @esphome/core

View File

@ -1 +1 @@
CODEOWNERS = ["@TH-Braemer"]
CODEOWNERS = ["@TH-Braemer", "@alexandrezia"]

View File

@ -1,4 +1,4 @@
// Datasheet https://wiki.dfrobot.com/_A02YYUW_Waterproof_Ultrasonic_Sensor_SKU_SEN0311
// Datasheet https://www.dypcn.com/uploads/A02-Datasheet.pdf
#include "a02yyuw.h"
#include "esphome/core/helpers.h"
@ -9,6 +9,13 @@ namespace a02yyuw {
static const char *const TAG = "a02yyuw.sensor";
void A02yyuwComponent::update() {
if (this->model_ == A02YYTW) {
this->write_byte(0xFF);
ESP_LOGV(TAG, "Request read out from sensor");
}
}
void A02yyuwComponent::loop() {
uint8_t data;
while (this->available() > 0) {
@ -37,7 +44,17 @@ void A02yyuwComponent::check_buffer_() {
this->buffer_.clear();
}
void A02yyuwComponent::dump_config() { LOG_SENSOR("", "A02yyuw Sensor", this); }
void A02yyuwComponent::dump_config() {
switch (this->model_) {
case A02YYUW:
ESP_LOGCONFIG(TAG, " sensor model: a02yyuw");
break;
case A02YYTW:
ESP_LOGCONFIG(TAG, " sensor model: a02yytw");
LOG_UPDATE_INTERVAL(this);
break;
}
}
} // namespace a02yyuw
} // namespace esphome

View File

@ -9,16 +9,23 @@
namespace esphome {
namespace a02yyuw {
class A02yyuwComponent : public sensor::Sensor, public Component, public uart::UARTDevice {
enum Model {
A02YYUW,
A02YYTW,
};
class A02yyuwComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice {
public:
// Nothing really public.
void set_model(Model model) { this->model_ = model; }
// ========== INTERNAL METHODS ==========
void update() override;
void loop() override;
void dump_config() override;
protected:
void check_buffer_();
Model model_;
std::vector<uint8_t> buffer_;
};

View File

@ -1,41 +1,71 @@
import esphome.codegen as cg
from esphome.components import sensor, uart
import esphome.config_validation as cv
from esphome.const import (
STATE_CLASS_MEASUREMENT,
ICON_ARROW_EXPAND_VERTICAL,
CONF_MODEL,
CONF_UPDATE_INTERVAL,
DEVICE_CLASS_DISTANCE,
ICON_ARROW_EXPAND_VERTICAL,
STATE_CLASS_MEASUREMENT,
UNIT_MILLIMETER,
)
CODEOWNERS = ["@TH-Braemer"]
CODEOWNERS = ["@TH-Braemer", "@alexandrezia"]
DEPENDENCIES = ["uart"]
a02yyuw_ns = cg.esphome_ns.namespace("a02yyuw")
A02yyuwComponent = a02yyuw_ns.class_(
"A02yyuwComponent", sensor.Sensor, cg.Component, uart.UARTDevice
"A02yyuwComponent", sensor.Sensor, cg.PollingComponent, uart.UARTDevice
)
Model = a02yyuw_ns.enum("Model")
MODEL = {
"a02yyuw": Model.A02YYUW,
"a02yytw": Model.A02YYTW,
}
CONFIG_SCHEMA = (
sensor.sensor_schema(
A02yyuwComponent,
unit_of_measurement=UNIT_MILLIMETER,
icon=ICON_ARROW_EXPAND_VERTICAL,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_DISTANCE,
)
.extend(cv.polling_component_schema("100ms"))
.extend(uart.UART_DEVICE_SCHEMA)
.extend(
{
cv.Optional(CONF_MODEL, default="a02yyuw"): cv.enum(MODEL, upper=False),
}
)
)
CONFIG_SCHEMA = sensor.sensor_schema(
A02yyuwComponent,
unit_of_measurement=UNIT_MILLIMETER,
icon=ICON_ARROW_EXPAND_VERTICAL,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_DISTANCE,
).extend(uart.UART_DEVICE_SCHEMA)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"a02yyuw",
baud_rate=9600,
require_tx=False,
require_rx=True,
data_bits=8,
parity=None,
stop_bits=1,
)
def final_validation(config):
require_tx = True
if config[CONF_MODEL] == "a02yyuw":
config.pop(CONF_UPDATE_INTERVAL, None)
require_tx = False
schema = uart.final_validate_device_schema(
"a02yyuw",
baud_rate=9600,
require_tx=require_tx,
require_rx=True,
data_bits=8,
parity=None,
stop_bits=1,
)
schema(config)
FINAL_VALIDATE_SCHEMA = final_validation
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
cg.add(var.set_model(config[CONF_MODEL]))
if CONF_UPDATE_INTERVAL in config:
cg.add(var.set_update_interval(config[CONF_UPDATE_INTERVAL]))

View File

@ -0,0 +1,31 @@
uart:
- id: uart_a02yyuw
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
baud_rate: 9600
sensor:
- platform: a02yyuw
id: a02yyuw_sensor
name: a02yyuw Distance
uart_id: uart_a02yyuw
- platform: a02yyuw
model: a02yyuw
id: a02yyuw_sensor_model
name: a02yyuw Distance Model
uart_id: uart_a02yyuw
- platform: a02yyuw
model: a02yytw
id: a02yytw_sensor_model
name: a02yytw Distance Model
uart_id: uart_a02yyuw
- platform: a02yyuw
model: a02yytw
update_interval: 500ms
id: a02yytw_sensor_model_interval
name: a02yyuw Distance Model Interval
uart_id: uart_a02yyuw

View File

@ -1,13 +1,5 @@
uart:
- id: uart_a02yyuw
tx_pin:
number: 17
rx_pin:
number: 16
baud_rate: 9600
substitutions:
tx_pin: GPIO17
rx_pin: GPIO16
sensor:
- platform: a02yyuw
id: a02yyuw_sensor
name: a02yyuw Distance
uart_id: uart_a02yyuw
<<: !include common.yaml

View File

@ -1,13 +1,5 @@
uart:
- id: uart_a02yyuw
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
substitutions:
tx_pin: GPIO4
rx_pin: GPIO5
sensor:
- platform: a02yyuw
id: a02yyuw_sensor
name: a02yyuw Distance
uart_id: uart_a02yyuw
<<: !include common.yaml

View File

@ -1,13 +1,5 @@
uart:
- id: uart_a02yyuw
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
substitutions:
tx_pin: GPIO4
rx_pin: GPIO5
sensor:
- platform: a02yyuw
id: a02yyuw_sensor
name: a02yyuw Distance
uart_id: uart_a02yyuw
<<: !include common.yaml

View File

@ -1,13 +1,6 @@
uart:
- id: uart_a02yyuw
tx_pin:
number: 17
rx_pin:
number: 16
baud_rate: 9600
substitutions:
tx_pin: GPIO17
rx_pin: GPIO16
<<: !include common.yaml
sensor:
- platform: a02yyuw
id: a02yyuw_sensor
name: a02yyuw Distance
uart_id: uart_a02yyuw

View File

@ -1,13 +1,5 @@
uart:
- id: uart_a02yyuw
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
substitutions:
tx_pin: GPIO4
rx_pin: GPIO5
sensor:
- platform: a02yyuw
id: a02yyuw_sensor
name: a02yyuw Distance
uart_id: uart_a02yyuw
<<: !include common.yaml

View File

@ -1,13 +1,5 @@
uart:
- id: uart_a02yyuw
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
substitutions:
tx_pin: GPIO4
rx_pin: GPIO5
sensor:
- platform: a02yyuw
id: a02yyuw_sensor
name: a02yyuw Distance
uart_id: uart_a02yyuw
<<: !include common.yaml