mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 15:08:10 +01:00
Added pzemdc reset energy action (#4481)
* remove unused sensors on pzemdc * add reset energy action for pzemdc * fix lint errors * remove trailing space on pzemdc.h * make method reset_energy of pzemdc public * Apply suggestions from code review --------- Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
600f4be2c4
commit
1087cb55b4
4 changed files with 45 additions and 4 deletions
|
@ -7,6 +7,7 @@ namespace pzemdc {
|
||||||
static const char *const TAG = "pzemdc";
|
static const char *const TAG = "pzemdc";
|
||||||
|
|
||||||
static const uint8_t PZEM_CMD_READ_IN_REGISTERS = 0x04;
|
static const uint8_t PZEM_CMD_READ_IN_REGISTERS = 0x04;
|
||||||
|
static const uint8_t PZEM_CMD_RESET_ENERGY = 0x42;
|
||||||
static const uint8_t PZEM_REGISTER_COUNT = 10; // 10x 16-bit registers
|
static const uint8_t PZEM_REGISTER_COUNT = 10; // 10x 16-bit registers
|
||||||
|
|
||||||
void PZEMDC::on_modbus_data(const std::vector<uint8_t> &data) {
|
void PZEMDC::on_modbus_data(const std::vector<uint8_t> &data) {
|
||||||
|
@ -61,5 +62,12 @@ void PZEMDC::dump_config() {
|
||||||
LOG_SENSOR("", "Energy", this->energy_sensor_);
|
LOG_SENSOR("", "Energy", this->energy_sensor_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PZEMDC::reset_energy() {
|
||||||
|
std::vector<uint8_t> cmd;
|
||||||
|
cmd.push_back(this->address_);
|
||||||
|
cmd.push_back(PZEM_CMD_RESET_ENERGY);
|
||||||
|
this->send_raw(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace pzemdc
|
} // namespace pzemdc
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -15,8 +15,6 @@ class PZEMDC : public PollingComponent, public modbus::ModbusDevice {
|
||||||
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
|
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
|
||||||
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
|
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
|
||||||
void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
|
void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
|
||||||
void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; }
|
|
||||||
void set_powerfactor_sensor(sensor::Sensor *powerfactor_sensor) { power_factor_sensor_ = powerfactor_sensor; }
|
|
||||||
|
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|
||||||
|
@ -24,14 +22,24 @@ class PZEMDC : public PollingComponent, public modbus::ModbusDevice {
|
||||||
|
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
|
void reset_energy();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
sensor::Sensor *voltage_sensor_{nullptr};
|
sensor::Sensor *voltage_sensor_{nullptr};
|
||||||
sensor::Sensor *current_sensor_{nullptr};
|
sensor::Sensor *current_sensor_{nullptr};
|
||||||
sensor::Sensor *power_sensor_{nullptr};
|
sensor::Sensor *power_sensor_{nullptr};
|
||||||
sensor::Sensor *frequency_sensor_{nullptr};
|
|
||||||
sensor::Sensor *power_factor_sensor_{nullptr};
|
|
||||||
sensor::Sensor *energy_sensor_{nullptr};
|
sensor::Sensor *energy_sensor_{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class ResetEnergyAction : public Action<Ts...> {
|
||||||
|
public:
|
||||||
|
ResetEnergyAction(PZEMDC *pzemdc) : pzemdc_(pzemdc) {}
|
||||||
|
|
||||||
|
void play(Ts... x) override { this->pzemdc_->reset_energy(); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
PZEMDC *pzemdc_;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace pzemdc
|
} // namespace pzemdc
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
|
from esphome import automation
|
||||||
|
from esphome.automation import maybe_simple_id
|
||||||
from esphome.components import sensor, modbus
|
from esphome.components import sensor, modbus
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
CONF_CURRENT,
|
CONF_CURRENT,
|
||||||
|
@ -24,6 +26,9 @@ AUTO_LOAD = ["modbus"]
|
||||||
pzemdc_ns = cg.esphome_ns.namespace("pzemdc")
|
pzemdc_ns = cg.esphome_ns.namespace("pzemdc")
|
||||||
PZEMDC = pzemdc_ns.class_("PZEMDC", cg.PollingComponent, modbus.ModbusDevice)
|
PZEMDC = pzemdc_ns.class_("PZEMDC", cg.PollingComponent, modbus.ModbusDevice)
|
||||||
|
|
||||||
|
# Actions
|
||||||
|
ResetEnergyAction = pzemdc_ns.class_("ResetEnergyAction", automation.Action)
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
|
@ -59,6 +64,20 @@ CONFIG_SCHEMA = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action(
|
||||||
|
"pzemdc.reset_energy",
|
||||||
|
ResetEnergyAction,
|
||||||
|
maybe_simple_id(
|
||||||
|
{
|
||||||
|
cv.GenerateID(CONF_ID): cv.use_id(PZEMDC),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
)
|
||||||
|
async def reset_energy_to_code(config, action_id, template_arg, args):
|
||||||
|
paren = await cg.get_variable(config[CONF_ID])
|
||||||
|
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
|
|
@ -579,6 +579,7 @@ sensor:
|
||||||
power_factor:
|
power_factor:
|
||||||
name: PZEMAC Power Factor
|
name: PZEMAC Power Factor
|
||||||
- platform: pzemdc
|
- platform: pzemdc
|
||||||
|
id: pzemdc1
|
||||||
voltage:
|
voltage:
|
||||||
name: PZEMDC Voltage
|
name: PZEMDC Voltage
|
||||||
current:
|
current:
|
||||||
|
@ -925,6 +926,11 @@ binary_sensor:
|
||||||
on_press:
|
on_press:
|
||||||
then:
|
then:
|
||||||
- pzemac.reset_energy: pzemac1
|
- pzemac.reset_energy: pzemac1
|
||||||
|
- platform: template
|
||||||
|
id: pzemdc_reset_energy
|
||||||
|
on_press:
|
||||||
|
then:
|
||||||
|
- pzemdc.reset_energy: pzemdc1
|
||||||
|
|
||||||
- platform: vbus
|
- platform: vbus
|
||||||
model: deltasol_bs_plus
|
model: deltasol_bs_plus
|
||||||
|
|
Loading…
Reference in a new issue