Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Mar 24, 2024
1 parent 333d53f commit 042b628
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 52 deletions.
56 changes: 36 additions & 20 deletions components/nuki_lock/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
DEVICE_CLASS_DOOR,
UNIT_PERCENT,
ENTITY_CATEGORY_CONFIG,
ENTITY_CATEGORY_DIAGNOSTIC,
CONF_TURN_OFF_ACTION,
CONF_TURN_ON_ACTION
ENTITY_CATEGORY_DIAGNOSTIC,
CONF_TRIGGER_ID
)

AUTO_LOAD = ["binary_sensor", "text_sensor", "sensor", "switch", "button"]
Expand All @@ -28,6 +27,10 @@

CONF_SET_PAIRING_MODE = "pairing_mode"

CONF_ON_PAIRING_MODE_ON = "on_pairing_mode_on"
CONF_ON_PAIRING_MODE_OFF = "on_pairing_mode_off"
CONF_ON_PAIRED = "on_paired"

nuki_lock_ns = cg.esphome_ns.namespace('nuki_lock')
NukiLock = nuki_lock_ns.class_('NukiLockComponent', lock.Lock, switch.Switch, cg.Component)

Expand All @@ -42,6 +45,10 @@
"NukiLockPairingModeAction", automation.Action
)

PairingModeOnTrigger = nuki_lock_ns.class_("PairingModeOnTrigger", automation.Trigger.template())
PairingModeOffTrigger = nuki_lock_ns.class_("PairingModeOffTrigger", automation.Trigger.template())
PairedTrigger = nuki_lock_ns.class_("PairedTrigger", automation.Trigger.template())

CONFIG_SCHEMA = lock.LOCK_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(NukiLock),
cv.Required(CONF_IS_CONNECTED): binary_sensor.binary_sensor_schema(
Expand Down Expand Up @@ -72,14 +79,20 @@
NukiLockPairingModeSwitch,
entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:bluetooth-connect",
).extend(
),
cv.Optional(CONF_ON_PAIRING_MODE_ON): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PairingModeOnTrigger),
}
),
cv.Optional(CONF_ON_PAIRING_MODE_OFF): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PairingModeOffTrigger),
}
),
cv.Optional(CONF_ON_PAIRED): automation.validate_automation(
{
cv.Optional(CONF_TURN_OFF_ACTION): automation.validate_automation(
single=True
),
cv.Optional(CONF_TURN_ON_ACTION): automation.validate_automation(
single=True
),
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PairedTrigger),
}
),
}).extend(cv.polling_component_schema("500ms"))
Expand Down Expand Up @@ -122,17 +135,20 @@ async def to_code(config):
if CONF_PAIRING_MODE_SWITCH in config:
sw = await switch.new_switch(config[CONF_PAIRING_MODE_SWITCH])
await cg.register_parented(sw, config[CONF_ID])
if CONF_TURN_OFF_ACTION in config:
await automation.build_automation(
sw.get_turn_off_trigger(), [], config[CONF_TURN_OFF_ACTION]
)
if CONF_TURN_ON_ACTION in config:
await automation.build_automation(
sw.get_turn_on_trigger(), [], config[CONF_TURN_ON_ACTION]
)

cg.add(var.set_pairing_mode_switch(sw))


for conf in config.get(CONF_ON_PAIRING_MODE_ON, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)

for conf in config.get(CONF_ON_PAIRING_MODE_OFF, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)

for conf in config.get(CONF_ON_PAIRED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)

@automation.register_action(
"nuki_lock.unpair",
NukiLockUnpairAction,
Expand Down
75 changes: 44 additions & 31 deletions components/nuki_lock/nuki_lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ namespace esphome
ESP_LOGI(TAG, "Nuki paired successfuly!");
this->update_status();

this->paired_callback_.call();

this->set_pairing_mode(false);
}
this->is_paired_->publish_state(paired);
Expand Down Expand Up @@ -545,6 +547,17 @@ namespace esphome
ESP_LOGI(TAG, "event notified %d", eventType);
}

// Unpair Button
void NukiLockUnpairButton::press_action()
{
this->parent_->unpair();
}

void NukiLockUnpairButton::dump_config()
{
LOG_BUTTON(TAG, "Unpair", this);
}

void NukiLockComponent::unpair()
{
if(this->nukiLock_.isPairedWithLock())
Expand All @@ -558,6 +571,31 @@ namespace esphome
}
}

