ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
esphome::modbus_client::ClientActionBase< Ts > Class Template Reference

Shared base for the modbus_client actions. More...

#include <modbus_client.h>

Inheritance diagram for esphome::modbus_client::ClientActionBase< Ts >:
esphome::Action< Ts... > esphome::modbus::ModbusClientDevice

Public Types

using retry_func_t = bool (*)(std::span<const uint8_t>)
 The retry decision for on_no_response: given the request PDU, return true to have the hub re-queue the frame.
 

Public Member Functions

 TEMPLATABLE_VALUE (uint8_t, target_address) Trigger< std
 
Trigger< std::span< const uint8_t >, modbus::ExceptionCode > * get_error_trigger ()
 
Trigger< std::span< const uint8_t > > * get_no_response_trigger ()
 
Trigger< std::span< const uint8_t > > * get_not_sent_trigger ()
 
void set_retry (retry_func_t f)
 
void on_sent (std::span< const uint8_t > request_pdu) override
 The frame was written to the wire: fires once per transmission, before any reply, and never for a send that ended in on_not_sent.
 
void on_not_sent (std::span< const uint8_t > request_pdu) override
 Never reached the wire, from either of two sources.
 
void on_error (std::span< const uint8_t > request_pdu, modbus::ExceptionCode exception_code) override
 A Modbus exception reply.
 
bool on_no_response (std::span< const uint8_t > request_pdu) override
 No reply within send_wait_time.
 
void play_complex (const Ts &...x) override
 Stamp the templated device address before every play(): subclasses cannot forget it, and the hub routes each reply by device pointer, so a changed address never mis-routes earlier replies.
 
- Public Member Functions inherited from esphome::Action< Ts... >
virtual void stop_complex ()
 
virtual bool is_running ()
 Check if this or any of the following actions are currently running.
 
int num_running_total ()
 The total number of actions that are currently running in this plus any of the following actions in the chain.
 
- Public Member Functions inherited from esphome::modbus::ModbusClientDevice
 ModbusClientDevice ()=default
 
 ModbusClientDevice (ModbusClientHub *parent, uint8_t address)
 
virtual ~ModbusClientDevice ()
 
 ModbusClientDevice (const ModbusClientDevice &)=delete
 
ModbusClientDeviceoperator= (const ModbusClientDevice &)=delete
 
 ModbusClientDevice (ModbusClientDevice &&)=delete
 
ModbusClientDeviceoperator= (ModbusClientDevice &&)=delete
 
void set_parent (ModbusClientHub *parent)
 
void set_address (uint8_t address)
 
virtual void on_response (std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu)
 Low-level response hook: called with the request PDU this device sent and the response PDU received The spans are only valid for the duration of the call - copy the bytes if they must outlive it.
 
 ESPDEPRECATED ("Override on_not_sent() instead. Removed in 2027.2.0", "2026.8.0") virtual void on_modbus_not_sent()
 
 ESPDEPRECATED ("Override on_no_response() instead. Removed in 2027.2.0", "2026.8.0") virtual bool on_modbus_no_response()
 
virtual void on_read_registers (EntityType entity_type, uint16_t start_address, std::span< const uint16_t > registers, ResponseStatus status)
 High-level typed response callbacks, fired by the default on_response()/on_error() with arguments parsed from the request and response PDUs.
 
virtual void on_read_holding_registers (uint16_t start_address, std::span< const uint16_t > registers, ResponseStatus status)
 
virtual void on_read_input_registers (uint16_t start_address, std::span< const uint16_t > registers, ResponseStatus status)
 
virtual void on_read_bits (EntityType entity_type, uint16_t start_address, PackedBits bits, ResponseStatus status)
 Coil/discrete-input reads are delivered as a PackedBits view (bit 0 = the bit at start_address, bits.size() = the count requested).
 
virtual void on_read_coils (uint16_t start_address, PackedBits bits, ResponseStatus status)
 
virtual void on_read_discrete_inputs (uint16_t start_address, PackedBits bits, ResponseStatus status)
 
virtual void on_write_single_register (uint16_t address, uint16_t value, ResponseStatus status)
 Write acknowledgements.
 
virtual void on_write_single_coil (uint16_t address, bool value, ResponseStatus status)
 
virtual void on_write_multiple_registers (uint16_t start_address, std::span< const uint16_t > registers, ResponseStatus status)
 
virtual void on_write_multiple_coils (uint16_t start_address, PackedBits bits, ResponseStatus status)
 
