10static const char *
const TAG =
"modbus_controller.switch";
13static constexpr size_t MODBUS_SWITCH_MAX_LOG_BYTES = 64;
16 optional<bool> initial_state = Switch::get_initial_state_with_restore_mode();
17 if (initial_state.has_value()) {
19 if (initial_state.value()) {
51 if (
val.has_value()) {
52 ESP_LOGV(TAG,
"Value overwritten by lambda");
57 ESP_LOGV(TAG,
"Publish '%s': new value = %s type = %d address = %X offset = %zx", this->
get_name().c_str(),
68 bool write_value =
state;
79#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
82 ESP_LOGV(TAG,
"Modbus Switch write raw: %s",
86 ESP_LOGW(TAG,
"Modbus write for '%s' was refused by the hub; state not published", this->
get_name().c_str());
92 if (!
val.has_value()) {
93 ESP_LOGV(TAG,
"Communication handled by lambda - exiting control");
98 ESP_LOGV(TAG,
"Value overwritten by lambda");
99 write_value =
val.value();
101 ESP_LOGV(TAG,
"write_state '%s': new value = %s (wire = %s) type = %d address = %X offset = %x",
108 std::array<bool, 1> states{write_value};
115 std::array<uint16_t, 1> states{
static_cast<uint16_t
>(write_value ? (0xFFFF & this->
bitmask) : 0)};
122 ESP_LOGW(TAG,
"Modbus write for '%s' was refused by the hub; state not published", this->
get_name().c_str());
const StringRef & get_name() const
Minimal static vector - saves memory by avoiding std::vector overhead.
void parse_and_publish(std::span< const uint8_t > data) override
optional< write_transform_func_t > write_transform_func_
bool assumed_state() override
void set_assumed_state(bool assumed_state)
void write_state(bool state) override
void dump_config() override
optional< transform_func_t > publish_transform_func_
modbus::EntityType register_type
uint8_t offset
Position of this sensor's data within its range's response - a byte offset for registers,...
uint16_t write_address() const
Address a write entity (switch/number/select) targets, derived from its resolved position within the ...
void warn_write_buffer_deprecated_(const LogString *platform, uint16_t address)
bool dispatched() const
Whether the lambda called a request helper since the last clear_dispatched_().
bool write_single_coil(uint16_t address, bool value)
bool write_single_register(uint16_t address, uint16_t value)
bool send_raw_frame_deprecated_(std::span< const uint8_t > frame)
void clear_tx_queue_for_device()
bool write_multiple_coils(uint16_t address, std::span< const bool > values)
bool write_multiple_registers(uint16_t address, std::span< const uint16_t > values)
void turn_on()
Turn this switch on.
void turn_off()
Turn this switch off.
bool state
The current reported state of the binary sensor.
void publish_state(bool state)
Publish a state to the front-end from the back-end.
T get_data(const uint8_t *data, size_t buffer_offset)
Extract data from modbus response buffer.
bool bit_from_packed(int bit, std::span< const uint8_t > data)
Extract coil data from modbus response buffer Responses for coil are packed into bytes .
const std::vector< uint8_t > & data
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".