Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
platform_lock.cpp
Go to the documentation of this file.
1/// @file platform_lock.cpp
2/// @brief Experimental lock entity for IO-Homecontrol lock devices.
3/// @ingroup hioc_platforms
4
5#include "platform_lock.h"
6
7#include "hub_internal.h"
8#include "esphome/core/log.h"
9
10namespace esphome {
11namespace home_io_control {
12
13static const char *const TAG = "home_io_control.lock";
14
16 // Locks participate in the same shared registry and callback fan-out as the other entity types.
17 this->parent_->add_device(this->device_id_, device_type_, subtype_, /*inverted=*/false);
18 this->parent_->set_device_status_poll_interval(this->device_id_, this->status_poll_interval_ms_);
19 this->parent_->register_device_callback(
20 [this](const std::string &id, const IoDevice &dev) { this->on_device_update_(id, dev); });
21 this->set_timeout("init_status", detail::INITIAL_STATUS_REQUEST_DELAY_MS,
22 [this]() { this->parent_->queue_request_device_status(this->device_id_); });
23
24 this->traits.set_assumed_state(false);
25 this->traits.set_requires_code(false);
26 this->traits.set_supports_open(false);
27 this->traits.set_supported_states(
28 {lock::LOCK_STATE_LOCKED, lock::LOCK_STATE_UNLOCKED, lock::LOCK_STATE_LOCKING, lock::LOCK_STATE_UNLOCKING});
29}
30
31void IOHomeLock::control(const lock::LockCall &call) {
32 const auto state = call.get_state();
33 if (!state.has_value())
34 return;
35
36 switch (state.value()) {
37 case lock::LOCK_STATE_LOCKED:
38 case lock::LOCK_STATE_LOCKING:
39 this->parent_->queue_set_lock_state(this->device_id_, true);
40 break;
41 case lock::LOCK_STATE_UNLOCKED:
42 case lock::LOCK_STATE_UNLOCKING:
43 this->parent_->queue_set_lock_state(this->device_id_, false);
44 break;
45 case lock::LOCK_STATE_NONE:
46 case lock::LOCK_STATE_JAMMED:
47 default:
48 break;
49 }
50}
51
52void IOHomeLock::on_device_update_(const std::string &id, const IoDevice &dev) {
53 if (id != this->device_id_ || dev.position == UNKNOWN_POSITION)
54 return;
55
56 if (!dev.is_stopped) {
57 if (dev.target != UNKNOWN_POSITION) {
58 this->publish_state(dev.target < detail::BINARY_ENTITY_ON_POSITION_THRESHOLD ? lock::LOCK_STATE_UNLOCKING
59 : lock::LOCK_STATE_LOCKING);
60 }
61 return;
62 }
63
64 this->publish_state(dev.position < detail::BINARY_ENTITY_ON_POSITION_THRESHOLD ? lock::LOCK_STATE_UNLOCKED
65 : lock::LOCK_STATE_LOCKED);
66}
67
69 LOG_LOCK("", "IO-Homecontrol Lock", this);
70 ESP_LOGCONFIG(TAG, " Device ID: %s", this->device_id_.c_str());
71 if (this->status_poll_interval_ms_ == 0) {
72 ESP_LOGCONFIG(TAG, " Status Poll Interval: default single settle poll");
73 } else {
74 ESP_LOGCONFIG(TAG, " Status Poll Interval: %u ms", this->status_poll_interval_ms_);
75 }
76 ESP_LOGCONFIG(TAG, " Status: experimental and untested");
77}
78
79} // namespace home_io_control
80} // namespace esphome
void setup() override
Initialize the lock entity and register it with the shared hub.
void on_device_update_(const std::string &id, const IoDevice &dev)
Handle inbound device status updates.
IOHomeControlComponent * parent_
void control(const lock::LockCall &call) override
Apply a Home Assistant lock control request.
void dump_config() override
Dump configuration to the log.
Internal helpers shared by the hub implementation .cpp files.
constexpr uint32_t INITIAL_STATUS_REQUEST_DELAY_MS
Delay before the first post-boot status request from an entity.
constexpr float BINARY_ENTITY_ON_POSITION_THRESHOLD
Shared 0-100 cutoff: values below this mean binary "on".
static constexpr float UNKNOWN_POSITION
Sentinel value meaning "position is not known yet".
static const char *const TAG
Definition hub_core.cpp:35
Experimental lock entity for IO-Homecontrol lock-capable devices.
Runtime state of a paired IO‑Homecontrol device.
float target
Target position the device is moving toward.
float position
Current position: 0=open, 100=closed, or UNKNOWN_POSITION.
bool is_stopped
True if device is not moving.