Skip to content

Commit

Permalink
BREAKING: Rename operation_mode to operation_status (Closes: #47) (
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Feb 5, 2023
1 parent da18d10 commit 0f8dbc3
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 113 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ sensor:
name: "${name} power demand"

- platform: soyosource_inverter
operation_mode_id:
name: "${name} operation mode id"
operation_status_id:
name: "${name} operation status id"
battery_voltage:
name: "${name} battery voltage"
battery_current:
Expand Down Expand Up @@ -180,8 +180,8 @@ sensor:

text_sensor:
- platform: soyosource_inverter
operation_mode:
name: "${name} operation mode"
operation_status:
name: "${name} operation status"
```
For a more advanced setup take a look at the [esp32-multiple-uarts-example.yaml](esp32-multiple-uarts-example.yaml).
Expand Down
10 changes: 5 additions & 5 deletions components/soyosource_inverter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
CONF_BATTERY_POWER = "battery_power"
CONF_AC_VOLTAGE = "ac_voltage"
CONF_AC_FREQUENCY = "ac_frequency"
CONF_OPERATION_MODE_ID = "operation_mode_id"
CONF_OPERATION_STATUS_ID = "operation_status_id"

ICON_OPERATION_MODE = "mdi:heart-pulse"
ICON_OPERATION_STATUS = "mdi:heart-pulse"

SENSORS = [
CONF_BATTERY_VOLTAGE,
Expand All @@ -41,16 +41,16 @@
CONF_AC_VOLTAGE,
CONF_AC_FREQUENCY,
CONF_TEMPERATURE,
CONF_OPERATION_MODE_ID,
CONF_OPERATION_STATUS_ID,
]

# pylint: disable=too-many-function-args
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_SOYOSOURCE_INVERTER_ID): cv.use_id(SoyosourceInverter),
cv.Optional(CONF_OPERATION_MODE_ID): sensor.sensor_schema(
cv.Optional(CONF_OPERATION_STATUS_ID): sensor.sensor_schema(
unit_of_measurement=UNIT_EMPTY,
icon=ICON_OPERATION_MODE,
icon=ICON_OPERATION_STATUS,
accuracy_decimals=0,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
Expand Down
26 changes: 13 additions & 13 deletions components/soyosource_inverter/soyosource_inverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace soyosource_inverter {

static const char *const TAG = "soyosource_inverter";

static const uint8_t OPERATION_MODES_SIZE = 13;
static const char *const OPERATION_MODES[OPERATION_MODES_SIZE] = {
static const uint8_t OPERATION_STATUS_SIZE = 13;
static const char *const OPERATION_STATUS[OPERATION_STATUS_SIZE] = {
"Normal", // 0x00
"Startup", // 0x01
"Standby", // 0x02
Expand Down Expand Up @@ -35,8 +35,8 @@ void SoyosourceInverter::on_soyosource_modbus_data(const std::vector<uint8_t> &d
return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0);
};

uint8_t raw_operation_mode = data[0];
float operation_mode_id = (float) raw_operation_mode;
uint8_t raw_operation_status = data[0];
float operation_status_id = (float) raw_operation_status;

uint16_t raw_battery_voltage = soyosource_get_16bit(1);
float battery_voltage = raw_battery_voltage * 0.1f;
Expand Down Expand Up @@ -69,14 +69,14 @@ void SoyosourceInverter::on_soyosource_modbus_data(const std::vector<uint8_t> &d
// Data bytes: 0 1 2 3 4 5 6 7 8 9 CRC
//
//
// Data byte 0: 0x04 (Operation mode) 0x00 (Normal), 0x01 (Startup), 0x02 (Standby),
// Data byte 0: 0x04 (Operation status) 0x00 (Normal), 0x01 (Startup), 0x02 (Standby),
// 0x03 (Startup aborted?), 0x04 (Error or battery mode?)
// Data byte 1...2: 0x01 0xDB (Battery voltage)
// Data byte 3...4: 0x00 0xA1 (Battery current)
// Data byte 5...6: 0x00 0xDD (AC voltage)
// Data byte 7: 0x64 (AC frequency / 2)
// Data byte 8...9: 0x02 0xCA (Temperature / 10.0 + 2 * 10)
this->publish_state_(this->operation_mode_id_sensor_, operation_mode_id);
this->publish_state_(this->operation_status_id_sensor_, operation_status_id);
this->publish_state_(this->battery_voltage_sensor_, battery_voltage);
this->publish_state_(this->battery_current_sensor_, battery_current);
this->publish_state_(this->battery_power_sensor_, battery_power);
Expand All @@ -85,17 +85,17 @@ void SoyosourceInverter::on_soyosource_modbus_data(const std::vector<uint8_t> &d
this->publish_state_(this->temperature_sensor_, temperature);
this->publish_state_(this->fan_running_binary_sensor_, (bool) (temperature >= 45.0));

if (raw_operation_mode < OPERATION_MODES_SIZE) {
this->publish_state_(this->operation_mode_text_sensor_, OPERATION_MODES[raw_operation_mode]);
if (raw_operation_status < OPERATION_STATUS_SIZE) {
this->publish_state_(this->operation_status_text_sensor_, OPERATION_STATUS[raw_operation_status]);
} else {
this->publish_state_(this->operation_mode_text_sensor_, "Unknown");
this->publish_state_(this->operation_status_text_sensor_, "Unknown");
}

this->no_response_count_ = 0;
}

void SoyosourceInverter::publish_device_offline_() {
this->publish_state_(this->operation_mode_id_sensor_, -1);
this->publish_state_(this->operation_status_id_sensor_, -1);
this->publish_state_(this->battery_voltage_sensor_, NAN);
this->publish_state_(this->battery_current_sensor_, NAN);
this->publish_state_(this->battery_power_sensor_, NAN);
Expand All @@ -104,7 +104,7 @@ void SoyosourceInverter::publish_device_offline_() {
this->publish_state_(this->temperature_sensor_, NAN);
// this->publish_state_(this->fan_running_binary_sensor_, NAN);

this->publish_state_(this->operation_mode_text_sensor_, "Offline");
this->publish_state_(this->operation_status_text_sensor_, "Offline");
}

void SoyosourceInverter::update() {
Expand Down Expand Up @@ -143,8 +143,8 @@ void SoyosourceInverter::publish_state_(text_sensor::TextSensor *text_sensor, co
void SoyosourceInverter::dump_config() {
ESP_LOGCONFIG(TAG, "SoyosourceInverter:");
ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
LOG_SENSOR("", "Operation Mode ID", this->operation_mode_id_sensor_);
LOG_TEXT_SENSOR("", "Operation Mode", this->operation_mode_text_sensor_);
LOG_SENSOR("", "Operation Status ID", this->operation_status_id_sensor_);
LOG_TEXT_SENSOR("", "Operation Status", this->operation_status_text_sensor_);
LOG_SENSOR("", "Battery Voltage", this->battery_voltage_sensor_);
LOG_SENSOR("", "Battery Current", this->battery_current_sensor_);
LOG_SENSOR("", "Battery Power", this->battery_power_sensor_);
Expand Down
12 changes: 6 additions & 6 deletions components/soyosource_inverter/soyosource_inverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class SoyosourceInverter : public PollingComponent, public soyosource_modbus::So
fan_running_binary_sensor_ = fan_running_binary_sensor;
}

void set_operation_mode_id_sensor(sensor::Sensor *operation_mode_id_sensor) {
operation_mode_id_sensor_ = operation_mode_id_sensor;
void set_operation_status_id_sensor(sensor::Sensor *operation_status_id_sensor) {
operation_status_id_sensor_ = operation_status_id_sensor;
}
void set_battery_voltage_sensor(sensor::Sensor *battery_voltage_sensor) {
battery_voltage_sensor_ = battery_voltage_sensor;
Expand All @@ -31,8 +31,8 @@ class SoyosourceInverter : public PollingComponent, public soyosource_modbus::So
void set_ac_frequency_sensor(sensor::Sensor *ac_frequency_sensor) { ac_frequency_sensor_ = ac_frequency_sensor; }
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }

void set_operation_mode_text_sensor(text_sensor::TextSensor *operation_mode_text_sensor) {
operation_mode_text_sensor_ = operation_mode_text_sensor;
void set_operation_status_text_sensor(text_sensor::TextSensor *operation_status_text_sensor) {
operation_status_text_sensor_ = operation_status_text_sensor;
}

void update() override;
Expand All @@ -44,15 +44,15 @@ class SoyosourceInverter : public PollingComponent, public soyosource_modbus::So
protected:
binary_sensor::BinarySensor *fan_running_binary_sensor_;

sensor::Sensor *operation_mode_id_sensor_;
sensor::Sensor *operation_status_id_sensor_;
sensor::Sensor *battery_voltage_sensor_;
sensor::Sensor *battery_current_sensor_;
sensor::Sensor *battery_power_sensor_;
sensor::Sensor *ac_voltage_sensor_;
sensor::Sensor *ac_frequency_sensor_;
sensor::Sensor *temperature_sensor_;

text_sensor::TextSensor *operation_mode_text_sensor_;
text_sensor::TextSensor *operation_status_text_sensor_;

uint8_t no_response_count_ = 0;

Expand Down
10 changes: 5 additions & 5 deletions components/soyosource_inverter/text_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

CODEOWNERS = ["@syssi"]

CONF_OPERATION_MODE = "operation_mode"
CONF_OPERATION_STATUS = "operation_status"

ICON_OPERATION_MODE = "mdi:heart-pulse"
ICON_OPERATION_STATUS = "mdi:heart-pulse"

TEXT_SENSORS = [
CONF_OPERATION_MODE,
CONF_OPERATION_STATUS,
]

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_SOYOSOURCE_INVERTER_ID): cv.use_id(SoyosourceInverter),
cv.Optional(CONF_OPERATION_MODE): text_sensor.TEXT_SENSOR_SCHEMA.extend(
cv.Optional(CONF_OPERATION_STATUS): text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(text_sensor.TextSensor),
cv.Optional(CONF_ICON, default=ICON_OPERATION_MODE): cv.icon,
cv.Optional(CONF_ICON, default=ICON_OPERATION_STATUS): cv.icon,
}
),
}
Expand Down
12 changes: 7 additions & 5 deletions components/soyosource_virtual_meter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
CONF_MAX_POWER_DEMAND = "max_power_demand"
CONF_BUFFER = "buffer"
CONF_POWER_DEMAND_DIVIDER = "power_demand_divider"
CONF_OPERATION_MODE_ID = "operation_mode_id"
CONF_OPERATION_STATUS_ID = "operation_status_id"

DEFAULT_BUFFER = 0
DEFAULT_MIN_POWER_DEMAND = 0
Expand Down Expand Up @@ -56,7 +56,7 @@ def validate_min_max(config):
CONF_POWER_DEMAND_CALCULATION, default="DUMB_OEM_BEHAVIOR"
): cv.enum(POWER_DEMAND_CALCULATION_OPTIONS, upper=True),
cv.Required(CONF_POWER_ID): cv.use_id(sensor.Sensor),
cv.Optional(CONF_OPERATION_MODE_ID): cv.use_id(sensor.Sensor),
cv.Optional(CONF_OPERATION_STATUS_ID): cv.use_id(sensor.Sensor),
cv.Optional(
CONF_POWER_SENSOR_INACTIVITY_TIMEOUT, default="20s"
): cv.positive_time_period_seconds,
Expand Down Expand Up @@ -99,6 +99,8 @@ async def to_code(config):
)
cg.add(var.set_power_demand_calculation(config[CONF_POWER_DEMAND_CALCULATION]))

if CONF_OPERATION_MODE_ID in config:
operation_mode_sensor = await cg.get_variable(config[CONF_OPERATION_MODE_ID])
cg.add(var.set_operation_mode_sensor(operation_mode_sensor))
if CONF_OPERATION_STATUS_ID in config:
operation_status_sensor = await cg.get_variable(
config[CONF_OPERATION_STATUS_ID]
)
cg.add(var.set_operation_status_sensor(operation_status_sensor))
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void SoyosourceVirtualMeter::update() {
}
}

// Override power demand and operation mode if the operation mode of the inverter isn't "normal" (optional!)
if (this->operation_mode_sensor_ != nullptr && this->operation_mode_sensor_->state != 0x0) {
// Override power demand and operation mode if the operation status of the inverter isn't "normal" (optional!)
if (this->operation_status_sensor_ != nullptr && this->operation_status_sensor_->state != 0x0) {
power_demand = 0;
operation_mode = "Standby";
ESP_LOGD(TAG, "'%s': The operation mode of the inverter isn't 0x0 (normal). Suspending the limiter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ enum PowerDemandCalculation {
class SoyosourceVirtualMeter : public PollingComponent, public soyosource_modbus::SoyosourceModbusDevice {
public:
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
void set_operation_mode_sensor(sensor::Sensor *operation_mode_sensor) {
operation_mode_sensor_ = operation_mode_sensor;
void set_operation_status_sensor(sensor::Sensor *operation_status_sensor) {
operation_status_sensor_ = operation_status_sensor;
}
void set_power_demand_sensor(sensor::Sensor *power_demand_sensor) { power_demand_sensor_ = power_demand_sensor; }
void set_buffer(int16_t buffer) { this->buffer_ = buffer; }
Expand Down Expand Up @@ -66,7 +66,7 @@ class SoyosourceVirtualMeter : public PollingComponent, public soyosource_modbus
number::Number *max_power_demand_number_;

sensor::Sensor *power_sensor_;
sensor::Sensor *operation_mode_sensor_;
sensor::Sensor *operation_status_sensor_;
sensor::Sensor *power_demand_sensor_;

switch_::Switch *manual_mode_switch_;
Expand Down
20 changes: 10 additions & 10 deletions esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ esphome:
comment: ${device_description}
project:
name: "syssi.esphome-soyosource-gtn-virtual-meter"
version: 1.2.0
version: 2.0.0

esp32:
board: esp-wrover-kit
Expand Down Expand Up @@ -71,9 +71,9 @@ soyosource_virtual_meter:
# A positive buffer value (10) tries to avoid exporting power to the grid (demand - 10 watts)
# A negative buffer value (-10) exports power to the grid (demand + 10 watts)
buffer: 10
# The operation_mode_id sensor is expected here. Passing the operation_mode won't work
# The state is used to suspend the limiter if the operation mode of the inverter isn't 0x0 (normal)
# operation_mode_id: operation_mode_id0
# The operation_status_id sensor is expected here. Passing the operation_status won't work
# The state is used to suspend the limiter if the operation status of the inverter isn't 0x0 (normal)
# operation_status_id: operation_status_id0

# The update interval is important and defaults to 3 seconds. If the demand is sent too frequently
# or rarely the interverter stops. TODO: Identify and validate the lower and upper update limit
Expand Down Expand Up @@ -109,9 +109,9 @@ sensor:

- platform: soyosource_inverter
soyosource_inverter_id: inverter0
operation_mode_id:
name: "${name} operation mode id"
id: operation_mode_id0
operation_status_id:
name: "${name} operation status id"
id: operation_status_id0
battery_voltage:
name: "${name} battery voltage"
battery_current:
Expand Down Expand Up @@ -159,10 +159,10 @@ switch:
text_sensor:
- platform: soyosource_inverter
soyosource_inverter_id: inverter0
operation_mode:
name: "${name} operation mode"
operation_status:
name: "${name} operation status"

- platform: soyosource_virtual_meter
soyosource_virtual_meter_id: virtualmeter0
operation_mode:
name: "${name} operation mode"
name: "${name} limiter operation mode"
38 changes: 19 additions & 19 deletions esp32-multiple-uarts-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ esphome:
comment: ${device_description}
project:
name: "syssi.esphome-soyosource-gtn-virtual-meter"
version: 1.2.0
version: 2.0.0

esp32:
board: esp-wrover-kit
Expand Down Expand Up @@ -85,9 +85,9 @@ soyosource_virtual_meter:
# A positive buffer value (10) tries to avoid exporting power to the grid (demand - 10 watts)
# A negative buffer value (-10) exports power to the grid (demand + 10 watts)
buffer: 10
# The operation_mode_id sensor is expected here. Passing the operation_mode won't work
# The state is used to suspend the limiter if the operation mode of the inverter isn't 0x0 (normal)
# operation_mode_id: operation_mode_id0
# The operation_status_id sensor is expected here. Passing the operation_status won't work
# The state is used to suspend the limiter if the operation status of the inverter isn't 0x0 (normal)
# operation_status_id: operation_status_id0

- id: virtualmeter1
soyosource_modbus_id: modbus1
Expand All @@ -101,9 +101,9 @@ soyosource_virtual_meter:
# A positive buffer value (10) tries to avoid exporting power to the grid (demand - 10 watts)
# A negative buffer value (-10) exports power to the grid (demand + 10 watts)
buffer: 10
# The operation_mode_id sensor is expected here. Passing the operation_mode won't work
# The state is used to suspend the limiter if the operation mode of the inverter isn't 0x0 (normal)
# operation_mode_id: operation_mode_id1
# The operation_status_id sensor is expected here. Passing the operation_status won't work
# The state is used to suspend the limiter if the operation status of the inverter isn't 0x0 (normal)
# operation_status_id: operation_status_id1

binary_sensor:
- platform: soyosource_inverter
Expand Down Expand Up @@ -150,9 +150,9 @@ sensor:

- platform: soyosource_inverter
soyosource_inverter_id: inverter0
operation_mode_id:
name: "${device0} operation mode id"
id: operation_mode_id0
operation_status_id:
name: "${device0} operation status id"
id: operation_status_id0
battery_voltage:
name: "${device0} battery voltage"
battery_current:
Expand All @@ -167,9 +167,9 @@ sensor:
name: "${device0} temperature"
- platform: soyosource_inverter
soyosource_inverter_id: inverter1
operation_mode_id:
name: "${device1} operation mode id"
id: operation_mode_id1
operation_status_id:
name: "${device1} operation status id"
id: operation_status_id1
battery_voltage:
name: "${device1} battery voltage"
battery_current:
Expand Down Expand Up @@ -231,18 +231,18 @@ switch:
text_sensor:
- platform: soyosource_inverter
soyosource_inverter_id: inverter0
operation_mode:
name: "${device0} operation mode"
operation_status:
name: "${device0} operation status"
- platform: soyosource_inverter
soyosource_inverter_id: inverter1
operation_mode:
name: "${device1} operation mode"
operation_status:
name: "${device1} operation status"

- platform: soyosource_virtual_meter
soyosource_virtual_meter_id: virtualmeter0
operation_mode:
name: "${device0} operation mode"
name: "${device0} limiter operation mode"
- platform: soyosource_virtual_meter
soyosource_virtual_meter_id: virtualmeter1
operation_mode:
name: "${device1} operation mode"
name: "${device1} limiter operation mode"
Loading

0 comments on commit 0f8dbc3

Please sign in to comment.