ESPHome 2026.5.0
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 : 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->start_address = start_address;
17 this->offset = offset;
18 this->bitmask = bitmask;
20 this->skip_updates = skip_updates;
21 this->register_count = 1;
23 this->start_address += offset;
24 this->offset = 0;
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(const std::vector<uint8_t> &data) override;
34 void set_parent(ModbusController *parent) { this->parent_ = parent; }
35
36 using transform_func_t = optional<bool> (*)(ModbusSwitch *, bool, const std::vector<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
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:454
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)
optional< bool >(*)(ModbusSwitch *, bool, const std::vector< uint8_t > &) transform_func_t
void set_parent(ModbusController *parent)
void set_write_template(write_transform_func_t f)
void parse_and_publish(const std::vector< uint8_t > &data) override
ModbusSwitch(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask, uint16_t skip_updates, bool force_new_range)
optional< transform_func_t > publish_transform_func_
void set_template(transform_func_t f)
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