Add Zio Ultrasonic Distance Sensor Component (#5059)

This commit is contained in:
kahrendt 2023-07-11 00:24:18 -04:00 committed by GitHub
parent 98fd092053
commit a391815921
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 0 deletions

View File

@ -321,3 +321,4 @@ esphome/components/xiaomi_mhoc401/* @vevsvevs
esphome/components/xiaomi_rtcgq02lm/* @jesserockz
esphome/components/xl9535/* @mreditor97
esphome/components/xpt2046/* @nielsnl68 @numo68
esphome/components/zio_ultrasonic/* @kahrendt

View File

@ -0,0 +1,34 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
DEVICE_CLASS_DISTANCE,
STATE_CLASS_MEASUREMENT,
)
DEPENDENCIES = ["i2c"]
CODEOWNERS = ["@kahrendt"]
zio_ultrasonic_ns = cg.esphome_ns.namespace("zio_ultrasonic")
ZioUltrasonicComponent = zio_ultrasonic_ns.class_(
"ZioUltrasonicComponent", cg.PollingComponent, i2c.I2CDevice, sensor.Sensor
)
CONFIG_SCHEMA = (
sensor.sensor_schema(
ZioUltrasonicComponent,
unit_of_measurement="mm",
accuracy_decimals=0,
device_class=DEVICE_CLASS_DISTANCE,
state_class=STATE_CLASS_MEASUREMENT,
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x00))
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)

View File

@ -0,0 +1,31 @@
#include "zio_ultrasonic.h"
#include "esphome/core/log.h"
namespace esphome {
namespace zio_ultrasonic {
static const char *const TAG = "zio_ultrasonic";
void ZioUltrasonicComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Zio Ultrasonic Sensor:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Sensor:", this);
}
void ZioUltrasonicComponent::update() {
uint16_t distance;
// Read an unsigned two byte integerfrom register 0x01 which gives distance in mm
if (!this->read_byte_16(0x01, &distance)) {
ESP_LOGE(TAG, "Error reading data from Zio Ultrasonic");
this->publish_state(NAN);
} else {
this->publish_state(distance);
}
}
} // namespace zio_ultrasonic
} // namespace esphome

View File

@ -0,0 +1,22 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
static const char *const TAG = "Zio Ultrasonic";
namespace esphome {
namespace zio_ultrasonic {
class ZioUltrasonicComponent : public i2c::I2CDevice, public PollingComponent, public sensor::Sensor {
public:
float get_setup_priority() const override { return setup_priority::DATA; }
void dump_config() override;
void update() override;
};
} // namespace zio_ultrasonic
} // namespace esphome

View File

@ -1310,6 +1310,10 @@ sensor:
fault_count: 1
polarity: active_high
function: comparator
- platform: zio_ultrasonic
name: "Distance"
update_interval: 60s
i2c_id: i2c_bus
esp32_touch:
setup_mode: false