// Pairing Mode Switch
void NukiLockPairingModeSwitch::setup()
{
this->publish_state(false);
}

void NukiLockPairingModeSwitch::dump_config()
{
LOG_SWITCH(TAG, "Pairing Mode", this);
}

void NukiLockPairingModeSwitch::write_state(bool state)
{
this->parent_->set_pairing_mode(state);

if(state)
{
this->pairing_mode_on_callback_.call();
}
else
{
this->pairing_mode_off_callback_.call();
}
}

void NukiLockComponent::set_pairing_mode(bool enabled)
{
uint16_t timer_minutes = 5;
Expand All @@ -580,45 +618,20 @@ namespace esphome
this->pairing_mode_switch_->publish_state(this->pairing_mode_);
}

void NukiLockUnpairButton::press_action()
void NukiLockComponent::add_pairing_mode_on_callback(std::function<void()> &&callback)
{
this->parent_->unpair();
this->pairing_mode_on_callback_.add(std::move(callback));
}

void NukiLockUnpairButton::dump_config()
void NukiLockComponent::add_pairing_mode_off_callback(std::function<void()> &&callback)
{
LOG_BUTTON(TAG, "Unpair", this);
this->pairing_mode_off_callback_.add(std::move(callback));
}


//NukiLockPairingModeSwitch::NukiLockPairingModeSwitch() : turn_on_trigger_(new Trigger<>()), turn_off_trigger_(new Trigger<>()) {}

void NukiLockPairingModeSwitch::setup()
void NukiLockComponent::add_paired_callback(std::function<void()> &&callback)
{
this->publish_state(false);
this->paired_callback_.add(std::move(callback));
}

void NukiLockPairingModeSwitch::dump_config()
{
LOG_SWITCH(TAG, "Pairing Mode", this);
}

void NukiLockPairingModeSwitch::write_state(bool state)
{
this->parent_->set_pairing_mode(state);

if(state)
{
this->turn_on_trigger_->trigger();
}
else
{
this->turn_off_trigger_->trigger();
}
}

Trigger<> *NukiLockPairingModeSwitch::get_turn_on_trigger() const { return this->turn_on_trigger_; }
Trigger<> *NukiLockPairingModeSwitch::get_turn_off_trigger() const { return this->turn_off_trigger_; }

} //namespace nuki_lock
} //namespace esphome
30 changes: 29 additions & 1 deletion components/nuki_lock/nuki_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ namespace esphome {
void set_unpair_button(button::Button *unpair_button) { this->unpair_button_ = unpair_button; }
void set_pairing_mode_switch(switch_::Switch *pairing_mode_switch) { this->pairing_mode_switch_ = pairing_mode_switch; }

void add_pairing_mode_on_callback(std::function<void()> &&callback);
void add_pairing_mode_off_callback(std::function<void()> &&callback);
void add_paired_callback(std::function<void()> &&callback);

float get_setup_priority() const override { return setup_priority::HARDWARE - 1.0f; }

void dump_config() override;
Expand Down Expand Up @@ -93,9 +97,12 @@ namespace esphome {
bool lock_n_go_;

bool pairing_mode_ = false;

uint16_t pairing_mode_timer_ = 0;

CallbackManager<void()> pairing_mode_on_callback_{};
CallbackManager<void()> pairing_mode_off_callback_{};
CallbackManager<void()> paired_callback_{};

private:
NukiLock::NukiLock nukiLock_;

Expand Down Expand Up @@ -133,6 +140,27 @@ namespace esphome {
NukiLockComponent *parent_;
};

class PairingModeOnTrigger : public Trigger<> {
public:
explicit PairingModeOnTrigger(NukiLockComponent *parent) {
parent->add_pairing_mode_on_callback([this]() { this->trigger(); });
}
};

class PairingModeOffTrigger : public Trigger<> {
public:
explicit PairingModeOffTrigger(NukiLockComponent *parent) {
parent->add_pairing_mode_off_callback([this]() { this->trigger(); });
}
};

class PairedTrigger : public Trigger<> {
public:
explicit PairedTrigger(NukiLockComponent *parent) {
parent->add_paired_callback([this]() { this->trigger(); });
}
};

class NukiLockUnpairButton : public Component, public button::Button {
public:
void set_parent(NukiLockComponent *parent) { this->parent_ = parent; }
Expand Down

0 comments on commit 042b628

Please sign in to comment.