mirror of
https://github.com/esphome/esphome.git
synced 2026-08-22 22:26:21 +00:00
[modbus_controller] Qualify modbus::EntityType at use sites (#17845)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
9ac4039c01
commit
6648ccc278
@@ -11,8 +11,8 @@ void ModbusBinarySensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
bool value;
|
||||
|
||||
switch (this->register_type) {
|
||||
case EntityType::DISCRETE_INPUT:
|
||||
case EntityType::COIL:
|
||||
case modbus::EntityType::DISCRETE_INPUT:
|
||||
case modbus::EntityType::COIL:
|
||||
// offset for coil is the actual number of the coil not the byte offset
|
||||
value = modbus::helpers::bit_from_packed(this->offset, data);
|
||||
break;
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusBinarySensor final : public Component, public binary_sensor::BinarySensor, public SensorItem {
|
||||
public:
|
||||
ModbusBinarySensor(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusBinarySensor(modbus::EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
@@ -20,7 +20,7 @@ class ModbusBinarySensor final : public Component, public binary_sensor::BinaryS
|
||||
this->skip_updates = skip_updates;
|
||||
this->force_new_range = force_new_range;
|
||||
|
||||
if (register_type == EntityType::COIL || register_type == EntityType::DISCRETE_INPUT) {
|
||||
if (register_type == modbus::EntityType::COIL || register_type == modbus::EntityType::DISCRETE_INPUT) {
|
||||
this->register_count = offset + 1;
|
||||
} else {
|
||||
this->register_count = 1;
|
||||
|
||||
@@ -116,7 +116,7 @@ void ModbusController::on_error(std::span<const uint8_t> request_pdu, modbus::Ex
|
||||
}
|
||||
}
|
||||
|
||||
SensorSet ModbusController::find_sensors_(EntityType register_type, uint16_t start_address) const {
|
||||
SensorSet ModbusController::find_sensors_(modbus::EntityType register_type, uint16_t start_address) const {
|
||||
auto reg_it = std::find_if(
|
||||
std::begin(this->register_ranges_), std::end(this->register_ranges_),
|
||||
[=](RegisterRange const &r) { return (r.start_address == start_address && r.register_type == register_type); });
|
||||
@@ -130,7 +130,7 @@ SensorSet ModbusController::find_sensors_(EntityType register_type, uint16_t sta
|
||||
// not found
|
||||
return {};
|
||||
}
|
||||
void ModbusController::on_register_data(EntityType register_type, uint16_t start_address,
|
||||
void ModbusController::on_register_data(modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
ESP_LOGV(TAG, "data for register address : 0x%X : ", start_address);
|
||||
|
||||
@@ -164,14 +164,14 @@ void ModbusController::update_range_(RegisterRange &r) {
|
||||
r.skip_updates_counter);
|
||||
if (r.skip_updates_counter == 0) {
|
||||
// if a custom command is used the user supplied custom_data is only available in the SensorItem.
|
||||
if (r.register_type == EntityType::CUSTOM) {
|
||||
if (r.register_type == modbus::EntityType::CUSTOM) {
|
||||
auto sensors = this->find_sensors_(r.register_type, r.start_address);
|
||||
if (!sensors.empty()) {
|
||||
auto sensor = sensors.cbegin();
|
||||
auto command_item = ModbusCommandItem::create_custom_command(
|
||||
this, (*sensor)->custom_data,
|
||||
[this](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->on_register_data(EntityType::CUSTOM, start_address, data);
|
||||
[this](modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->on_register_data(modbus::EntityType::CUSTOM, start_address, data);
|
||||
});
|
||||
command_item.register_address = (*sensor)->start_address;
|
||||
command_item.register_count = (*sensor)->register_count;
|
||||
@@ -237,7 +237,7 @@ size_t ModbusController::create_register_ranges_() {
|
||||
// this is not the first register in range so it might be possible
|
||||
// to reuse the last register or extend the current range
|
||||
if (!curr->force_new_range && r.register_type == curr->register_type &&
|
||||
curr->register_type != EntityType::CUSTOM) {
|
||||
curr->register_type != modbus::EntityType::CUSTOM) {
|
||||
if (curr->start_address == (r.start_address + r.register_count - prev->register_count) &&
|
||||
curr->register_count == prev->register_count && curr->get_register_size() == prev->get_register_size()) {
|
||||
// this register can re-use the data from the previous register
|
||||
@@ -347,7 +347,7 @@ void ModbusController::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void ModbusController::on_write_register_response(EntityType register_type, uint16_t start_address,
|
||||
void ModbusController::on_write_register_response(modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
ESP_LOGV(TAG, "Command ACK 0x%X %d ", modbus::helpers::get_data<uint16_t>(data, 0),
|
||||
modbus::helpers::get_data<int16_t>(data, 1));
|
||||
@@ -362,8 +362,9 @@ void ModbusController::dump_sensors_() {
|
||||
}
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_read_command(
|
||||
ModbusController *modbusdevice, EntityType register_type, uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> &&handler) {
|
||||
ModbusController *modbusdevice, modbus::EntityType register_type, uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = register_type;
|
||||
@@ -374,15 +375,16 @@ ModbusCommandItem ModbusCommandItem::create_read_command(
|
||||
return cmd;
|
||||
}
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_read_command(ModbusController *modbusdevice, EntityType register_type,
|
||||
uint16_t start_address, uint16_t register_count) {
|
||||
ModbusCommandItem ModbusCommandItem::create_read_command(ModbusController *modbusdevice,
|
||||
modbus::EntityType register_type, uint16_t start_address,
|
||||
uint16_t register_count) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = register_type;
|
||||
cmd.function_code = modbus::helpers::modbus_register_read_function(register_type);
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = register_count;
|
||||
cmd.on_data_func = [modbusdevice](EntityType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice](modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_register_data(register_type, start_address, data);
|
||||
};
|
||||
@@ -394,11 +396,11 @@ ModbusCommandItem ModbusCommandItem::create_write_multiple_command(ModbusControl
|
||||
const std::vector<uint16_t> &values) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = EntityType::HOLDING;
|
||||
cmd.register_type = modbus::EntityType::HOLDING;
|
||||
cmd.function_code = FunctionCode::WRITE_MULTIPLE_REGISTERS;
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = register_count;
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -414,11 +416,11 @@ ModbusCommandItem ModbusCommandItem::create_write_single_coil(ModbusController *
|
||||
bool value) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = EntityType::COIL;
|
||||
cmd.register_type = modbus::EntityType::COIL;
|
||||
cmd.function_code = FunctionCode::WRITE_SINGLE_COIL;
|
||||
cmd.register_address = address;
|
||||
cmd.register_count = 1;
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -431,11 +433,11 @@ ModbusCommandItem ModbusCommandItem::create_write_multiple_coils(ModbusControlle
|
||||
const std::vector<bool> &values) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = EntityType::COIL;
|
||||
cmd.register_type = modbus::EntityType::COIL;
|
||||
cmd.function_code = FunctionCode::WRITE_MULTIPLE_COILS;
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = values.size();
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -463,11 +465,11 @@ ModbusCommandItem ModbusCommandItem::create_write_single_command(ModbusControlle
|
||||
uint16_t value) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = EntityType::HOLDING;
|
||||
cmd.register_type = modbus::EntityType::HOLDING;
|
||||
cmd.function_code = FunctionCode::WRITE_SINGLE_REGISTER;
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = 1; // not used here anyways
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -480,12 +482,13 @@ ModbusCommandItem ModbusCommandItem::create_write_single_command(ModbusControlle
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint8_t> &values,
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> &&handler) {
|
||||
std::function<void(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.function_code = FunctionCode::CUSTOM;
|
||||
if (handler == nullptr) {
|
||||
cmd.on_data_func = [](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
cmd.on_data_func = [](modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
ESP_LOGI(TAG, "Custom Command sent");
|
||||
};
|
||||
} else {
|
||||
@@ -498,12 +501,13 @@ ModbusCommandItem ModbusCommandItem::create_custom_command(
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint16_t> &values,
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> &&handler) {
|
||||
std::function<void(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler) {
|
||||
ModbusCommandItem cmd = {};
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.function_code = FunctionCode::CUSTOM;
|
||||
if (handler == nullptr) {
|
||||
cmd.on_data_func = [](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
cmd.on_data_func = [](modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
ESP_LOGI(TAG, "Custom Command sent");
|
||||
};
|
||||
} else {
|
||||
|
||||
@@ -17,7 +17,6 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusController;
|
||||
|
||||
using modbus::EntityType;
|
||||
using modbus::ExceptionCode;
|
||||
using modbus::FunctionCode;
|
||||
using modbus::helpers::SensorValueType;
|
||||
@@ -35,12 +34,12 @@ ESPDEPRECATED("Use modbus::helpers::value_type_is_float() instead. Removed in 20
|
||||
inline bool value_type_is_float(SensorValueType v) { return modbus::helpers::value_type_is_float(v); }
|
||||
|
||||
ESPDEPRECATED("Use modbus::helpers::modbus_register_read_function() instead. Removed in 2026.10.0", "2026.4.0")
|
||||
inline FunctionCode modbus_register_read_function(EntityType reg_type) {
|
||||
inline FunctionCode modbus_register_read_function(modbus::EntityType reg_type) {
|
||||
return modbus::helpers::modbus_register_read_function(reg_type);
|
||||
}
|
||||
|
||||
ESPDEPRECATED("Use modbus::helpers::modbus_register_write_function() instead. Removed in 2026.10.0", "2026.4.0")
|
||||
inline FunctionCode modbus_register_write_function(EntityType reg_type) {
|
||||
inline FunctionCode modbus_register_write_function(modbus::EntityType reg_type) {
|
||||
return modbus::helpers::modbus_register_write_function(reg_type);
|
||||
}
|
||||
|
||||
@@ -110,7 +109,7 @@ class SensorItem {
|
||||
|
||||
void set_custom_data(const std::vector<uint8_t> &data) { custom_data = data; }
|
||||
size_t virtual get_register_size() const {
|
||||
if (register_type == EntityType::COIL || register_type == EntityType::DISCRETE_INPUT) {
|
||||
if (register_type == modbus::EntityType::COIL || register_type == modbus::EntityType::DISCRETE_INPUT) {
|
||||
return 1;
|
||||
} else { // if CONF_RESPONSE_BYTES is used override the default
|
||||
return response_bytes > 0 ? response_bytes : register_count * 2;
|
||||
@@ -118,7 +117,7 @@ class SensorItem {
|
||||
}
|
||||
// Override register size for modbus devices not using 1 register for one dword
|
||||
void set_register_size(uint8_t register_size) { response_bytes = register_size; }
|
||||
EntityType register_type{EntityType::CUSTOM};
|
||||
modbus::EntityType register_type{modbus::EntityType::CUSTOM};
|
||||
SensorValueType sensor_value_type{SensorValueType::RAW};
|
||||
uint16_t start_address{0};
|
||||
uint32_t bitmask{0};
|
||||
@@ -165,7 +164,7 @@ using SensorSet = std::set<SensorItem *, SensorItemsComparator>;
|
||||
|
||||
struct RegisterRange {
|
||||
uint16_t start_address;
|
||||
EntityType register_type;
|
||||
modbus::EntityType register_type;
|
||||
uint8_t register_count;
|
||||
uint16_t skip_updates; // the config value
|
||||
SensorSet sensors; // all sensors of this range
|
||||
@@ -179,8 +178,9 @@ class ModbusCommandItem {
|
||||
uint16_t register_address{0};
|
||||
uint16_t register_count{0};
|
||||
FunctionCode function_code{FunctionCode::CUSTOM};
|
||||
EntityType register_type{EntityType::CUSTOM};
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> on_data_func;
|
||||
modbus::EntityType register_type{modbus::EntityType::CUSTOM};
|
||||
std::function<void(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
on_data_func;
|
||||
std::vector<uint8_t> payload = {};
|
||||
bool send();
|
||||
/// Check if the command should be retried based on the max_retries parameter
|
||||
@@ -196,10 +196,10 @@ class ModbusCommandItem {
|
||||
* @param handler function called when the response is received
|
||||
* @return ModbusCommandItem with the prepared command
|
||||
*/
|
||||
static ModbusCommandItem create_read_command(ModbusController *modbusdevice, EntityType register_type,
|
||||
uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data)> &&handler);
|
||||
static ModbusCommandItem create_read_command(
|
||||
ModbusController *modbusdevice, modbus::EntityType register_type, uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler);
|
||||
/** Create modbus read command
|
||||
* Function code 02-04
|
||||
* @param modbusdevice pointer to the device to execute the command
|
||||
@@ -208,7 +208,7 @@ class ModbusCommandItem {
|
||||
* @param register_count number of registers to read
|
||||
* @return ModbusCommandItem with the prepared command
|
||||
*/
|
||||
static ModbusCommandItem create_read_command(ModbusController *modbusdevice, EntityType register_type,
|
||||
static ModbusCommandItem create_read_command(ModbusController *modbusdevice, modbus::EntityType register_type,
|
||||
uint16_t start_address, uint16_t register_count);
|
||||
/** Create modbus read command
|
||||
* Function code 02-04
|
||||
@@ -258,7 +258,7 @@ class ModbusCommandItem {
|
||||
*/
|
||||
static ModbusCommandItem create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint8_t> &values,
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
std::function<void(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler = nullptr);
|
||||
|
||||
/** Create custom modbus command
|
||||
@@ -270,7 +270,7 @@ class ModbusCommandItem {
|
||||
*/
|
||||
static ModbusCommandItem create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint16_t> &values,
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
std::function<void(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler = nullptr);
|
||||
|
||||
bool is_equal(const ModbusCommandItem &other);
|
||||
@@ -305,10 +305,11 @@ class ModbusController final : public PollingComponent, public modbus::ModbusCli
|
||||
/// called when a modbus error response was received
|
||||
void on_error(std::span<const uint8_t> request_pdu, modbus::ExceptionCode exception_code) override;
|
||||
/// default delegate called by process_modbus_data when a response has retrieved from the incoming queue
|
||||
void on_register_data(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data);
|
||||
void on_register_data(modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data);
|
||||
/// default delegate called by process_modbus_data when a response for a write response has retrieved from the
|
||||
/// incoming queue
|
||||
void on_write_register_response(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data);
|
||||
void on_write_register_response(modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data);
|
||||
/// Allow a duplicate command to be sent
|
||||
void set_allow_duplicate_commands(bool allow_duplicate_commands) {
|
||||
this->allow_duplicate_commands_ = allow_duplicate_commands;
|
||||
@@ -344,7 +345,7 @@ class ModbusController final : public PollingComponent, public modbus::ModbusCli
|
||||
/// parse sensormap_ and create range of sequential addresses
|
||||
size_t create_register_ranges_();
|
||||
// find register in sensormap. Returns iterator with all registers having the same start address
|
||||
SensorSet find_sensors_(EntityType register_type, uint16_t start_address) const;
|
||||
SensorSet find_sensors_(modbus::EntityType register_type, uint16_t start_address) const;
|
||||
/// submit the read command for the address range to the send queue
|
||||
void update_range_(RegisterRange &r);
|
||||
/// parse incoming modbus data
|
||||
|
||||
@@ -57,7 +57,7 @@ void ModbusNumber::control(float value) {
|
||||
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
|
||||
write_cmd = ModbusCommandItem::create_custom_command(
|
||||
this->parent_, data,
|
||||
[this, write_cmd](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
[this, write_cmd](modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->parent_->on_write_register_response(write_cmd.register_type, this->start_address, data);
|
||||
});
|
||||
} else {
|
||||
@@ -77,7 +77,7 @@ void ModbusNumber::control(float value) {
|
||||
this->parent_, this->start_address + this->offset / 2, this->register_count, data);
|
||||
}
|
||||
// publish new value
|
||||
write_cmd.on_data_func = [this, write_cmd, value](EntityType register_type, uint16_t start_address,
|
||||
write_cmd.on_data_func = [this, write_cmd, value](modbus::EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
// gets called when the write command is ack'd from the device
|
||||
this->parent_->on_write_register_response(write_cmd.register_type, start_address, data);
|
||||
|
||||
@@ -12,7 +12,7 @@ using value_to_data_t = std::function<float>(float);
|
||||
|
||||
class ModbusNumber final : public number::Number, public Component, public SensorItem {
|
||||
public:
|
||||
ModbusNumber(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusNumber(modbus::EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
SensorValueType value_type, int register_count, uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
|
||||
@@ -89,7 +89,7 @@ void ModbusBinaryOutput::write_state(bool state) {
|
||||
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
|
||||
cmd = ModbusCommandItem::create_custom_command(
|
||||
this->parent_, data,
|
||||
[this, cmd](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
[this, cmd](modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->parent_->on_write_register_response(cmd.register_type, this->start_address, data);
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace esphome::modbus_controller {
|
||||
class ModbusFloatOutput final : public output::FloatOutput, public Component, public SensorItem {
|
||||
public:
|
||||
ModbusFloatOutput(uint16_t start_address, uint8_t offset, SensorValueType value_type, int register_count) {
|
||||
this->register_type = EntityType::HOLDING;
|
||||
this->register_type = modbus::EntityType::HOLDING;
|
||||
this->start_address = start_address;
|
||||
this->offset = offset;
|
||||
this->bitmask = 0xFFFFFFFF;
|
||||
@@ -44,7 +44,7 @@ class ModbusFloatOutput final : public output::FloatOutput, public Component, pu
|
||||
class ModbusBinaryOutput final : public output::BinaryOutput, public Component, public SensorItem {
|
||||
public:
|
||||
ModbusBinaryOutput(uint16_t start_address, uint8_t offset) {
|
||||
this->register_type = EntityType::COIL;
|
||||
this->register_type = modbus::EntityType::COIL;
|
||||
this->start_address = start_address;
|
||||
this->bitmask = 0xFFFFFFFF;
|
||||
this->sensor_value_type = SensorValueType::BIT;
|
||||
|
||||
@@ -13,7 +13,7 @@ class ModbusSelect final : public Component, public select::Select, public Senso
|
||||
public:
|
||||
ModbusSelect(SensorValueType sensor_value_type, uint16_t start_address, uint8_t register_count, uint16_t skip_updates,
|
||||
bool force_new_range, std::vector<int64_t> mapping) {
|
||||
this->register_type = EntityType::HOLDING; // not configurable
|
||||
this->register_type = modbus::EntityType::HOLDING; // not configurable
|
||||
this->sensor_value_type = sensor_value_type;
|
||||
this->start_address = start_address;
|
||||
this->offset = 0; // not configurable
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusSensor final : public Component, public sensor::Sensor, public SensorItem {
|
||||
public:
|
||||
ModbusSensor(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusSensor(modbus::EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
SensorValueType value_type, int register_count, uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
|
||||
@@ -30,8 +30,8 @@ bool ModbusSwitch::assumed_state() { return this->assumed_state_; }
|
||||
void ModbusSwitch::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
bool value = false;
|
||||
switch (this->register_type) {
|
||||
case EntityType::DISCRETE_INPUT:
|
||||
case EntityType::COIL:
|
||||
case modbus::EntityType::DISCRETE_INPUT:
|
||||
case modbus::EntityType::COIL:
|
||||
// offset for coil is the actual number of the coil not the byte offset
|
||||
value = modbus::helpers::bit_from_packed(this->offset, data);
|
||||
break;
|
||||
@@ -82,13 +82,13 @@ void ModbusSwitch::write_state(bool state) {
|
||||
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
|
||||
cmd = ModbusCommandItem::create_custom_command(
|
||||
this->parent_, data,
|
||||
[this, cmd](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
[this, cmd](modbus::EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->parent_->on_write_register_response(cmd.register_type, this->start_address, data);
|
||||
});
|
||||
} else {
|
||||
ESP_LOGV(TAG, "write_state '%s': new value = %s type = %d address = %X offset = %x", this->get_name().c_str(),
|
||||
ONOFF(state), (int) this->register_type, this->start_address, this->offset);
|
||||
if (this->register_type == EntityType::COIL) {
|
||||
if (this->register_type == modbus::EntityType::COIL) {
|
||||
// offset for coil and discrete inputs is the coil/register number not bytes
|
||||
if (this->use_write_multiple_) {
|
||||
std::vector<bool> states{state};
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusSwitch final : public Component, public switch_::Switch, public SensorItem {
|
||||
public:
|
||||
ModbusSwitch(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusSwitch(modbus::EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
@@ -19,7 +19,7 @@ class ModbusSwitch final : public Component, public switch_::Switch, public Sens
|
||||
this->sensor_value_type = SensorValueType::BIT;
|
||||
this->skip_updates = skip_updates;
|
||||
this->register_count = 1;
|
||||
if (register_type == EntityType::HOLDING || register_type == EntityType::COIL) {
|
||||
if (register_type == modbus::EntityType::HOLDING || register_type == modbus::EntityType::COIL) {
|
||||
this->start_address += offset;
|
||||
this->offset = 0;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ enum class RawEncoding { NONE = 0, HEXBYTES = 1, COMMA = 2, ANSI = 3 };
|
||||
|
||||
class ModbusTextSensor final : public Component, public text_sensor::TextSensor, public SensorItem {
|
||||
public:
|
||||
ModbusTextSensor(EntityType register_type, uint16_t start_address, uint8_t offset, uint8_t register_count,
|
||||
ModbusTextSensor(modbus::EntityType register_type, uint16_t start_address, uint8_t offset, uint8_t register_count,
|
||||
uint16_t response_bytes, RawEncoding encode, uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
|
||||
Reference in New Issue
Block a user