Skip to content

Commit

Permalink
Add last lock action & trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Nov 12, 2024
1 parent de0d4f3 commit 51fd21f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .github/test-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ lock:
name: "Nuki Door Sensor: State"
last_unlock_user:
name: "Nuki Last Unlock User"

last_lock_action:
name: "Nuki Last Lock Action"
last_lock_action_trigger:
name: "Nuki Last Lock Action Trigger"

unpair:
name: "Nuki Unpair Device"

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ lock:
name: "Nuki Door Sensor: State"
last_unlock_user:
name: "Nuki Last Unlock User"
last_lock_action:
name: "Nuki Last Lock Action"
last_lock_action_trigger:
name: "Nuki Last Lock Action Trigger"
# Optional: Switches
pairing_mode:
name: "Nuki Pairing Mode"
Expand Down
18 changes: 18 additions & 0 deletions components/nuki_lock/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

CONF_DOOR_SENSOR_STATE = "door_sensor_state"
CONF_LAST_UNLOCK_USER_TEXT_SENSOR = "last_unlock_user"
CONF_LAST_LOCK_ACTION_TEXT_SENSOR = "last_lock_action"
CONF_LAST_LOCK_ACTION_TRIGGER_TEXT_SENSOR = "last_lock_action_trigger"

CONF_UNPAIR_BUTTON = "unpair"

Expand Down Expand Up @@ -220,6 +222,14 @@
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon="mdi:account-clock"
),
cv.Optional(CONF_LAST_LOCK_ACTION_TEXT_SENSOR): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon="mdi:account-clock"
),
cv.Optional(CONF_LAST_LOCK_ACTION_TRIGGER_TEXT_SENSOR): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon="mdi:account-clock"
),

cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
device_class=DEVICE_CLASS_BATTERY,
Expand Down Expand Up @@ -444,6 +454,14 @@ async def to_code(config):
sens = await text_sensor.new_text_sensor(last_unlock_user)
cg.add(var.set_last_unlock_user_text_sensor(sens))

if last_lock_action_trigger := config.get(CONF_LAST_LOCK_ACTION_TRIGGER_TEXT_SENSOR):
sens = await text_sensor.new_text_sensor(last_lock_action_trigger)
cg.add(var.set_last_lock_action_trigger_text_sensor(sens))

if last_lock_action := config.get(CONF_LAST_LOCK_ACTION_TEXT_SENSOR):
sens = await text_sensor.new_text_sensor(last_lock_action)
cg.add(var.set_last_lock_action_text_sensor(sens))

# Button
if unpair := config.get(CONF_UNPAIR_BUTTON):
b = await button.new_button(unpair)
Expand Down
25 changes: 19 additions & 6 deletions components/nuki_lock/nuki_lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,23 @@ void NukiLockComponent::update_status()

if (cmd_result == Nuki::CmdResult::Success) {
this->status_update_consecutive_errors_ = 0;
NukiLock::LockState currentLockState = this->retrieved_key_turner_state_.lockState;
char currentLockStateAsString[30];
NukiLock::lockstateToString(currentLockState, currentLockStateAsString);
NukiLock::LockState current_lock_state = this->retrieved_key_turner_state_.lockState;

ESP_LOGI(TAG, "Bat state: %#x, Bat crit: %d, Bat perc:%d lock state: %s (%d) %d:%d:%d",
char current_lock_state_as_string[30];
NukiLock::lockstateToString(current_lock_state, current_lock_state_as_string);

char last_lock_action[30];
NukiLock::lockactionToString(this->retrieved_key_turner_state_.lastLockAction, last_lock_action);

char last_lock_action_trigger[30];
NukiLock::triggerToString(this->retrieved_key_turner_state_.lastLockActionTrigger, last_lock_action_trigger);

ESP_LOGI(TAG, "Bat state: %#x, Bat crit: %d, Bat perc: %d lock state: %s (%d) %d:%d:%d",
this->retrieved_key_turner_state_.criticalBatteryState,
this->nuki_lock_.isBatteryCritical(),
this->nuki_lock_.getBatteryPerc(),
currentLockStateAsString,
currentLockState,
current_lock_state_as_string,
current_lock_state,
this->retrieved_key_turner_state_.currentTimeHour,
this->retrieved_key_turner_state_.currentTimeMinute,
this->retrieved_key_turner_state_.currentTimeSecond
Expand All @@ -295,6 +302,10 @@ void NukiLockComponent::update_status()
#ifdef USE_TEXT_SENSOR
if (this->door_sensor_state_text_sensor_ != nullptr)
this->door_sensor_state_text_sensor_->publish_state(this->nuki_doorsensor_to_string(this->retrieved_key_turner_state_.doorSensorState));
if (this->last_lock_action_text_sensor_ != nullptr)
this->last_lock_action_text_sensor_->publish_state(last_lock_action);
if (this->last_lock_action_trigger_text_sensor_ != nullptr)
this->last_lock_action_trigger_text_sensor_->publish_state(last_lock_action_trigger);
#endif

if (
Expand Down Expand Up @@ -1094,6 +1105,8 @@ void NukiLockComponent::dump_config() {
#ifdef USE_TEXT_SENSOR
LOG_TEXT_SENSOR(TAG, "Door Sensor State", this->door_sensor_state_text_sensor_);
LOG_TEXT_SENSOR(TAG, "Last Unlock User", this->last_unlock_user_text_sensor_);
LOG_TEXT_SENSOR(TAG, "Last Lock Action", this->last_lock_action_text_sensor_);
LOG_TEXT_SENSOR(TAG, "Last Lock Action Trigger", this->last_lock_action_trigger_text_sensor_);
#endif
#ifdef USE_SENSOR
LOG_SENSOR(TAG, "Battery Level", this->battery_level_sensor_);
Expand Down
2 changes: 2 additions & 0 deletions components/nuki_lock/nuki_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class NukiLockComponent : public lock::Lock, public PollingComponent, public Nuk
#ifdef USE_TEXT_SENSOR
SUB_TEXT_SENSOR(door_sensor_state)
SUB_TEXT_SENSOR(last_unlock_user)
SUB_TEXT_SENSOR(last_lock_action)
SUB_TEXT_SENSOR(last_lock_action_trigger)
#endif
#ifdef USE_NUMBER
SUB_NUMBER(led_brightness)
Expand Down

0 comments on commit 51fd21f

Please sign in to comment.