1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-28 05:33:53 +00:00

Modbus controller (#1779)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Martin
2021-09-26 22:27:24 +02:00
committed by GitHub
parent 4d28afc153
commit 7672ba2c8d
27 changed files with 2505 additions and 34 deletions

View File

@@ -0,0 +1,101 @@
from esphome.components import text_sensor
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_ID, CONF_ADDRESS, CONF_LAMBDA, CONF_OFFSET
from .. import (
SensorItem,
modbus_controller_ns,
ModbusController,
MODBUS_REGISTER_TYPE,
)
from ..const import (
CONF_BYTE_OFFSET,
CONF_FORCE_NEW_RANGE,
CONF_MODBUS_CONTROLLER_ID,
CONF_REGISTER_COUNT,
CONF_RESPONSE_SIZE,
CONF_SKIP_UPDATES,
CONF_RAW_ENCODE,
CONF_REGISTER_TYPE,
)
DEPENDENCIES = ["modbus_controller"]
CODEOWNERS = ["@martgras"]
ModbusTextSensor = modbus_controller_ns.class_(
"ModbusTextSensor", cg.Component, text_sensor.TextSensor, SensorItem
)
RawEncoding_ns = modbus_controller_ns.namespace("RawEncoding")
RawEncoding = RawEncoding_ns.enum("RawEncoding")
RAW_ENCODING = {
"NONE": RawEncoding.NONE,
"HEXBYTES": RawEncoding.HEXBYTES,
"COMMA": RawEncoding.COMMA,
}
CONFIG_SCHEMA = cv.All(
text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(ModbusTextSensor),
cv.GenerateID(CONF_MODBUS_CONTROLLER_ID): cv.use_id(ModbusController),
cv.Required(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE),
cv.Required(CONF_ADDRESS): cv.positive_int,
cv.Optional(CONF_OFFSET, default=0): cv.positive_int,
cv.Optional(CONF_BYTE_OFFSET): cv.positive_int,
cv.Optional(CONF_REGISTER_COUNT, default=0): cv.positive_int,
cv.Optional(CONF_RESPONSE_SIZE, default=2): cv.positive_int,
cv.Optional(CONF_RAW_ENCODE, default="NONE"): cv.enum(RAW_ENCODING),
cv.Optional(CONF_SKIP_UPDATES, default=0): cv.positive_int,
cv.Optional(CONF_FORCE_NEW_RANGE, default=False): cv.boolean,
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
}
).extend(cv.COMPONENT_SCHEMA),
)
async def to_code(config):
byte_offset = 0
if CONF_OFFSET in config:
byte_offset = config[CONF_OFFSET]
# A CONF_BYTE_OFFSET setting overrides CONF_OFFSET
if CONF_BYTE_OFFSET in config:
byte_offset = config[CONF_BYTE_OFFSET]
response_size = config[CONF_RESPONSE_SIZE]
reg_count = config[CONF_REGISTER_COUNT]
if reg_count == 0:
reg_count = response_size / 2
var = cg.new_Pvariable(
config[CONF_ID],
config[CONF_REGISTER_TYPE],
config[CONF_ADDRESS],
byte_offset,
reg_count,
config[CONF_RESPONSE_SIZE],
config[CONF_RAW_ENCODE],
config[CONF_SKIP_UPDATES],
config[CONF_FORCE_NEW_RANGE],
)
await cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
paren = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
cg.add(paren.add_sensor_item(var))
if CONF_LAMBDA in config:
template_ = await cg.process_lambda(
config[CONF_LAMBDA],
[
(ModbusTextSensor.operator("ptr"), "item"),
(cg.std_string.operator("const").operator("ref"), "x"),
(
cg.std_vector.template(cg.uint8).operator("const").operator("ref"),
"data",
),
],
return_type=cg.optional.template(cg.std_string),
)
cg.add(var.set_template(template_))

View File

@@ -0,0 +1,56 @@
#include "modbus_textsensor.h"
#include "esphome/core/log.h"
#include <iomanip>
#include <sstream>
namespace esphome {
namespace modbus_controller {
static const char *const TAG = "modbus_controller.text_sensor";
void ModbusTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Modbus Controller Text Sensor", this); }
void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
std::ostringstream output;
uint8_t max_items = this->response_bytes_;
char buffer[4];
bool add_comma = false;
for (auto b : data) {
switch (this->encode_) {
case RawEncoding::HEXBYTES:
sprintf(buffer, "%02x", b);
output << buffer;
break;
case RawEncoding::COMMA:
sprintf(buffer, add_comma ? ",%d" : "%d", b);
output << buffer;
add_comma = true;
break;
// Anything else no encoding
case RawEncoding::NONE:
default:
output << (char) b;
break;
}
if (--max_items == 0) {
break;
}
}
auto result = output.str();
// Is there a lambda registered
// call it with the pre converted value and the raw data array
if (this->transform_func_.has_value()) {
// the lambda can parse the response itself
auto val = (*this->transform_func_)(this, result, data);
if (val.has_value()) {
ESP_LOGV(TAG, "Value overwritten by lambda");
result = val.value();
}
}
this->publish_state(result);
}
} // namespace modbus_controller
} // namespace esphome

View File

@@ -0,0 +1,52 @@
#pragma once
#include "esphome/components/modbus_controller/modbus_controller.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/core/component.h"
namespace esphome {
namespace modbus_controller {
enum class RawEncoding { NONE = 0, HEXBYTES = 1, COMMA = 2 };
class ModbusTextSensor : public Component, public text_sensor::TextSensor, public SensorItem {
public:
ModbusTextSensor(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint8_t register_count,
uint16_t response_bytes, RawEncoding encode, uint8_t skip_updates, bool force_new_range)
: Component() {
this->register_type = register_type;
this->start_address = start_address;
this->offset = offset;
this->response_bytes_ = response_bytes;
this->register_count = register_count;
this->encode_ = encode;
this->skip_updates = skip_updates;
this->bitmask = 0xFFFFFFFF;
this->sensor_value_type = SensorValueType::RAW;
this->force_new_range = force_new_range;
}
size_t get_register_size() const override {
if (sensor_value_type == SensorValueType::RAW) {
return this->response_bytes_;
} else {
return SensorItem::get_register_size();
}
}
void dump_config() override;
void parse_and_publish(const std::vector<uint8_t> &data) override;
using transform_func_t =
std::function<optional<std::string>(ModbusTextSensor *, std::string, const std::vector<uint8_t> &)>;
void set_template(transform_func_t &&f) { this->transform_func_ = f; }
protected:
optional<transform_func_t> transform_func_{nullopt};
protected:
RawEncoding encode_;
uint16_t response_bytes_;
};
} // namespace modbus_controller
} // namespace esphome