2023-06-07 17:01:36 +00:00
|
|
|
#include "ratgdo_cover.h"
|
2023-06-07 16:56:55 +00: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 14:13:38 +00:00
|
|
|
|
|
|
|
void RATGDOCover::setup()
|
|
|
|
{
|
|
|
|
this->parent_->subscribe_door_state([=](DoorState state, float position) {
|
|
|
|
this->on_door_state(state, position);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-25 23:03:39 +00:00
|
|
|
void RATGDOCover::on_door_state(DoorState state, float position)
|
2023-06-07 16:56:55 +00:00
|
|
|
{
|
2023-06-07 17:01:36 +00:00
|
|
|
switch (state) {
|
2023-06-07 19:11:03 +00:00
|
|
|
case DoorState::DOOR_STATE_OPEN:
|
2023-06-07 16:56:55 +00:00
|
|
|
this->position = COVER_OPEN;
|
2023-06-07 17:01:36 +00:00
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
2023-06-07 17:19:52 +00:00
|
|
|
break;
|
2023-06-07 19:11:03 +00:00
|
|
|
case DoorState::DOOR_STATE_CLOSED:
|
2023-06-07 17:01:36 +00:00
|
|
|
this->position = COVER_CLOSED;
|
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
2023-06-07 17:19:52 +00:00
|
|
|
break;
|
2023-06-07 19:11:03 +00:00
|
|
|
case DoorState::DOOR_STATE_OPENING:
|
2023-06-07 17:01:36 +00:00
|
|
|
this->current_operation = COVER_OPERATION_OPENING;
|
2023-06-25 23:03:39 +00:00
|
|
|
this->position = position;
|
2023-06-07 17:19:52 +00:00
|
|
|
break;
|
2023-06-07 19:11:03 +00:00
|
|
|
case DoorState::DOOR_STATE_CLOSING:
|
2023-06-07 17:01:36 +00:00
|
|
|
this->current_operation = COVER_OPERATION_CLOSING;
|
2023-06-25 23:03:39 +00:00
|
|
|
this->position = position;
|
2023-06-07 17:19:52 +00:00
|
|
|
break;
|
2023-06-07 19:11:03 +00:00
|
|
|
case DoorState::DOOR_STATE_STOPPED:
|
2023-06-24 20:38:44 +00:00
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
2023-06-25 23:03:39 +00:00
|
|
|
this->position = position;
|
2023-06-07 17:01:36 +00:00
|
|
|
default:
|
|
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-06-07 16:56:55 +00:00
|
|
|
|
|
|
|
this->publish_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
CoverTraits RATGDOCover::get_traits()
|
|
|
|
{
|
|
|
|
auto traits = CoverTraits();
|
|
|
|
traits.set_supports_stop(true);
|
2023-06-24 20:38:44 +00:00
|
|
|
traits.set_supports_toggle(true);
|
|
|
|
traits.set_supports_position(true);
|
2023-06-07 16:56:55 +00:00
|
|
|
return traits;
|
|
|
|
}
|
2023-06-07 17:26:12 +00:00
|
|
|
void RATGDOCover::control(const CoverCall& call)
|
|
|
|
{
|
|
|
|
if (call.get_stop()) {
|
|
|
|
this->parent_->stopDoor();
|
|
|
|
}
|
2023-06-24 20:38:44 +00:00
|
|
|
if (call.get_toggle()) {
|
|
|
|
this->parent_->toggleDoor();
|
|
|
|
}
|
2023-06-07 17:26:12 +00:00
|
|
|
if (call.get_position().has_value()) {
|
|
|
|
auto pos = *call.get_position();
|
|
|
|
if (pos == COVER_OPEN) {
|
|
|
|
this->parent_->openDoor();
|
|
|
|
} else if (pos == COVER_CLOSED) {
|
|
|
|
this->parent_->closeDoor();
|
2023-06-25 23:03:39 +00:00
|
|
|
} else {
|
|
|
|
this->parent_->setDoorPosition(pos);
|
2023-06-07 17:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 16:56:55 +00:00
|
|
|
|
|
|
|
} // namespace ratgdo
|
|
|
|
} // namespace esphome
|