From b304d25336bc53c297ef08ce718f51d26cfa12f8 Mon Sep 17 00:00:00 2001 From: Masterz69 Date: Fri, 19 Apr 2024 13:25:25 +0300 Subject: [PATCH] Change component name to aj_sr04m --- .../{ultrasonic_uart => aj_sr04m}/__init__.py | 0 esphome/components/aj_sr04m/aj_sr04m.cpp | 32 +++++++++++++++++++ .../aj_sr04m.h} | 6 ++-- .../{ultrasonic_uart => aj_sr04m}/sensor.py | 16 +++++----- .../ultrasonic_uart/ultrasonic_sensor.cpp | 26 --------------- 5 files changed, 43 insertions(+), 37 deletions(-) rename esphome/components/{ultrasonic_uart => aj_sr04m}/__init__.py (100%) create mode 100644 esphome/components/aj_sr04m/aj_sr04m.cpp rename esphome/components/{ultrasonic_uart/ultrasonic_sensor.h => aj_sr04m/aj_sr04m.h} (66%) rename esphome/components/{ultrasonic_uart => aj_sr04m}/sensor.py (76%) delete mode 100644 esphome/components/ultrasonic_uart/ultrasonic_sensor.cpp diff --git a/esphome/components/ultrasonic_uart/__init__.py b/esphome/components/aj_sr04m/__init__.py similarity index 100% rename from esphome/components/ultrasonic_uart/__init__.py rename to esphome/components/aj_sr04m/__init__.py diff --git a/esphome/components/aj_sr04m/aj_sr04m.cpp b/esphome/components/aj_sr04m/aj_sr04m.cpp new file mode 100644 index 0000000000..cb44251f96 --- /dev/null +++ b/esphome/components/aj_sr04m/aj_sr04m.cpp @@ -0,0 +1,32 @@ +#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 diff --git a/esphome/components/ultrasonic_uart/ultrasonic_sensor.h b/esphome/components/aj_sr04m/aj_sr04m.h similarity index 66% rename from esphome/components/ultrasonic_uart/ultrasonic_sensor.h rename to esphome/components/aj_sr04m/aj_sr04m.h index 4d2610b078..81ff3f6798 100644 --- a/esphome/components/ultrasonic_uart/ultrasonic_sensor.h +++ b/esphome/components/aj_sr04m/aj_sr04m.h @@ -5,9 +5,9 @@ #include "esphome/components/uart/uart.h" namespace esphome { -namespace ultrasonic_uart { +namespace aj_sr04m { -class UltrasonicSensorUart : public sensor::Sensor, public PollingComponent, public uart::UARTDevice { +class Ajsr04mComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice { public: void setup() override; void dump_config() override; @@ -17,5 +17,5 @@ class UltrasonicSensorUart : public sensor::Sensor, public PollingComponent, pub float get_setup_priority() const override; }; -} // namespace ultrasonic_uart +} // namespace aj_sr04m } // namespace esphome diff --git a/esphome/components/ultrasonic_uart/sensor.py b/esphome/components/aj_sr04m/sensor.py similarity index 76% rename from esphome/components/ultrasonic_uart/sensor.py rename to esphome/components/aj_sr04m/sensor.py index 5ddb0ff71f..1218e80e0a 100644 --- a/esphome/components/ultrasonic_uart/sensor.py +++ b/esphome/components/aj_sr04m/sensor.py @@ -3,16 +3,16 @@ import esphome.config_validation as cv from esphome.components import sensor, uart from esphome.const import ( STATE_CLASS_MEASUREMENT, - UNIT_CENTIMETER, + UNIT_METER, ICON_ARROW_EXPAND_VERTICAL, DEVICE_CLASS_DISTANCE, ) DEPENDENCIES = ["uart"] -ultrasonic_uart_ns = cg.esphome_ns.namespace("ultrasonic_uart") -UltrasonicSensorUart = ultrasonic_uart_ns.class_( - "UltrasonicSensorUart", +aj_sr04m_ns = cg.esphome_ns.namespace("aj_sr04m") +Ajsr04mComponent = aj_sr04m_ns.class_( + "Ajsr04mComponent", sensor.Sensor, cg.PollingComponent, uart.UARTDevice, @@ -20,10 +20,10 @@ UltrasonicSensorUart = ultrasonic_uart_ns.class_( CONFIG_SCHEMA = ( sensor.sensor_schema( - UltrasonicSensorUart, - unit_of_measurement=UNIT_CENTIMETER, + Ajsr04mComponent, + unit_of_measurement=UNIT_METER, icon=ICON_ARROW_EXPAND_VERTICAL, - accuracy_decimals=2, + accuracy_decimals=3, state_class=STATE_CLASS_MEASUREMENT, device_class=DEVICE_CLASS_DISTANCE, ) @@ -32,7 +32,7 @@ CONFIG_SCHEMA = ( ) FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( - "ultrasonic_uart", + "aj_sr04m", baud_rate=9600, require_tx=True, require_rx=True, diff --git a/esphome/components/ultrasonic_uart/ultrasonic_sensor.cpp b/esphome/components/ultrasonic_uart/ultrasonic_sensor.cpp deleted file mode 100644 index 0a943b2934..0000000000 --- a/esphome/components/ultrasonic_uart/ultrasonic_sensor.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "ultrasonic_sensor.h" -#include "esphome/core/log.h" - -namespace esphome { -namespace ultrasonic_uart { - -static const char *const TAG = "ultrasonic_uart"; - -void UltrasonicSensorUart::setup() { ESP_LOGCONFIG(TAG, "Setting up Ultrasonic Sensor..."); } -void UltrasonicSensorUart::update() { - this->write(0x55); - while (this->available() == 4) { - auto frame = *this->read_array<4>(); - if ((frame[0] == 0xFF) && (((frame[0] + frame[1] + frame[2]) & 0x00FF) == frame[3])) { - float value = ((frame[1] << 8) + frame[2]) / 10.0; - this->publish_state(value); - } - } -} -void UltrasonicSensorUart::dump_config() { - LOG_SENSOR("", "Ultrasonic Sensor", this); - LOG_UPDATE_INTERVAL(this); -} -float UltrasonicSensorUart::get_setup_priority() const { return setup_priority::DATA; } -} // namespace ultrasonic_uart -} // namespace esphome