ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
esphome::modbus_controller::SensorItem Class Referenceabstract

#include <modbus_controller.h>

Inheritance diagram for esphome::modbus_controller::SensorItem:
esphome::modbus_controller::ModbusBinaryOutput esphome::modbus_controller::ModbusBinarySensor esphome::modbus_controller::ModbusFloatOutput esphome::modbus_controller::ModbusNumber esphome::modbus_controller::ModbusSelect esphome::modbus_controller::ModbusSensor esphome::modbus_controller::ModbusSwitch esphome::modbus_controller::ModbusTextSensor

Public Member Functions

virtual void parse_and_publish (std::span< const uint8_t > data)=0
 Parse this sensor's slice out of its range's response and publish it.
 
bool addresses_bits () const
 Coils and discrete inputs address individual bits; every other type addresses 16-bit registers.
 
uint16_t write_address () const
 Address a write entity (switch/number/select) targets, derived from its resolved position within the range so that a write lands on the register the sensor reads from.
 
void set_offset_from_start_address (uint8_t offset)
 Records the offset as configured, and seeds the resolved position with it.
 
void set_address (uint16_t address)
 Sets the configured address, and points the range base at it.
 
void set_custom_pdu (std::initializer_list< uint8_t > pdu)
 
virtual uint16_t entity_count () const
 Entities this item spans: one bit for bit-addressed types, ceil(bytes / 2) registers for RAW with a response_size, else the value type's register width.
 
virtual size_t get_register_size () const
 Bytes this item's registers occupy in a response: one per bit for bit-addressed types; response_size when set (devices that answer more bytes per register than the standard two); else two per register.
 
void set_register_size (uint8_t register_size)
 

Data Fields

modbus::EntityType register_type {modbus::EntityType::CUSTOM}
 
SensorValueType sensor_value_type {SensorValueType::RAW}
 
uint16_t start_address {0}
 
uint32_t bitmask {0}
 
uint8_t offset {0}
 Position of this sensor's data within its range's response - a byte offset for registers, a bit index for coils and discrete inputs.
 
uint8_t response_bytes {0}
 
uint8_t offset_from_start_address {0}
 The offset exactly as configured: measured from this sensor's own start_address, where offset is measured from the first register of the range it ends up polled in.
 
uint16_t range_start_address {0}
 First register of the range this sensor is polled in; equals start_address for an unpolled item.
 
SmallInlineBuffer< 8 > custom_pdu {}
 
RangeReuse reuse_previous_range {RangeReuse::AUTO}
 

Detailed Description

Definition at line 139 of file modbus_controller.h.

Member Function Documentation

◆ addresses_bits()

bool esphome::modbus_controller::SensorItem::addresses_bits ( ) const
inline

Coils and discrete inputs address individual bits; every other type addresses 16-bit registers.

Definition at line 147 of file modbus_controller.h.

◆ entity_count()

virtual uint16_t esphome::modbus_controller::SensorItem::entity_count ( ) const
inlinevirtual

Entities this item spans: one bit for bit-addressed types, ceil(bytes / 2) registers for RAW with a response_size, else the value type's register width.

Reimplemented in esphome::modbus_controller::ModbusBinarySensor.

Definition at line 175 of file modbus_controller.h.

◆ get_register_size()

virtual size_t esphome::modbus_controller::SensorItem::get_register_size ( ) const
inlinevirtual

Bytes this item's registers occupy in a response: one per bit for bit-addressed types; response_size when set (devices that answer more bytes per register than the standard two); else two per register.

Definition at line 187 of file modbus_controller.h.

◆ parse_and_publish()

virtual void esphome::modbus_controller::SensorItem::parse_and_publish ( std::span< const uint8_t > data)
pure virtual

Parse this sensor's slice out of its range's response and publish it.

The span points into the response buffer and is only valid for the duration of the call. Read the sensor's data from offset within it.

