11static const char *
const TAG =
"mqtt.valve";
24 if (traits.get_supports_position()) {
25 this->
subscribe(this->get_position_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
27 if (!value.has_value()) {
28 ESP_LOGW(TAG,
"Invalid position value: '%s'", payload.c_str());
42 LOG_MQTT_COMPONENT(
true, has_command_topic)
43 if (traits.get_supports_position()) {
45 " Position State Topic: '%s'\n"
46 " Position Command Topic: '%s'",
47 this->get_position_state_topic().c_str(), this->get_position_command_topic().c_str());
53 if (!device_class.empty()) {
54 root[MQTT_DEVICE_CLASS] = device_class;
59 if (traits.get_is_assumed_state()) {
60 root[MQTT_OPTIMISTIC] =
true;
62 if (traits.get_supports_position()) {
63 root[MQTT_POSITION_TOPIC] = this->get_position_state_topic();
64 root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic();
75 if (traits.get_supports_position()) {
76 char pos[VALUE_ACCURACY_MAX_LEN];
78 if (!this->
publish(this->get_position_state_topic(), pos,
len))
85 : traits.get_supports_position() ?
"open"
StringRef get_device_class_ref() const
Get the device class as StringRef.
const StringRef & get_name() const
constexpr const char * c_str() const
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
state bool send_initial_state() override
MQTTValveComponent(valve::Valve *valve)
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
void dump_config() override
ValveCall & set_position(float position)
Set the call to a certain target position.
ValveCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
void perform()
Perform the valve call.
Base class for all valve devices.
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
ValveCall make_call()
Construct a new valve call used to control the valve.
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
void add_on_state_callback(std::function< void()> &&f)
virtual ValveTraits get_traits()=0
bool get_supports_position() const
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
@ VALVE_OPERATION_OPENING
The valve is currently opening.
@ VALVE_OPERATION_CLOSING
The valve is currently closing.
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Simple Helper struct used for Home Assistant MQTT send_discovery().