This commit is contained in:
Masterz69 2024-05-02 17:21:58 +12:00 committed by GitHub
commit 1542af3481
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 186 additions and 0 deletions

View File

@ -28,6 +28,7 @@ esphome/components/airthings_ble/* @jeromelaban
esphome/components/airthings_wave_base/* @jeromelaban @kpfleming @ncareau
esphome/components/airthings_wave_mini/* @ncareau
esphome/components/airthings_wave_plus/* @jeromelaban
esphome/components/aj_sr04m/* @Masterz69
esphome/components/alarm_control_panel/* @grahambrown11 @hwstar
esphome/components/alpha3/* @jan-hofmeier
esphome/components/am2315c/* @swoboda1337

View File

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

View File

@ -0,0 +1,31 @@
#include "aj_sr04m.h"
#include "esphome/core/log.h"
namespace esphome {
namespace aj_sr04m {
static const char *const TAG = "aj_sr04m.sensor";
void Ajsr04mComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up AJ_SR04M Sensor..."); }
void Ajsr04mComponent::update() {
this->write(0x55);
ESP_LOGV(TAG, "Request read out from sensor");
while (this->available() == 4) {
auto frame = *this->read_array<4>();
auto checksum = (frame[0] + frame[1] + frame[2]) & 0x00FF;
if ((frame[0] == 0xFF) && (checksum == frame[3])) {
float value = ((frame[1] << 8) + frame[2]) / 1000.0;
this->publish_state(value);
} else {
ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, frame[3]);
}
}
}
void Ajsr04mComponent::dump_config() {
LOG_SENSOR("", "AJ_SR04M Sensor", this);
LOG_UPDATE_INTERVAL(this);
}
float Ajsr04mComponent::get_setup_priority() const { return setup_priority::DATA; }
} // namespace aj_sr04m
} // namespace esphome

View File

@ -0,0 +1,21 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace aj_sr04m {
class Ajsr04mComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice {
public:
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override;
};
} // namespace aj_sr04m
} // namespace esphome

View File

@ -0,0 +1,48 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, uart
from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_METER,
ICON_ARROW_EXPAND_VERTICAL,
DEVICE_CLASS_DISTANCE,
)
DEPENDENCIES = ["uart"]
aj_sr04m_ns = cg.esphome_ns.namespace("aj_sr04m")
Ajsr04mComponent = aj_sr04m_ns.class_(
"Ajsr04mComponent",
sensor.Sensor,
cg.PollingComponent,
uart.UARTDevice,
)
CONFIG_SCHEMA = (
sensor.sensor_schema(
Ajsr04mComponent,
unit_of_measurement=UNIT_METER,
icon=ICON_ARROW_EXPAND_VERTICAL,
accuracy_decimals=3,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_DISTANCE,
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"aj_sr04m",
baud_rate=9600,
require_tx=True,
require_rx=True,
data_bits=8,
parity=None,
stop_bits=1,
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)

View File

@ -0,0 +1,14 @@
uart:
- id: aj_sr04m_uart
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: aj_sr04m
id: aj_sr04m_sensor
name: "aj_sr04m Distance"
uart_id: aj_sr04m_uart
update_interval: 2s

View File

@ -0,0 +1,14 @@
uart:
- id: aj_sr04m_uart
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: aj_sr04m
id: aj_sr04m_sensor
name: "aj_sr04m Distance"
uart_id: aj_sr04m_uart
update_interval: 2s

View File

@ -0,0 +1,14 @@
uart:
- id: aj_sr04m_uart
tx_pin:
number: 17
rx_pin:
number: 16
baud_rate: 9600
sensor:
- platform: aj_sr04m
id: aj_sr04m_sensor
name: "aj_sr04m Distance"
uart_id: aj_sr04m_uart
update_interval: 2s

View File

@ -0,0 +1,14 @@
uart:
- id: aj_sr04m_uart
tx_pin:
number: 17
rx_pin:
number: 16
baud_rate: 9600
sensor:
- platform: aj_sr04m
id: aj_sr04m_sensor
name: "aj_sr04m Distance"
uart_id: aj_sr04m_uart
update_interval: 2s

View File

@ -0,0 +1,14 @@
uart:
- id: aj_sr04m_uart
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: aj_sr04m
id: aj_sr04m_sensor
name: "aj_sr04m Distance"
uart_id: aj_sr04m_uart
update_interval: 2s

View File

@ -0,0 +1,14 @@
uart:
- id: aj_sr04m_uart
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: aj_sr04m
id: aj_sr04m_sensor
name: "aj_sr04m Distance"
uart_id: aj_sr04m_uart
update_interval: 2s