virtual void on_custom_response (std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu, ResponseStatus status)
 Catch-all for custom function codes and anything that is not a standard-conformant transaction (see dispatch_response_()); on failure the response is empty and the exception code is in status.
 
 ESPDEPRECATED ("Use the typed read_*/write_* helpers or queue_pdu() instead. Removed in 2027.2.0", "2026.8.0") void send(uint8_t function
 

Protected Member Functions

void send_or_resolve_ (std::span< const uint8_t > pdu)
 The hub refuses some sends at the door with no callback (a duplicate write already pending, a full queue, or an empty PDU - which is how the create_*_pdu() builders reject out-of-spec input).
 
- Protected Member Functions inherited from esphome::Action< Ts... >
virtual void play (const Ts &...x)=0
 
void play_next_ (const Ts &...x)
 
void play_next_tuple_ (const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
 
void play_next_tuple_ (const std::tuple< Ts... > &tuple)
 
virtual void stop ()
 
void stop_next_ ()
 
bool is_running_next_ ()
 

Protected Attributes

Trigger< std::span< const uint8_t > > sent_trigger_
 
Trigger< std::span< const uint8_t >, modbus::ExceptionCodeerror_trigger_
 
Trigger< std::span< const uint8_t > > no_response_trigger_
 
Trigger< std::span< const uint8_t > > not_sent_trigger_
 
retry_func_t retry_func_ {nullptr}
 
- Protected Attributes inherited from esphome::Action< Ts... >
friend ActionList< Ts... >
 
Action< Ts... > * next_
 
int num_running_
 The number of instances of this sequence in the list of actions that is currently being executed.
 

Additional Inherited Members

- Data Fields inherited from esphome::modbus::ModbusClientDevice
uint16_t start_address
 
uint16_t uint16_t number_of_entities
 
uint16_t uint16_t uint8_t payload_len = 0
 
uint16_t uint16_t uint8_t const uint8_t * payload
 
ModbusClientHubparent_ {nullptr}
 
uint8_t address_ {0}
 
bool custom_response_warned_ {false}
 

Detailed Description

template<typename... Ts>
class esphome::modbus_client::ClientActionBase< Ts >

Shared base for the modbus_client actions.

Each ACTION INSTANCE is its own modbus::ModbusClientDevice: the hub routes every reply (or its lack) straight back to the action that sent it, so there is no central client object and no request matching. The device address is templatable; it is stamped on the device at play() time; the hub routes each reply by device pointer, so a changed address never mis-routes an earlier reply. (The address is not passed to the reply triggers - under overlapping sends it could misreport, and the handler can recompute the expression it configured.)

Definition at line 18 of file modbus_client.h.

Member Typedef Documentation

◆ retry_func_t

template<typename... Ts>
using esphome::modbus_client::ClientActionBase< Ts >::retry_func_t = bool (*)(std::span<const uint8_t>)

The retry decision for on_no_response: given the request PDU, return true to have the hub re-queue the frame.

Set from the lambda form or a then: automation's nested retry lambda; may coexist with the no_response trigger (actions run, then this decides the retry).

Definition at line 30 of file modbus_client.h.

Member Function Documentation

◆ get_error_trigger()

template<typename... Ts>
Trigger< std::span< const uint8_t >, modbus::ExceptionCode > * esphome::modbus_client::ClientActionBase< Ts >::get_error_trigger ( )
inline

Definition at line 23 of file modbus_client.h.

◆ get_no_response_trigger()

template<typename... Ts>
Trigger< std::span< const uint8_t > > * esphome::modbus_client::ClientActionBase< Ts >::get_no_response_trigger ( )
inline

Definition at line 24 of file modbus_client.h.

◆ get_not_sent_trigger()

template<typename... Ts>
Trigger< std::span< const uint8_t > > * esphome::modbus_client::ClientActionBase< Ts >::get_not_sent_trigger ( )
inline

Definition at line 25 of file modbus_client.h.

◆ on_error()

template<typename... Ts>
void esphome::modbus_client::ClientActionBase< Ts >::on_error ( std::span< const uint8_t > request_pdu,
modbus::ExceptionCode exception_code )
inlineoverridevirtual

A Modbus exception reply.

Lives here beside its trigger so every action subclass gets the pairing: register_client_action() wires on_error for all of them, so a derived class must not have to remember the override.

Reimplemented from esphome::modbus::ModbusClientDevice.

Definition at line 45 of file modbus_client.h.

◆ on_no_response()

template<typename... Ts>
bool esphome::modbus_client::ClientActionBase< Ts >::on_no_response ( std::span< const uint8_t > request_pdu)
inlineoverridevirtual

No reply within send_wait_time.

Run the on_no_response actions (empty in the pure-lambda form), then let the retry lambda, if set, decide whether the hub re-queues the frame (true = retry). The two coexist: a then: automation can also carry a retry lambda. No lambda = no retry.

Reimplemented from esphome::modbus::ModbusClientDevice.

Definition at line 51 of file modbus_client.h.

◆ on_not_sent()

template<typename... Ts>
void esphome::modbus_client::ClientActionBase< Ts >::on_not_sent ( std::span< const uint8_t > request_pdu)
inlineoverridevirtual

Never reached the wire, from either of two sources.

The hub calls this for a request it accepted and then dropped, which happens only when clear_tx_queue_for_address() retires it - a modbus device going offline, say. Everything the hub refuses at the door instead returns false from queue_pdu() with no callback at all, so send_or_resolve_() below turns those into this same callback: a full queue, a duplicate write, or an empty PDU from a rejecting builder.

Reimplemented from esphome::modbus::ModbusClientDevice.

Definition at line 41 of file modbus_client.h.

◆ on_sent()

template<typename... Ts>
void esphome::modbus_client::ClientActionBase< Ts >::on_sent ( std::span< const uint8_t > request_pdu)
inlineoverridevirtual

The frame was written to the wire: fires once per transmission, before any reply, and never for a send that ended in on_not_sent.

request_pdu is the PDU sent (function code + data).

Reimplemented from esphome::modbus::ModbusClientDevice.

Definition at line 35 of file modbus_client.h.

◆ play_complex()

template<typename... Ts>
void esphome::modbus_client::ClientActionBase< Ts >::play_complex ( const Ts &... x)
inlineoverridevirtual

Stamp the templated device address before every play(): subclasses cannot forget it, and the hub routes each reply by device pointer, so a changed address never mis-routes earlier replies.

Reimplemented from esphome::Action< Ts... >.

Definition at line 59 of file modbus_client.h.

◆ send_or_resolve_()

template<typename... Ts>
void esphome::modbus_client::ClientActionBase< Ts >::send_or_resolve_ ( std::span< const uint8_t > pdu)
inlineprotected

The hub refuses some sends at the door with no callback (a duplicate write already pending, a full queue, or an empty PDU - which is how the create_*_pdu() builders reject out-of-spec input).

Every send still gets exactly one outcome (a broadcast (address 0) is the exception - never answered, it resolves through on_sent() alone), so resolve refusals here via on_not_sent. Takes a span, not a PduBuffer: the builders return right-sized buffers (a read PDU is 5 bytes), and a PduBuffer parameter would widen each one to the 253-byte maximum just to cross the call.

Definition at line 71 of file modbus_client.h.

◆ set_retry()

template<typename... Ts>
void esphome::modbus_client::ClientActionBase< Ts >::set_retry ( retry_func_t f)
inline

Definition at line 31 of file modbus_client.h.

◆ TEMPLATABLE_VALUE()

template<typename... Ts>
esphome::modbus_client::ClientActionBase< Ts >::TEMPLATABLE_VALUE ( uint8_t ,
target_address  )
inline

Definition at line 20 of file modbus_client.h.

Field Documentation

◆ error_trigger_

template<typename... Ts>
Trigger<std::span<const uint8_t>, modbus::ExceptionCode> esphome::modbus_client::ClientActionBase< Ts >::error_trigger_
protected

Definition at line 77 of file modbus_client.h.

◆ no_response_trigger_

template<typename... Ts>
Trigger<std::span<const uint8_t> > esphome::modbus_client::ClientActionBase< Ts >::no_response_trigger_
protected

Definition at line 78 of file modbus_client.h.

◆ not_sent_trigger_

template<typename... Ts>
Trigger<std::span<const uint8_t> > esphome::modbus_client::ClientActionBase< Ts >::not_sent_trigger_
protected

Definition at line 79 of file modbus_client.h.

◆ retry_func_

template<typename... Ts>
retry_func_t esphome::modbus_client::ClientActionBase< Ts >::retry_func_ {nullptr}
protected

Definition at line 80 of file modbus_client.h.

◆ sent_trigger_

template<typename... Ts>
Trigger<std::span<const uint8_t> > esphome::modbus_client::ClientActionBase< Ts >::sent_trigger_
protected

Definition at line 76 of file modbus_client.h.


The documentation for this class was generated from the following file: