2023-06-07 23:10:02 +00:00
|
|
|
#include "ratgdo_number.h"
|
|
|
|
#include "../ratgdo_state.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace ratgdo {
|
|
|
|
|
2024-01-19 23:24:16 +00:00
|
|
|
using protocol::SetClientID;
|
|
|
|
using protocol::SetRollingCodeCounter;
|
|
|
|
|
2023-11-07 18:58:16 +00:00
|
|
|
float normalize_client_id(float client_id)
|
|
|
|
{
|
|
|
|
uint32_t int_value = static_cast<uint32_t>(client_id);
|
|
|
|
if ((int_value & 0xFFF) != 0x539) {
|
|
|
|
client_id = ceil((client_id - 0x539) / 0x1000) * 0x1000 + 0x539;
|
|
|
|
}
|
|
|
|
return client_id;
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:10:02 +00:00
|
|
|
static const char* const TAG = "ratgdo.number";
|
|
|
|
|
|
|
|
void RATGDONumber::dump_config()
|
|
|
|
{
|
2023-06-07 23:13:38 +00:00
|
|
|
LOG_NUMBER("", "RATGDO Number", this);
|
2023-10-26 17:04:35 +00:00
|
|
|
if (this->number_type_ == RATGDO_CLIENT_ID) {
|
|
|
|
ESP_LOGCONFIG(TAG, " Type: Client ID");
|
|
|
|
} else if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) {
|
2023-06-25 23:03:39 +00:00
|
|
|
ESP_LOGCONFIG(TAG, " Type: Rolling Code Counter");
|
|
|
|
} else if (this->number_type_ == RATGDO_OPENING_DURATION) {
|
|
|
|
ESP_LOGCONFIG(TAG, " Type: Opening Duration");
|
|
|
|
} else if (this->number_type_ == RATGDO_CLOSING_DURATION) {
|
|
|
|
ESP_LOGCONFIG(TAG, " Type: Closing Duration");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 14:13:38 +00:00
|
|
|
void RATGDONumber::setup()
|
2023-06-07 23:10:02 +00:00
|
|
|
{
|
2023-07-07 22:56:12 +00:00
|
|
|
float value;
|
|
|
|
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
|
|
|
|
if (!this->pref_.load(&value)) {
|
2023-10-26 17:04:35 +00:00
|
|
|
if (this->number_type_ == RATGDO_CLIENT_ID) {
|
2023-11-07 18:58:16 +00:00
|
|
|
value = ((random_uint32() + 1) % 0x7FF) << 12 | 0x539; // max size limited to be precisely convertible to float
|
2023-10-26 17:04:35 +00:00
|
|
|
} else {
|
|
|
|
value = 0;
|
|
|
|
}
|
2023-11-07 18:58:16 +00:00
|
|
|
} else {
|
2023-11-06 00:13:32 +00:00
|
|
|
if (this->number_type_ == RATGDO_CLIENT_ID) {
|
|
|
|
uint32_t int_value = static_cast<uint32_t>(value);
|
|
|
|
if ((int_value & 0xFFF) != 0x539) {
|
2023-11-06 01:50:59 +00:00
|
|
|
value = ((random_uint32() + 1) % 0x7FF) << 12 | 0x539; // max size limited to be precisely convertible to float
|
2023-11-06 00:13:32 +00:00
|
|
|
this->pref_.save(&value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-07 22:56:12 +00:00
|
|
|
this->control(value);
|
|
|
|
|
2023-07-01 14:13:38 +00:00
|
|
|
if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) {
|
|
|
|
this->parent_->subscribe_rolling_code_counter([=](uint32_t value) {
|
2023-07-07 22:56:12 +00:00
|
|
|
this->update_state(value);
|
2023-07-01 14:13:38 +00:00
|
|
|
});
|
|
|
|
} else if (this->number_type_ == RATGDO_OPENING_DURATION) {
|
|
|
|
this->parent_->subscribe_opening_duration([=](float value) {
|
2023-07-07 22:56:12 +00:00
|
|
|
this->update_state(value);
|
2023-07-01 14:13:38 +00:00
|
|
|
});
|
|
|
|
} else if (this->number_type_ == RATGDO_CLOSING_DURATION) {
|
|
|
|
this->parent_->subscribe_closing_duration([=](float value) {
|
2023-07-07 22:56:12 +00:00
|
|
|
this->update_state(value);
|
2023-07-01 14:13:38 +00:00
|
|
|
});
|
2023-06-25 23:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 14:13:38 +00:00
|
|
|
void RATGDONumber::set_number_type(NumberType number_type_)
|
2023-06-25 23:03:39 +00:00
|
|
|
{
|
2023-07-01 14:13:38 +00:00
|
|
|
this->number_type_ = number_type_;
|
|
|
|
if (this->number_type_ == RATGDO_OPENING_DURATION || this->number_type_ == RATGDO_CLOSING_DURATION) {
|
|
|
|
this->traits.set_step(0.1);
|
2023-07-01 14:35:05 +00:00
|
|
|
this->traits.set_min_value(0.0);
|
|
|
|
this->traits.set_max_value(180.0);
|
2023-10-26 17:04:35 +00:00
|
|
|
} else if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) {
|
2023-07-03 16:47:00 +00:00
|
|
|
this->traits.set_max_value(0xfffffff);
|
2023-10-26 17:04:35 +00:00
|
|
|
} else if (this->number_type_ == RATGDO_CLIENT_ID) {
|
2023-11-06 01:50:59 +00:00
|
|
|
this->traits.set_step(0x1000);
|
|
|
|
this->traits.set_min_value(0x539);
|
|
|
|
this->traits.set_max_value(0x7ff539);
|
2023-07-03 16:47:00 +00:00
|
|
|
}
|
2023-06-25 23:03:39 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:56:12 +00:00
|
|
|
void RATGDONumber::update_state(float value)
|
|
|
|
{
|
2024-01-19 23:24:16 +00:00
|
|
|
if (value == this->state) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-07 22:56:12 +00:00
|
|
|
this->pref_.save(&value);
|
|
|
|
this->publish_state(value);
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:10:02 +00:00
|
|
|
void RATGDONumber::control(float value)
|
|
|
|
{
|
2023-06-25 23:03:39 +00:00
|
|
|
if (this->number_type_ == RATGDO_ROLLING_CODE_COUNTER) {
|
2024-01-19 23:24:16 +00:00
|
|
|
this->parent_->call_protocol(SetRollingCodeCounter { static_cast<uint32_t>(value) });
|
2023-06-25 23:03:39 +00:00
|
|
|
} else if (this->number_type_ == RATGDO_OPENING_DURATION) {
|
2023-07-03 02:44:34 +00:00
|
|
|
this->parent_->set_opening_duration(value);
|
2023-06-25 23:03:39 +00:00
|
|
|
} else if (this->number_type_ == RATGDO_CLOSING_DURATION) {
|
2023-07-03 02:44:34 +00:00
|
|
|
this->parent_->set_closing_duration(value);
|
2023-10-26 17:04:35 +00:00
|
|
|
} else if (this->number_type_ == RATGDO_CLIENT_ID) {
|
2023-11-07 18:58:16 +00:00
|
|
|
value = normalize_client_id(value);
|
2024-01-19 23:24:16 +00:00
|
|
|
this->parent_->call_protocol(SetClientID { static_cast<uint32_t>(value) });
|
2023-06-25 23:03:39 +00:00
|
|
|
}
|
2023-11-05 23:47:11 +00:00
|
|
|
this->update_state(value);
|
2023-06-07 23:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ratgdo
|
|
|
|
} // namespace esphome
|