ESPHome
2026.11.0-dev
Loading...
Searching...
No Matches
esphome
components
hoermann_hcp
text_sensor
hoermann_hcp_text_sensor.cpp
Go to the documentation of this file.
1
#include "
hoermann_hcp_text_sensor.h
"
2
3
#include <cstring>
4
5
#include "
esphome/core/log.h
"
6
#include "
esphome/core/progmem.h
"
7
8
namespace
esphome::hoermann_hcp
{
9
10
ESPHOME_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.
13
PROGMEM_STRING_TABLE
(DoorStateStrings,
"Open"
,
"Opening"
,
"Closed"
,
"Closing"
,
"Half open"
,
"Moving to vent"
,
14
"Vent position"
,
"Moving to half"
,
"Stopped"
);
15
static_assert
(DoorStateStrings::COUNT ==
static_cast<
size_t
>
(
DoorState::STOPPED
) + 1,
16
"DoorStateStrings must list every DoorState in order"
);
17
18
void
HoermannHcpDoorStateTextSensor::setup
() {
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
24
void
HoermannHcpDoorStateTextSensor::dump_config
() { LOG_TEXT_SENSOR(
""
,
"Hoermann HCP Door State"
,
this
); }
25
26
void
HoermannHcpDoorStateTextSensor::update_from_state_
() {
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
esphome::hoermann_hcp::HoermannHcpDoorStateTextSensor::update_from_state_
void update_from_state_()
Definition
hoermann_hcp_text_sensor.cpp:26
esphome::hoermann_hcp::HoermannHcpDoorStateTextSensor::parent_
HoermannHcp *const parent_
Definition
hoermann_hcp_text_sensor.h:19
esphome::hoermann_hcp::HoermannHcpDoorStateTextSensor::published_
bool published_
Definition
hoermann_hcp_text_sensor.h:21
esphome::hoermann_hcp::HoermannHcpDoorStateTextSensor::published_state_
DoorState published_state_
Definition
hoermann_hcp_text_sensor.h:20
esphome::hoermann_hcp::HoermannHcpDoorStateTextSensor::dump_config
void dump_config() override
Definition
hoermann_hcp_text_sensor.cpp:24
esphome::hoermann_hcp::HoermannHcpDoorStateTextSensor::setup
void setup() override
Definition
hoermann_hcp_text_sensor.cpp:18
esphome::text_sensor::TextSensor::state
std::string state
Definition
text_sensor.h:30
esphome::text_sensor::TextSensor::publish_state
void publish_state(const std::string &state)
Definition
text_sensor.h:41
hoermann_hcp_text_sensor.h
log.h
esphome::hoermann_hcp
Definition
hoermann_hcp_binary_sensor.cpp:5
esphome::hoermann_hcp::DoorState
DoorState
Definition
hoermann_hcp.h:16
esphome::hoermann_hcp::DoorState::STOPPED
@ STOPPED
esphome::hoermann_hcp::PROGMEM_STRING_TABLE
PROGMEM_STRING_TABLE(DoorStateStrings, "Open", "Opening", "Closed", "Closing", "Half open", "Moving to vent", "Vent position", "Moving to half", "Stopped")
esphome::hoermann_hcp::ESPHOME_LOG_TAG
ESPHOME_LOG_TAG(TAG, "hoermann_hcp.binary_sensor")
progmem.h
Generated by
1.12.0