ESPHome 2026.8.1
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 {
12 public:
14 uint16_t skip_updates, bool force_new_range) {
15 this->register_type = register_type;
16 this->set_address(start_address);
18 this->bitmask = bitmask;
20 this->skip_updates = skip_updates;
21 this->register_count = 1;
23 this->set_address(this->start_address + offset);
25 }
26 this->force_new_range = force_new_range;
27 };
28 void setup() override;
29 void write_state(bool state) override;
30 void dump_config() override;
32 void set_state(bool state) { this->state = state; }
33 void parse_and_publish(std::span<const uint8_t> data) override;
34 void set_parent(ModbusController *parent) { this->parent_ = parent; }
35
36 using transform_func_t = optional<bool> (*)(ModbusSwitch *, bool, std::span<const uint8_t>);
37 using write_transform_func_t = optional<bool> (*)(ModbusSwitch *, bool, std::vector<uint8_t> &);
40 void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
41
42 protected:
43 bool assumed_state() override;
46 optional<transform_func_t> publish_transform_func_{nullopt};
47 optional<write_transform_func_t> write_transform_func_{nullopt};
48 bool assumed_state_{false};
49};
50
51} // namespace esphome::modbus_controller
void parse_and_publish(std::span< const uint8_t > data) override
ModbusSwitch(modbus::EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask, uint16_t skip_updates, bool force_new_range)
void set_use_write_mutiple(bool use_write_multiple)
optional< bool >(*)(ModbusSwitch *, bool, std::vector< uint8_t > &) write_transform_func_t
optional< write_transform_func_t > write_transform_func_
void set_assumed_state(bool assumed_state)
void set_parent(ModbusController *parent)
void set_write_template(write_transform_func_t f)
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,...
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
static void uint32_t