[uart_tcp] Expose the connection entities as platforms (#21008)

This commit is contained in:
J. Nick Koston
2026-10-17 15:09:32 -10:00
committed by GitHub
parent 42e849967a
commit e034662dc6
10 changed files with 105 additions and 24 deletions
+3 -20
View File
@@ -1,8 +1,7 @@
import esphome.codegen as cg
from esphome.components import binary_sensor, sensor, socket, uart
from esphome.components import socket, uart
from esphome.components.const import (
CONF_ALLOWED_IPS,
CONF_CONNECTED,
CONF_HOST,
CONF_RECONNECT_INTERVAL,
CONF_ROLE,
@@ -15,9 +14,6 @@ from esphome.const import (
CONF_ID,
CONF_PORT,
CONF_UART_ID,
DEVICE_CLASS_CONNECTIVITY,
ENTITY_CATEGORY_DIAGNOSTIC,
STATE_CLASS_TOTAL_INCREASING,
)
from esphome.core import CORE
import esphome.final_validate as fv
@@ -26,10 +22,10 @@ from esphome.types import ConfigType
CODEOWNERS = ["@Bascht74"]
DOMAIN = "uart_tcp"
DEPENDENCIES = ["network", "uart"]
AUTO_LOAD = ["binary_sensor", "sensor", "socket"]
AUTO_LOAD = ["socket"]
MULTI_CONF = True
CONF_DISCONNECTS = "disconnects"
CONF_UART_TCP_ID = "uart_tcp_id"
uart_tcp_ns = cg.esphome_ns.namespace("uart_tcp")
UartTcp = uart_tcp_ns.class_("UartTcp", cg.Component, uart.UARTDevice)
@@ -43,15 +39,6 @@ BASE_SCHEMA = cv.Schema(
cv.Optional(
CONF_RECONNECT_INTERVAL, default="5s"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_CONNECTED): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_CONNECTIVITY,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_DISCONNECTS): sensor.sensor_schema(
accuracy_decimals=0,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
).extend(cv.COMPONENT_SCHEMA)
@@ -144,7 +131,3 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_reconnect_interval(config[CONF_RECONNECT_INTERVAL]))
if (host := config.get(CONF_HOST)) is not None:
cg.add(var.set_host(host))
binary_sensors = binary_sensor.sub_binary_sensors(config)
await binary_sensors(CONF_CONNECTED, var.set_connected_sensor)
sensors = sensor.sub_sensors(config)
await sensors(CONF_DISCONNECTS, var.set_disconnects_sensor)
@@ -0,0 +1,25 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
from esphome.components.const import CONF_CONNECTED
import esphome.config_validation as cv
from esphome.const import CONF_ID, DEVICE_CLASS_CONNECTIVITY, ENTITY_CATEGORY_DIAGNOSTIC
from esphome.types import ConfigType
from . import CONF_UART_TCP_ID, UartTcp
DEPENDENCIES = ["uart_tcp"]
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_UART_TCP_ID): cv.use_id(UartTcp),
cv.Optional(CONF_CONNECTED): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_CONNECTIVITY,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_UART_TCP_ID])
binary_sensors = binary_sensor.sub_binary_sensors(config)
await binary_sensors(CONF_CONNECTED, hub.set_connected_sensor)
+31
View File
@@ -0,0 +1,31 @@
import esphome.codegen as cg
from esphome.components import sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
ENTITY_CATEGORY_DIAGNOSTIC,
STATE_CLASS_TOTAL_INCREASING,
)
from esphome.types import ConfigType
from . import CONF_UART_TCP_ID, UartTcp
DEPENDENCIES = ["uart_tcp"]
CONF_DISCONNECTS = "disconnects"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_UART_TCP_ID): cv.use_id(UartTcp),
cv.Optional(CONF_DISCONNECTS): sensor.sensor_schema(
accuracy_decimals=0,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_UART_TCP_ID])
sensors = sensor.sub_sensors(config)
await sensors(CONF_DISCONNECTS, hub.set_disconnects_sensor)
+6
View File
@@ -16,9 +16,11 @@ void UartTcp::setup() {
#ifdef USE_SOCKET_TCP_LISTENER
this->listener_.begin(TAG);
#endif
#ifdef USE_BINARY_SENSOR
if (this->connected_sensor_ != nullptr) {
this->connected_sensor_->publish_state(false);
}
#endif
#ifdef USE_SENSOR
if (this->disconnects_sensor_ != nullptr) {
this->disconnects_sensor_->publish_state(0);
@@ -37,7 +39,9 @@ void UartTcp::dump_config() {
#ifdef USE_SOCKET_TCP_LISTENER
this->listener_.dump_config();
#endif
#ifdef USE_BINARY_SENSOR
LOG_BINARY_SENSOR(" ", "Connected", this->connected_sensor_);
#endif
#ifdef USE_SENSOR
LOG_SENSOR(" ", "Disconnects", this->disconnects_sensor_);
#endif
@@ -57,9 +61,11 @@ void UartTcp::sync_link_() {
// The driver kept whatever arrived while the link was down.
this->discard_uart_();
}
#ifdef USE_BINARY_SENSOR
if (this->connected_sensor_ != nullptr) {
this->connected_sensor_->publish_state(up);
}
#endif
#ifdef USE_SENSOR
// Only edges get here, so down is the falling edge.
if (!up && this->disconnects_sensor_ != nullptr) {
+7
View File
@@ -1,6 +1,9 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
@@ -22,7 +25,9 @@ class UartTcp final : public Component, public uart::UARTDevice {
void set_host(const char *host) { this->link_.set_host(host); }
void set_port(uint16_t port) { this->link_.set_port(port); }
void set_reconnect_interval(uint32_t ms) { this->link_.set_reconnect_interval(ms); }
#ifdef USE_BINARY_SENSOR
void set_connected_sensor(binary_sensor::BinarySensor *sensor) { this->connected_sensor_ = sensor; }
#endif
#ifdef USE_SENSOR
void set_disconnects_sensor(sensor::Sensor *sensor) { this->disconnects_sensor_ = sensor; }
#endif
@@ -53,7 +58,9 @@ class UartTcp final : public Component, public uart::UARTDevice {
#ifdef USE_SOCKET_TCP_LISTENER
socket::TcpListener listener_;
#endif
#ifdef USE_BINARY_SENSOR
binary_sensor::BinarySensor *connected_sensor_{nullptr};
#endif
// Loop start time of the last socket-to-UART write; sizes the next paced write.
uint32_t last_write_ms_{0};
#ifdef USE_SENSOR
+3 -2
View File
@@ -1,3 +1,4 @@
# The disconnects counter is compiled only with USE_SENSOR; declaring the
# sensor domain makes the C++ unit test build define it.
# The connection entities are compiled only with USE_BINARY_SENSOR and
# USE_SENSOR; declaring the domains makes the C++ unit test build define them.
binary_sensor:
sensor:
+8
View File
@@ -10,7 +10,15 @@ uart_tcp:
allowed_ips:
- 192.168.1.10
- 192.168.1.0/24
binary_sensor:
- platform: uart_tcp
uart_tcp_id: bridge
connected:
name: UART TCP Connected
sensor:
- platform: uart_tcp
uart_tcp_id: bridge
disconnects:
name: UART TCP Disconnects
@@ -12,7 +12,15 @@ uart_tcp:
host: 192.0.2.20
port: 502
reconnect_interval: 10s
binary_sensor:
- platform: uart_tcp
uart_tcp_id: bridge
connected:
name: UART TCP Connected
sensor:
- platform: uart_tcp
uart_tcp_id: bridge
disconnects:
name: UART TCP Disconnects
+8
View File
@@ -8,7 +8,15 @@ uart_tcp:
uart_id: uart_bus
host: 127.0.0.1
port: 44502
binary_sensor:
- platform: uart_tcp
uart_tcp_id: bridge
connected:
name: UART TCP Connected
sensor:
- platform: uart_tcp
uart_tcp_id: bridge
disconnects:
name: UART TCP Disconnects
@@ -24,11 +24,15 @@ uart_tcp:
port: 18126
allowed_ips:
- 127.0.0.1
connected:
name: Bridge Connected
- id: denied_bridge
uart_id: denied_bus
role: server
port: 18127
allowed_ips:
- 192.0.2.1
binary_sensor:
- platform: uart_tcp
uart_tcp_id: bridge
connected:
name: Bridge Connected