ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
hoermann_hcp_light.cpp
Go to the documentation of this file.
2
3#include "esphome/core/log.h"
4
5namespace esphome::hoermann_hcp {
6
7static const char *const TAG = "hoermann_hcp.light";
8
10 auto traits = light::LightTraits();
11 traits.set_supported_color_modes({light::ColorMode::ON_OFF});
12 return traits;
13}
14
16 // Nothing is known about the lamp until the bus controller is heard from, so flag the entity until then.
17 this->status_set_warning(LOG_STR("waiting for the bus controller"));
18 this->parent_->add_on_state_callback([this]() { this->update_from_state_(); });
19}
20
22
24 bool binary;
26 // A publish of ours only reaches write_state() a loop pass later, by which time the lamp may have moved on,
27 // so it is recognised by the value it carried rather than by the current one.
28 const optional<bool> published = this->published_state_;
29 this->published_state_.reset();
30 // LightState::setup() always performs a call, so the very first write here is the restored state coming back
31 // rather than a request.
32 const bool restored = !this->boot_replay_done_;
33 this->boot_replay_done_ = true;
34 const bool heading_on = this->parent_->is_light_heading_on();
35 if (binary == heading_on)
36 return;
37 if (restored) {
38 ESP_LOGD(TAG, "Ignoring the restored state, the door decides what the lamp is doing");
39 } else if (published != binary) {
40 if (!this->parent_->is_light_known()) {
41 // Commanding a lamp that has not been read could switch off one that is already on.
42 ESP_LOGW(TAG, "Door has not reported the lamp yet, ignoring the requested state");
43 } else if (this->parent_->cancel_light_toggle() || this->parent_->toggle_light()) {
44 // A toggle the controller has not fetched is withdrawn outright rather than fought with a second one.
45 return;
46 } else {
47 ESP_LOGW(TAG, "Light command was not accepted by the door");
48 }
49 }
50 // Nothing was sent, so the entity has to go back to showing the lamp rather than the request.
51 this->publish_lamp_state_(heading_on);
52}
53
55 if (this->light_state_ == nullptr)
56 return;
57 if (!this->parent_->is_valid()) {
58 this->status_set_warning(LOG_STR("bus controller not responding"));
59 return;
60 }
61 if (!this->parent_->is_light_known()) {
62 // Commands are refused until the door says, so say so rather than looking healthy and doing nothing.
63 this->status_set_warning(LOG_STR("door has not reported the lamp"));
64 return;
65 }
67 const bool heading_on = this->parent_->is_light_heading_on();
68 if (this->light_state_->remote_values.is_on() != heading_on)
69 this->publish_lamp_state_(heading_on);
70}
71
72// Re-enters write_state() a loop pass later, where published_state_ marks the write as ours.
74 this->published_state_ = on;
75 auto call = this->light_state_->make_call();
76 call.set_state(on);
77 // The bus reports the lamp on every broadcast, so nothing here is worth restoring from flash.
78 call.set_save(false);
79 call.perform();
80}
81
82} // namespace esphome::hoermann_hcp
void status_clear_warning()
Definition component.h:289
void add_on_state_callback(F &&callback)
light::LightTraits get_traits() override
void write_state(light::LightState *state) override
void setup_state(light::LightState *state) override
LightCall & set_save(bool save)
Set whether this light call should trigger a save state to recover them at startup....
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
bool is_on() const
Get the binary true/false state of these light color values.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
LightColorValues remote_values
The remote color values reported to the frontend.
void current_values_as_binary(bool *binary)
The result of all the current_values_as_* methods have gamma correction applied.
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
bool state
Definition fan.h:2
@ ON_OFF
Only on/off control.