mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Add an action for pzemac to reset the total energy (#2480)
This commit is contained in:
parent
f34b46a621
commit
a0ea2aae6e
4 changed files with 49 additions and 0 deletions
|
@ -7,6 +7,7 @@ namespace pzemac {
|
|||
static const char *const TAG = "pzemac";
|
||||
|
||||
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
|
||||
|
||||
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_);
|
||||
}
|
||||
|
||||
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 esphome
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/modbus/modbus.h"
|
||||
|
@ -7,6 +8,8 @@
|
|||
namespace esphome {
|
||||
namespace pzemac {
|
||||
|
||||
template<typename... Ts> class ResetEnergyAction;
|
||||
|
||||
class PZEMAC : public PollingComponent, public modbus::ModbusDevice {
|
||||
public:
|
||||
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;
|
||||
|
||||
protected:
|
||||
template<typename... Ts> friend class ResetEnergyAction;
|
||||
sensor::Sensor *voltage_sensor_;
|
||||
sensor::Sensor *current_sensor_;
|
||||
sensor::Sensor *power_sensor_;
|
||||
sensor::Sensor *energy_sensor_;
|
||||
sensor::Sensor *frequency_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
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import esphome.codegen as cg
|
||||
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.const import (
|
||||
CONF_CURRENT,
|
||||
|
@ -29,6 +31,9 @@ AUTO_LOAD = ["modbus"]
|
|||
pzemac_ns = cg.esphome_ns.namespace("pzemac")
|
||||
PZEMAC = pzemac_ns.class_("PZEMAC", cg.PollingComponent, modbus.ModbusDevice)
|
||||
|
||||
# Actions
|
||||
ResetEnergyAction = pzemac_ns.class_("ResetEnergyAction", automation.Action)
|
||||
|
||||
CONFIG_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):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
|
|
@ -478,6 +478,7 @@ sensor:
|
|||
power:
|
||||
name: 'PZEM004T Power'
|
||||
- platform: pzemac
|
||||
id: pzemac1
|
||||
voltage:
|
||||
name: 'PZEMAC Voltage'
|
||||
current:
|
||||
|
@ -776,6 +777,11 @@ binary_sensor:
|
|||
on_press:
|
||||
then:
|
||||
- cover.toggle: time_based_cover
|
||||
- platform: template
|
||||
id: 'pzemac_reset_energy'
|
||||
on_press:
|
||||
then:
|
||||
- pzemac.reset_energy: pzemac1
|
||||
|
||||
globals:
|
||||
- id: my_global_string
|
||||
|
|
Loading…
Reference in a new issue