diff --git a/components/victron/README.md b/components/victron/README.md index 1e828fb..3380dc1 100644 --- a/components/victron/README.md +++ b/components/victron/README.md @@ -1,4 +1,4 @@ -# Victron solar charge controller +#Victron solar charge controller A configured uart component is required. diff --git a/components/victron/__init__.py b/components/victron/__init__.py index d2b0a45..f70e987 100644 --- a/components/victron/__init__.py +++ b/components/victron/__init__.py @@ -1,29 +1,17 @@ -import esphome.codegen as cg -import esphome.config_validation as cv -from esphome.components import uart -from esphome.const import CONF_ID +import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import uart from esphome.const import CONF_ID -AUTO_LOAD = ["sensor", "text_sensor"] + AUTO_LOAD =["sensor", "text_sensor"] -DEPENDENCIES = ["uart"] + DEPENDENCIES =["uart"] -CODEOWNERS = ["@KinDR007"] + CODEOWNERS =["@KinDR007"] -MULTI_CONF = True + MULTI_CONF = True -victron_ns = cg.esphome_ns.namespace("victron") -VictronComponent = victron_ns.class_("VictronComponent", uart.UARTDevice, cg.Component) + victron_ns = cg.esphome_ns.namespace("victron") VictronComponent = victron_ns.class_("VictronComponent", uart.UARTDevice, cg.Component) -CONF_VICTRON_ID = "victron_id" + CONF_VICTRON_ID = "victron_id" -CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(VictronComponent), - } -) + CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend({cv.GenerateID() : cv.declare_id(VictronComponent), }) - -def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) - yield cg.register_component(var, config) - yield uart.register_uart_device(var, config) + def to_code(config) : var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield uart.register_uart_device(var, config) diff --git a/components/victron/sensor.py b/components/victron/sensor.py index 2686db7..97d80a3 100644 --- a/components/victron/sensor.py +++ b/components/victron/sensor.py @@ -63,6 +63,7 @@ SENSORS = [ CONF_LOAD_CURRENT, ] + CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_VICTRON_ID): cv.use_id(VictronComponent), diff --git a/components/victron/text_sensor.py b/components/victron/text_sensor.py index 3bcaf96..db5d9dd 100644 --- a/components/victron/text_sensor.py +++ b/components/victron/text_sensor.py @@ -16,6 +16,8 @@ CONF_TRACKING_MODE = "tracking_mode" CONF_DEVICE_MODE = "device_mode" CONF_FIRMWARE_VERSION = "firmware_version" CONF_DEVICE_TYPE = "device_type" +CONF_LOAD_STATE = "load_state" +CONF_RELAY_STATE = "relay_state" TEXT_SENSORS = [ CONF_CHARGING_MODE, @@ -51,10 +53,15 @@ CONFIG_SCHEMA = cv.Schema( cv.Optional(CONF_DEVICE_TYPE): text_sensor.TEXT_SENSOR_SCHEMA.extend( {cv.GenerateID(): cv.declare_id(text_sensor.TextSensor)} ), + cv.Optional(CONF_LOAD_STATE): text_sensor.TEXT_SENSOR_SCHEMA.extend( + {cv.GenerateID(): cv.declare_id(text_sensor.TextSensor)} + ), + cv.Optional(CONF_RELAY_STATE): text_sensor.TEXT_SENSOR_SCHEMA.extend( + {cv.GenerateID(): cv.declare_id(text_sensor.TextSensor)} + ), } ) - def to_code(config): hub = yield cg.get_variable(config[CONF_VICTRON_ID]) for key in TEXT_SENSORS: diff --git a/components/victron/victron.cpp b/components/victron/victron.cpp index 362252f..2f10cb0 100644 --- a/components/victron/victron.cpp +++ b/components/victron/victron.cpp @@ -34,6 +34,8 @@ void VictronComponent::dump_config() { LOG_TEXT_SENSOR(" ", "Device Mode", device_mode_text_sensor_); LOG_TEXT_SENSOR(" ", "Firmware Version", firmware_version_text_sensor_); LOG_TEXT_SENSOR(" ", "Device Type", device_type_text_sensor_); + LOG_TEXT_SENSOR(" ", "Load state", load_state_text_sensor_); + LOG_TEXT_SENSOR(" ", "Relay state", relay_state_text_sensor_); check_uart_settings(19200); } @@ -471,6 +473,24 @@ void VictronComponent::handle_value_() { if ((device_type_text_sensor_ != nullptr) && !device_type_text_sensor_->has_state()) { device_type_text_sensor_->publish_state(device_type_text(value)); } + } else if (label_ == "LOAD") { + value = atoi(value_.c_str()); // NOLINT(cert-err34-c) + + // ESP_LOGD(TAG, "received PID: '%s'", value_.c_str()); + // value = strtol(value_.c_str(), nullptr, 0); + // ESP_LOGD(TAG, "received PID: '%04x'", value); + if ((load_state_text_sensor_ != nullptr) && !load_state_text_sensor_->has_state()) { + load_state_text_sensor_->publish_state(value); + } + } else if (label_ == "RELAY") { + value = atoi(value_.c_str()); // NOLINT(cert-err34-c) + + // ESP_LOGD(TAG, "received PID: '%s'", value_.c_str()); + // value = strtol(value_.c_str(), nullptr, 0); + // ESP_LOGD(TAG, "received PID: '%04x'", value); + if ((relay_state_text_sensor_ != nullptr) && !relay_state_text_sensor_->has_state()) { + relay_state_text_sensor_->publish_state(value); + } } } diff --git a/components/victron/victron.h b/components/victron/victron.h index a9878ca..95bfd3c 100644 --- a/components/victron/victron.h +++ b/components/victron/victron.h @@ -68,6 +68,12 @@ class VictronComponent : public uart::UARTDevice, public Component { void set_device_type_text_sensor(text_sensor::TextSensor *device_type_text_sensor) { device_type_text_sensor_ = device_type_text_sensor; } + void set_load_state_text_sensor(text_sensor::TextSensor *load_state_text_sensor) { + load_state_text_sensor_ = load_state_text_sensor; + } + void set_relay_state_text_sensor(text_sensor::TextSensor *relay_state_text_sensor) { + relay_state_text_sensor_ = relay_state_text_sensor; + } void dump_config() override; void loop() override; @@ -104,6 +110,8 @@ class VictronComponent : public uart::UARTDevice, public Component { text_sensor::TextSensor *device_mode_text_sensor_{nullptr}; text_sensor::TextSensor *firmware_version_text_sensor_{nullptr}; text_sensor::TextSensor *device_type_text_sensor_{nullptr}; + text_sensor::TextSensor *load_state_text_sensor_{nullptr}; + text_sensor::TextSensor *relay_state_text_sensor_{nullptr}; int state_{0}; std::string label_;