Implemented in esphome::modbus_controller::ModbusBinaryOutput, esphome::modbus_controller::ModbusBinarySensor, esphome::modbus_controller::ModbusFloatOutput, esphome::modbus_controller::ModbusNumber, esphome::modbus_controller::ModbusSelect, esphome::modbus_controller::ModbusSensor, esphome::modbus_controller::ModbusSwitch, and esphome::modbus_controller::ModbusTextSensor.

◆ set_address()

void esphome::modbus_controller::SensorItem::set_address ( uint16_t address)
inline

Sets the configured address, and points the range base at it.

Building the ranges moves the base to the range's first register; an item that is never polled (an output, or a switch with assumed_state) keeps its own address, so write_address() stays correct for it.

Definition at line 166 of file modbus_controller.h.

◆ set_custom_pdu()

void esphome::modbus_controller::SensorItem::set_custom_pdu ( std::initializer_list< uint8_t > pdu)
inline

Definition at line 171 of file modbus_controller.h.

◆ set_offset_from_start_address()

void esphome::modbus_controller::SensorItem::set_offset_from_start_address ( uint8_t offset)
inline

Records the offset as configured, and seeds the resolved position with it.

Building the ranges overwrites offset with the position within the range; an item that is never polled keeps this value, which is what its own address arithmetic expects.

Definition at line 158 of file modbus_controller.h.

◆ set_register_size()

void esphome::modbus_controller::SensorItem::set_register_size ( uint8_t register_size)
inline

Definition at line 195 of file modbus_controller.h.

◆ write_address()

uint16_t esphome::modbus_controller::SensorItem::write_address ( ) const
inline

Address a write entity (switch/number/select) targets, derived from its resolved position within the range so that a write lands on the register the sensor reads from.

Definition at line 151 of file modbus_controller.h.

Field Documentation

◆ bitmask

uint32_t esphome::modbus_controller::SensorItem::bitmask {0}

Definition at line 199 of file modbus_controller.h.

◆ custom_pdu

SmallInlineBuffer<8> esphome::modbus_controller::SensorItem::custom_pdu {}

Definition at line 214 of file modbus_controller.h.

◆ offset

uint8_t esphome::modbus_controller::SensorItem::offset {0}

Position of this sensor's data within its range's response - a byte offset for registers, a bit index for coils and discrete inputs.

Resolved while the ranges are built, so it already accounts for the registers ahead of it (including wide response_size ones) and for any offset inherited from an earlier sensor sharing the same register.

Definition at line 204 of file modbus_controller.h.

◆ offset_from_start_address

uint8_t esphome::modbus_controller::SensorItem::offset_from_start_address {0}

The offset exactly as configured: measured from this sensor's own start_address, where offset is measured from the first register of the range it ends up polled in.

Same units as offset - bytes for registers, bits for coils and discrete inputs. Kept so the resolution can be recomputed, and so the sort order of the sensor set never depends on the resolved value. Declared before range_start_address so it lands in the padding after response_bytes.

Definition at line 211 of file modbus_controller.h.

◆ range_start_address

uint16_t esphome::modbus_controller::SensorItem::range_start_address {0}

First register of the range this sensor is polled in; equals start_address for an unpolled item.

Definition at line 213 of file modbus_controller.h.

◆ register_type

modbus::EntityType esphome::modbus_controller::SensorItem::register_type {modbus::EntityType::CUSTOM}

Definition at line 196 of file modbus_controller.h.

◆ response_bytes

uint8_t esphome::modbus_controller::SensorItem::response_bytes {0}

Definition at line 205 of file modbus_controller.h.

◆ reuse_previous_range

RangeReuse esphome::modbus_controller::SensorItem::reuse_previous_range {RangeReuse::AUTO}

Definition at line 215 of file modbus_controller.h.

◆ sensor_value_type

SensorValueType esphome::modbus_controller::SensorItem::sensor_value_type {SensorValueType::RAW}

Definition at line 197 of file modbus_controller.h.

◆ start_address

uint16_t esphome::modbus_controller::SensorItem::start_address {0}

Definition at line 198 of file modbus_controller.h.


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