diff --git a/esphome/components/pzemac/pzemac.cpp b/esphome/components/pzemac/pzemac.cpp index b1a9607304..c3738d1852 100644 --- a/esphome/components/pzemac/pzemac.cpp +++ b/esphome/components/pzemac/pzemac.cpp @@ -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 &data) { @@ -73,5 +74,12 @@ void PZEMAC::dump_config() { LOG_SENSOR("", "Power Factor", this->power_factor_sensor_); } +void PZEMAC::reset_energy_() { + std::vector cmd; + cmd.push_back(this->address_); + cmd.push_back(PZEM_CMD_RESET_ENERGY); + this->send_raw(cmd); +} + } // namespace pzemac } // namespace esphome diff --git a/esphome/components/pzemac/pzemac.h b/esphome/components/pzemac/pzemac.h index 07f661535f..e9f76972a3 100644 --- a/esphome/components/pzemac/pzemac.h +++ b/esphome/components/pzemac/pzemac.h @@ -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 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 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 class ResetEnergyAction : public Action { + public: + ResetEnergyAction(PZEMAC *pzemac) : pzemac_(pzemac) {} + + void play(Ts... x) override { this->pzemac_->reset_energy_(); } + + protected: + PZEMAC *pzemac_; }; } // namespace pzemac diff --git a/esphome/components/pzemac/sensor.py b/esphome/components/pzemac/sensor.py index b6697e3d19..ab7dd3e202 100644 --- a/esphome/components/pzemac/sensor.py +++ b/esphome/components/pzemac/sensor.py @@ -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) diff --git a/tests/test3.yaml b/tests/test3.yaml index 01600ad74b..82e3a58725 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -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