ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
hoermann_hcp_text_sensor.cpp
Go to the documentation of this file.
2
3#include <cstring>
4
5#include "esphome/core/log.h"
7
8namespace esphome::hoermann_hcp {
9
10ESPHOME_LOG_TAG(TAG, "hoermann_hcp.text_sensor");
11
12// Indexed by DoorState. Each fits the 15 characters std::string keeps inline, so publishing never allocates.
13PROGMEM_STRING_TABLE(DoorStateStrings, "Open", "Opening", "Closed", "Closing", "Half open", "Moving to vent",
14 "Vent position", "Moving to half", "Stopped");
15static_assert(DoorStateStrings::COUNT == static_cast<size_t>(DoorState::STOPPED) + 1,
16 "DoorStateStrings must list every DoorState in order");
17
19 this->parent_->add_on_state_callback([this]() { this->update_from_state_(); });
20 // A state decoded before the callback was added would otherwise wait for the next change.
21 this->update_from_state_();
22}
23
24void HoermannHcpDoorStateTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Hoermann HCP Door State", this); }
25
27 // The last state stays while the bus controller is gone, and is published again once it is back.
28 if (!this->parent_->is_valid()) {
29 this->published_ = false;
30 return;
31 }
32 if (!this->parent_->is_door_state_known())
33 return;
34 // Any hub change runs this, so only a new door state is published.
35 const DoorState state = this->parent_->get_door_state();
36 if (this->published_ && state == this->published_state_)
37 return;
38 this->published_ = true;
39 this->published_state_ = state;
40 char text[16];
41 ESPHOME_strncpy_P(text,
42 reinterpret_cast<ESPHOME_PGM_P>(
43 DoorStateStrings::get_progmem_str(static_cast<uint8_t>(state), DoorStateStrings::LAST_INDEX)),
44 sizeof(text));
45 text[sizeof(text) - 1] = '\0';
46 this->publish_state(text);
47}
48
49} // namespace esphome::hoermann_hcp
void publish_state(const std::string &state)
Definition text_sensor.h:41
PROGMEM_STRING_TABLE(DoorStateStrings, "Open", "Opening", "Closed", "Closing", "Half open", "Moving to vent", "Vent position", "Moving to half", "Stopped")
ESPHOME_LOG_TAG(TAG, "hoermann_hcp.binary_sensor")