mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 23:40:25 +00:00
[mhz19] Use register_apply_action for the four mhz19 actions (#19631)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
|
||||
@@ -49,27 +48,4 @@ class MHZ19Component final : public PollingComponent, public uart::UARTDevice {
|
||||
MHZ19DetectionRange detection_range_{MHZ19_DETECTION_RANGE_DEFAULT};
|
||||
};
|
||||
|
||||
template<typename... Ts> class MHZ19CalibrateZeroAction final : public Action<Ts...>, public Parented<MHZ19Component> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->calibrate_zero(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class MHZ19ABCEnableAction final : public Action<Ts...>, public Parented<MHZ19Component> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->abc_enable(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class MHZ19ABCDisableAction final : public Action<Ts...>, public Parented<MHZ19Component> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->abc_disable(); }
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
class MHZ19DetectionRangeSetAction final : public Action<Ts...>, public Parented<MHZ19Component> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(MHZ19DetectionRange, detection_range)
|
||||
|
||||
void play(const Ts &...x) override { this->parent_->range_set(this->detection_range_.value(x...)); }
|
||||
};
|
||||
|
||||
} // namespace esphome::mhz19
|
||||
|
||||
@@ -15,8 +15,6 @@ from esphome.const import (
|
||||
UNIT_CELSIUS,
|
||||
UNIT_PARTS_PER_MILLION,
|
||||
)
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.types import ConfigType
|
||||
|
||||
DEPENDENCIES = ["uart"]
|
||||
@@ -26,21 +24,6 @@ CONF_DETECTION_RANGE = "detection_range"
|
||||
|
||||
mhz19_ns = cg.esphome_ns.namespace("mhz19")
|
||||
MHZ19Component = mhz19_ns.class_("MHZ19Component", cg.PollingComponent, uart.UARTDevice)
|
||||
MHZ19CalibrateZeroAction = mhz19_ns.class_(
|
||||
"MHZ19CalibrateZeroAction", automation.Action, cg.Parented.template(MHZ19Component)
|
||||
)
|
||||
MHZ19ABCEnableAction = mhz19_ns.class_(
|
||||
"MHZ19ABCEnableAction", automation.Action, cg.Parented.template(MHZ19Component)
|
||||
)
|
||||
MHZ19ABCDisableAction = mhz19_ns.class_(
|
||||
"MHZ19ABCDisableAction", automation.Action, cg.Parented.template(MHZ19Component)
|
||||
)
|
||||
MHZ19DetectionRangeSetAction = mhz19_ns.class_(
|
||||
"MHZ19DetectionRangeSetAction",
|
||||
automation.Action,
|
||||
cg.Parented.template(MHZ19Component),
|
||||
)
|
||||
|
||||
mhz19_detection_range = mhz19_ns.enum("MHZ19DetectionRange")
|
||||
MHZ19_DETECTION_RANGE_ENUM = {
|
||||
2000: mhz19_detection_range.MHZ19_DETECTION_RANGE_0_2000PPM,
|
||||
@@ -122,28 +105,14 @@ NO_ARGS_ACTION_SCHEMA = maybe_simple_id(
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"mhz19.calibrate_zero",
|
||||
MHZ19CalibrateZeroAction,
|
||||
NO_ARGS_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"mhz19.abc_enable",
|
||||
MHZ19ABCEnableAction,
|
||||
NO_ARGS_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"mhz19.abc_disable",
|
||||
MHZ19ABCDisableAction,
|
||||
NO_ARGS_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
for _name, _call in (
|
||||
("mhz19.calibrate_zero", "calibrate_zero()"),
|
||||
("mhz19.abc_enable", "abc_enable()"),
|
||||
("mhz19.abc_disable", "abc_disable()"),
|
||||
):
|
||||
automation.register_apply_action(
|
||||
_name, NO_ARGS_ACTION_SCHEMA, automation.ApplyCall(_call)
|
||||
)
|
||||
|
||||
|
||||
RANGE_ACTION_SCHEMA = maybe_simple_id(
|
||||
@@ -156,21 +125,8 @@ RANGE_ACTION_SCHEMA = maybe_simple_id(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"mhz19.detection_range_set",
|
||||
MHZ19DetectionRangeSetAction,
|
||||
RANGE_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_DETECTION_RANGE, "range_set", mhz19_detection_range),
|
||||
)
|
||||
async def mhz19_detection_range_set_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
detection_range = config.get(CONF_DETECTION_RANGE)
|
||||
template_ = await cg.templatable(detection_range, args, mhz19_detection_range)
|
||||
cg.add(var.set_detection_range(template_))
|
||||
return var
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
button:
|
||||
- platform: template
|
||||
name: MH-Z19 Actions
|
||||
on_press:
|
||||
- mhz19.calibrate_zero: mhz19_sensor
|
||||
- mhz19.abc_enable: mhz19_sensor
|
||||
- mhz19.abc_disable: mhz19_sensor
|
||||
- mhz19.detection_range_set:
|
||||
id: mhz19_sensor
|
||||
detection_range: 2000ppm
|
||||
@@ -1,5 +1,6 @@
|
||||
sensor:
|
||||
- platform: mhz19
|
||||
id: mhz19_sensor
|
||||
co2:
|
||||
name: MH-Z19 CO2 Value
|
||||
temperature:
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
substitutions:
|
||||
tx_pin: GPIO0
|
||||
rx_pin: GPIO2
|
||||
|
||||
packages:
|
||||
uart: !include ../../test_build_components/common/uart/esp8266-ard.yaml
|
||||
mhz19: !include common.yaml
|
||||
actions: !include common-actions.yaml
|
||||
Reference in New Issue
Block a user