2023-06-07 12:01:36 -05:00
|
|
|
#include "ratgdo_cover.h"
|
2023-06-07 11:56:55 -05:00
|
|
|
#include "../ratgdo_state.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace ratgdo {
|
|
|
|
|
|
|
|
using namespace esphome::cover;
|
|
|
|
|
|
|
|
static const char* const TAG = "ratgdo.cover";
|
|
|
|
|
|
|
|
void RATGDOCover::dump_config()
|
|
|
|
{
|
|
|
|
LOG_COVER("", "RATGDO Cover", this);
|
|
|
|
}
|
2023-07-01 07:13:38 -07:00
|
|
|
|
|
|
|
void RATGDOCover::setup()
|
|
|
|
{
|
2023-07-07 15:56:12 -07:00
|
|
|
auto state = this->restore_state_();
|
|
|
|
if (state.has_value()) {
|
|
|
|
this->parent_->set_door_position(state.value().position);
|
|
|
|
}
|
2023-07-01 07:13:38 -07:00
|
|
|
this->parent_->subscribe_door_state([=](DoorState state, float position) {
|
|
|
|
this->on_door_state(state, position);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-25 16:03:39 -07:00
|
|
|
void RATGDOCover::on_door_state(DoorState state, float position)
|
2023-06-07 11:56:55 -05:00
|
|
|
{
|
2024-01-19 13:24:16 -10:00
|
|
|
bool save_to_flash = true;
|
2023-06-07 12:01:36 -05:00
|
|
|
switch (state) {
|
2023-07-03 09:47:00 -07:00
|
|
|
case DoorState::OPEN:
|
2023-06-07 11:56:55 -05:00
|
|
|
this->position = COVER_OPEN;
|
2023-06-07 12:01:36 -05:00
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
2023-06-07 12:19:52 -05:00
|
|
|
break;
|
2023-07-03 09:47:00 -07:00
|
|
|
case DoorState::CLOSED:
|
2023-06-07 12:01:36 -05:00
|
|
|
this->position = COVER_CLOSED;
|
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
2023-06-07 12:19:52 -05:00
|
|
|
break;
|
2023-07-03 09:47:00 -07:00
|
|
|
case DoorState::OPENING:
|
2023-06-07 12:01:36 -05:00
|
|
|
this->current_operation = COVER_OPERATION_OPENING;
|
2023-06-25 16:03:39 -07:00
|
|
|
this->position = position;
|
2024-01-19 13:24:16 -10:00
|
|
|
save_to_flash = false;
|
2023-06-07 12:19:52 -05:00
|
|
|
break;
|
2023-07-03 09:47:00 -07:00
|
|
|
case DoorState::CLOSING:
|
2023-06-07 12:01:36 -05:00
|
|
|
this->current_operation = COVER_OPERATION_CLOSING;
|
2023-06-25 16:03:39 -07:00
|
|
|
this->position = position;
|
2024-01-19 13:24:16 -10:00
|
|
|
save_to_flash = false;
|
2023-06-07 12:19:52 -05:00
|
|
|
break;
|
2023-07-03 09:47:00 -07:00
|
|
|
case DoorState::STOPPED:
|
2023-06-24 13:38:44 -07:00
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
2023-06-25 16:03:39 -07:00
|
|
|
this->position = position;
|
2023-07-03 09:47:00 -07:00
|
|
|
case DoorState::UNKNOWN:
|
2023-06-07 12:01:36 -05:00
|
|
|
default:
|
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
2023-07-02 19:44:34 -07:00
|
|
|
this->position = position;
|
2023-06-07 12:01:36 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-06-07 11:56:55 -05:00
|
|
|
|
2024-01-19 13:24:16 -10:00
|
|
|
this->publish_state(save_to_flash);
|
2023-06-07 11:56:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CoverTraits RATGDOCover::get_traits()
|
|
|
|
{
|
|
|
|
auto traits = CoverTraits();
|
|
|
|
traits.set_supports_stop(true);
|
2023-06-24 13:38:44 -07:00
|
|
|
traits.set_supports_toggle(true);
|
|
|
|
traits.set_supports_position(true);
|
2023-06-07 11:56:55 -05:00
|
|
|
return traits;
|
|
|
|
}
|
2023-07-02 21:45:00 -05:00
|
|
|
|
2023-06-07 12:26:12 -05:00
|
|
|
void RATGDOCover::control(const CoverCall& call)
|
|
|
|
{
|
|
|
|
if (call.get_stop()) {
|
2024-01-19 13:24:16 -10:00
|
|
|
this->parent_->door_stop();
|
2023-06-07 12:26:12 -05:00
|
|
|
}
|
2023-06-24 13:38:44 -07:00
|
|
|
if (call.get_toggle()) {
|
2024-01-19 13:24:16 -10:00
|
|
|
this->parent_->door_toggle();
|
2023-06-24 13:38:44 -07:00
|
|
|
}
|
2023-06-07 12:26:12 -05:00
|
|
|
if (call.get_position().has_value()) {
|
|
|
|
auto pos = *call.get_position();
|
|
|
|
if (pos == COVER_OPEN) {
|
2024-01-19 13:24:16 -10:00
|
|
|
this->parent_->door_open();
|
2023-06-07 12:26:12 -05:00
|
|
|
} else if (pos == COVER_CLOSED) {
|
2024-01-19 13:24:16 -10:00
|
|
|
this->parent_->door_close();
|
2023-06-25 16:03:39 -07:00
|
|
|
} else {
|
2023-07-02 19:44:34 -07:00
|
|
|
this->parent_->door_move_to_position(pos);
|
2023-06-07 12:26:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 11:56:55 -05:00
|
|
|
|
|
|
|
} // namespace ratgdo
|
|
|
|
} // namespace esphome
|