mirror of
https://github.com/esphome/esphome.git
synced 2026-09-29 07:50:21 +00:00
[pmwcs3] Use register_apply_action for the calibration and address actions (#19666)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
@@ -32,35 +31,4 @@ class PMWCS3Component final : public PollingComponent, public i2c::I2CDevice {
|
||||
sensor::Sensor *vwc_sensor_{nullptr};
|
||||
};
|
||||
|
||||
template<typename... Ts> class PMWCS3AirCalibrationAction final : public Action<Ts...> {
|
||||
public:
|
||||
PMWCS3AirCalibrationAction(PMWCS3Component *parent) : parent_(parent) {}
|
||||
|
||||
void play(const Ts &...x) override { this->parent_->air_calibration(); }
|
||||
|
||||
protected:
|
||||
PMWCS3Component *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class PMWCS3WaterCalibrationAction final : public Action<Ts...> {
|
||||
public:
|
||||
PMWCS3WaterCalibrationAction(PMWCS3Component *parent) : parent_(parent) {}
|
||||
|
||||
void play(const Ts &...x) override { this->parent_->water_calibration(); }
|
||||
|
||||
protected:
|
||||
PMWCS3Component *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class PMWCS3NewI2cAddressAction final : public Action<Ts...> {
|
||||
public:
|
||||
PMWCS3NewI2cAddressAction(PMWCS3Component *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(int, new_address)
|
||||
|
||||
void play(const Ts &...x) override { this->parent_->new_i2c_address(this->new_address_.value(x...)); }
|
||||
|
||||
protected:
|
||||
PMWCS3Component *parent_;
|
||||
};
|
||||
|
||||
} // namespace esphome::pmwcs3
|
||||
|
||||
@@ -10,8 +10,6 @@ from esphome.const import (
|
||||
ICON_THERMOMETER,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
)
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.types import ConfigType
|
||||
|
||||
CODEOWNERS = ["@SeByDocKy"]
|
||||
@@ -29,17 +27,6 @@ PMWCS3Component = pmwcs3_ns.class_(
|
||||
"PMWCS3Component", cg.PollingComponent, i2c.I2CDevice
|
||||
)
|
||||
|
||||
# Actions
|
||||
PMWCS3AirCalibrationAction = pmwcs3_ns.class_(
|
||||
"PMWCS3AirCalibrationAction", automation.Action
|
||||
)
|
||||
PMWCS3WaterCalibrationAction = pmwcs3_ns.class_(
|
||||
"PMWCS3WaterCalibrationAction", automation.Action
|
||||
)
|
||||
PMWCS3NewI2cAddressAction = pmwcs3_ns.class_(
|
||||
"PMWCS3NewI2cAddressAction", automation.Action
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
@@ -104,23 +91,18 @@ PMWCS3_CALIBRATION_SCHEMA = cv.Schema(
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
automation.register_simple_action(
|
||||
automation.register_apply_action(
|
||||
"pmwcs3.air_calibration",
|
||||
PMWCS3AirCalibrationAction,
|
||||
PMWCS3_CALIBRATION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("air_calibration()"),
|
||||
)
|
||||
|
||||
|
||||
automation.register_simple_action(
|
||||
automation.register_apply_action(
|
||||
"pmwcs3.water_calibration",
|
||||
PMWCS3WaterCalibrationAction,
|
||||
PMWCS3_CALIBRATION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("water_calibration()"),
|
||||
)
|
||||
|
||||
|
||||
PMWCS3_NEW_I2C_ADDRESS_SCHEMA = cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(PMWCS3Component),
|
||||
@@ -129,21 +111,8 @@ PMWCS3_NEW_I2C_ADDRESS_SCHEMA = cv.maybe_simple_value(
|
||||
key=CONF_ADDRESS,
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"pmwcs3.new_i2c_address",
|
||||
PMWCS3NewI2cAddressAction,
|
||||
PMWCS3_NEW_I2C_ADDRESS_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_ADDRESS, "new_i2c_address", cg.uint8),
|
||||
)
|
||||
async def pmwcs3newi2caddress_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
parent = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, parent)
|
||||
address = await cg.templatable(config[CONF_ADDRESS], args, cg.int_)
|
||||
cg.add(var.set_new_address(address))
|
||||
return var
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
button:
|
||||
- platform: template
|
||||
name: PMWCS3 Actions
|
||||
on_press:
|
||||
- pmwcs3.air_calibration:
|
||||
- pmwcs3.water_calibration:
|
||||
- pmwcs3.new_i2c_address: 0x64
|
||||
- pmwcs3.new_i2c_address:
|
||||
address: !lambda return 0x65;
|
||||
@@ -0,0 +1,4 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
|
||||
pmwcs3: !include common.yaml
|
||||
actions: !include common-actions.yaml
|
||||
Reference in New Issue
Block a user