Add an emulation switch toggle to the web UI

This commit is contained in:
Paul Wieland 2024-07-11 16:36:21 -04:00
parent b148105423
commit 01a4a41809
7 changed files with 39 additions and 1 deletions

View File

@ -59,6 +59,12 @@ switch:
output: true output: true
name: "Status obstruction" name: "Status obstruction"
entity_category: diagnostic entity_category: diagnostic
- platform: ratgdo
type: emulation
id: "${id_prefix}_emulation_enabled"
ratgdo_id: ${id_prefix}
name: "Emulation Mode"
entity_category: config
binary_sensor: binary_sensor:
- platform: ratgdo - platform: ratgdo

View File

@ -679,6 +679,10 @@ namespace ratgdo {
{ {
this->learn_state.subscribe([=](LearnState state) { defer("learn_state", [=] { f(state); }); }); this->learn_state.subscribe([=](LearnState state) { defer("learn_state", [=] { f(state); }); });
} }
void RATGDOComponent::subscribe_emulation_state(std::function<void(EmulationState)>&& f)
{
this->emulation_state.subscribe([=](EmulationState state) { defer("emulation_state", [=] { f(state); }); });
}
// dry contact methods // dry contact methods
void RATGDOComponent::set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor) void RATGDOComponent::set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor)

View File

@ -83,6 +83,7 @@ namespace ratgdo {
observable<ButtonState> button_state { ButtonState::UNKNOWN }; observable<ButtonState> button_state { ButtonState::UNKNOWN };
observable<MotionState> motion_state { MotionState::UNKNOWN }; observable<MotionState> motion_state { MotionState::UNKNOWN };
observable<LearnState> learn_state { LearnState::UNKNOWN }; observable<LearnState> learn_state { LearnState::UNKNOWN };
observable<EmulationState> emulation_state { EmulationState::INACTIVE };
OnceCallbacks<void(DoorState)> on_door_state_; OnceCallbacks<void(DoorState)> on_door_state_;
@ -172,6 +173,7 @@ namespace ratgdo {
void subscribe_motion_state(std::function<void(MotionState)>&& f); void subscribe_motion_state(std::function<void(MotionState)>&& f);
void subscribe_sync_failed(std::function<void(bool)>&& f); void subscribe_sync_failed(std::function<void(bool)>&& f);
void subscribe_learn_state(std::function<void(LearnState)>&& f); void subscribe_learn_state(std::function<void(LearnState)>&& f);
void subscribe_emulation_state(std::function<void(EmulationState)>&& f);
protected: protected:
RATGDOStore isr_store_ {}; RATGDOStore isr_store_ {};

View File

@ -76,6 +76,10 @@ namespace ratgdo {
(UNKNOWN, 2)) (UNKNOWN, 2))
LearnState learn_state_toggle(LearnState state); LearnState learn_state_toggle(LearnState state);
ENUM(EmulationState, uint8_t,
(INACTIVE, 0),
(ACTIVE, 1))
ENUM(PairedDevice, uint8_t, ENUM(PairedDevice, uint8_t,
(ALL, 0), (ALL, 0),
(REMOTE, 1), (REMOTE, 1),

View File

@ -13,6 +13,7 @@ SwitchType = ratgdo_ns.enum("SwitchType")
CONF_TYPE = "type" CONF_TYPE = "type"
TYPES = { TYPES = {
"learn": SwitchType.RATGDO_LEARN, "learn": SwitchType.RATGDO_LEARN,
"emulation": SwitchType.RATGDO_EMULATION
} }

View File

@ -4,6 +4,7 @@
namespace esphome { namespace esphome {
namespace ratgdo { namespace ratgdo {
using protocol::SetEnableEmulationMode;
static const char* const TAG = "ratgdo.switch"; static const char* const TAG = "ratgdo.switch";
@ -21,6 +22,18 @@ namespace ratgdo {
this->parent_->subscribe_learn_state([=](LearnState state) { this->parent_->subscribe_learn_state([=](LearnState state) {
this->publish_state(state == LearnState::ACTIVE); this->publish_state(state == LearnState::ACTIVE);
}); });
} else if (this->switch_type_ == SwitchType::RATGDO_EMULATION) {
bool value = false;
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
this->pref_.load(&value);
this->state = value;
this->parent_->emulation_state = value ? EmulationState::ACTIVE : EmulationState::INACTIVE;
this->parent_->subscribe_emulation_state([=](EmulationState state) {
this->publish_state(state == EmulationState::ACTIVE);
});
this->parent_->call_protocol(SetEnableEmulationMode { static_cast<bool>(this->state) });
} }
} }
@ -32,6 +45,12 @@ namespace ratgdo {
} else { } else {
this->parent_->inactivate_learn(); this->parent_->inactivate_learn();
} }
} else if (this->switch_type_ == SwitchType::RATGDO_EMULATION) {
this->state = state;
this->parent_->emulation_state = state ? EmulationState::ACTIVE : EmulationState::INACTIVE;
this->parent_->call_protocol(SetEnableEmulationMode { static_cast<bool>(state) });
this->pref_.save(&state);
} }
} }

View File

@ -9,7 +9,8 @@ namespace esphome {
namespace ratgdo { namespace ratgdo {
enum SwitchType { enum SwitchType {
RATGDO_LEARN RATGDO_LEARN,
RATGDO_EMULATION
}; };
class RATGDOSwitch : public switch_::Switch, public RATGDOClient, public Component { class RATGDOSwitch : public switch_::Switch, public RATGDOClient, public Component {
@ -22,6 +23,7 @@ namespace ratgdo {
protected: protected:
SwitchType switch_type_; SwitchType switch_type_;
ESPPreferenceObject pref_;
}; };
} // namespace ratgdo } // namespace ratgdo