Add an action for pzemac to reset the total energy (#2480)

This commit is contained in:
Chris Nussbaum 2022-01-10 04:13:39 -06:00 committed by GitHub
parent f34b46a621
commit a0ea2aae6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 0 deletions

View file

@ -7,6 +7,7 @@ namespace pzemac {
static const char *const TAG = "pzemac"; static const char *const TAG = "pzemac";
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 PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) { void PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) {
@ -73,5 +74,12 @@ void PZEMAC::dump_config() {
LOG_SENSOR("", "Power Factor", this->power_factor_sensor_); LOG_SENSOR("", "Power Factor", this->power_factor_sensor_);
} }
void PZEMAC::reset_energy_() {
std::vector<uint8_t> cmd;
cmd.push_back(this->address_);
cmd.push_back(PZEM_CMD_RESET_ENERGY);
this->send_raw(cmd);
}
} // namespace pzemac } // namespace pzemac
} // namespace esphome } // namespace esphome

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/components/modbus/modbus.h" #include "esphome/components/modbus/modbus.h"
@ -7,6 +8,8 @@
namespace esphome { namespace esphome {
namespace pzemac { namespace pzemac {
template<typename... Ts> class ResetEnergyAction;
class PZEMAC : public PollingComponent, public modbus::ModbusDevice { class PZEMAC : public PollingComponent, public modbus::ModbusDevice {
public: public:
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
@ -23,12 +26,25 @@ class PZEMAC : public PollingComponent, public modbus::ModbusDevice {
void dump_config() override; void dump_config() override;
protected: protected:
template<typename... Ts> friend class ResetEnergyAction;
sensor::Sensor *voltage_sensor_; sensor::Sensor *voltage_sensor_;
sensor::Sensor *current_sensor_; sensor::Sensor *current_sensor_;
sensor::Sensor *power_sensor_; sensor::Sensor *power_sensor_;
sensor::Sensor *energy_sensor_; sensor::Sensor *energy_sensor_;
sensor::Sensor *frequency_sensor_; sensor::Sensor *frequency_sensor_;
sensor::Sensor *power_factor_sensor_; sensor::Sensor *power_factor_sensor_;
void reset_energy_();
};
template<typename... Ts> class ResetEnergyAction : public Action<Ts...> {
public:
ResetEnergyAction(PZEMAC *pzemac) : pzemac_(pzemac) {}
void play(Ts... x) override { this->pzemac_->reset_energy_(); }
protected:
PZEMAC *pzemac_;
}; };
} // namespace pzemac } // namespace pzemac

View file

@ -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,
@ -29,6 +31,9 @@ AUTO_LOAD = ["modbus"]
pzemac_ns = cg.esphome_ns.namespace("pzemac") pzemac_ns = cg.esphome_ns.namespace("pzemac")
PZEMAC = pzemac_ns.class_("PZEMAC", cg.PollingComponent, modbus.ModbusDevice) PZEMAC = pzemac_ns.class_("PZEMAC", cg.PollingComponent, modbus.ModbusDevice)
# Actions
ResetEnergyAction = pzemac_ns.class_("ResetEnergyAction", automation.Action)
CONFIG_SCHEMA = ( CONFIG_SCHEMA = (
cv.Schema( cv.Schema(
{ {
@ -75,6 +80,20 @@ CONFIG_SCHEMA = (
) )
@automation.register_action(
"pzemac.reset_energy",
ResetEnergyAction,
maybe_simple_id(
{
cv.Required(CONF_ID): cv.use_id(PZEMAC),
}
),
)
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)

View file

@ -478,6 +478,7 @@ sensor:
power: power:
name: 'PZEM004T Power' name: 'PZEM004T Power'
- platform: pzemac - platform: pzemac
id: pzemac1
voltage: voltage:
name: 'PZEMAC Voltage' name: 'PZEMAC Voltage'
current: current:
@ -776,6 +777,11 @@ binary_sensor:
on_press: on_press:
then: then:
- cover.toggle: time_based_cover - cover.toggle: time_based_cover
- platform: template
id: 'pzemac_reset_energy'
on_press:
then:
- pzemac.reset_energy: pzemac1
globals: globals:
- id: my_global_string - id: my_global_string