ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
modbus_switch.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <vector>
8
10
11class ModbusSwitch final : public Component, public switch_::Switch, public SensorItem, public WriterEntity {
12 public:
15 this->register_type = register_type;
16 this->set_address(start_address);
18 this->bitmask = bitmask;
20 // A holding byte offset folds into the address as whole registers (odd offsets are rejected at
21 // validation: a 16-bit register write cannot target half a register); a coil offset is a coil count.
22 if (register_type == modbus::EntityType::HOLDING) {
23 this->set_address(start_address + offset / 2);
25 } else if (register_type == modbus::EntityType::COIL) {
26 this->set_address(start_address + offset);
28 }
29 this->reuse_previous_range = reuse_previous_range;
30 };
31 void setup() override;
32 void write_state(bool state) override;
33 void dump_config() override;
35 void set_state(bool state) { this->state = state; }
36 void parse_and_publish(std::span<const uint8_t> data) override;
37 void set_parent(ModbusController *parent) { this->set_controller_(parent); }
38
39 using transform_func_t = optional<bool> (*)(ModbusSwitch *, bool, std::span<const uint8_t>);
40 using write_transform_func_t = optional<bool> (*)(ModbusSwitch *, bool, modbus::helpers::PduBuffer &);
43 void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
44
45 protected:
46 bool assumed_state() override;
48 optional<transform_func_t> publish_transform_func_{nullopt};
49 optional<write_transform_func_t> write_transform_func_{nullopt};
50 bool assumed_state_{false};
51};
52
53} // namespace esphome::modbus_controller
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:227
void parse_and_publish(std::span< const uint8_t > data) override
void set_use_write_mutiple(bool use_write_multiple)
optional< write_transform_func_t > write_transform_func_
void set_assumed_state(bool assumed_state)
ModbusSwitch(modbus::EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask, RangeReuse reuse_previous_range)
void set_parent(ModbusController *parent)
void set_write_template(write_transform_func_t f)
optional< bool >(*)(ModbusSwitch *, bool, modbus::helpers::PduBuffer &) write_transform_func_t
optional< bool >(*)(ModbusSwitch *, bool, std::span< const uint8_t >) transform_func_t
optional< transform_func_t > publish_transform_func_
void set_template(transform_func_t f)
void set_address(uint16_t address)
Sets the configured address, and points the range base at it.
void set_offset_from_start_address(uint8_t offset)
Records the offset as configured, and seeds the resolved position with it.
uint8_t offset
Position of this sensor's data within its range's response - a byte offset for registers,...
Gives a writer entity the write API of the WriterDevice it owns.
void set_controller_(ModbusController *controller)
Base class for all switches.
Definition switch.h:38
bool state
The current reported state of the binary sensor.
Definition switch.h:55
const std::vector< uint8_t > & data
RangeReuse
How an item relates to the register range built just before it (same register type,...
static void uint32_t