Skip to content

Commit 1ffc690

Browse files
committed
Add last unlock user sensor
1 parent e20ca5d commit 1ffc690

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ESPHome Nuki Lock Component (ESP32) [![Build Component](https://github.com/uriyacovy/ESPHome_nuki_lock/actions/workflows/build.yaml/badge.svg)](https://github.com/uriyacovy/ESPHome_nuki_lock/actions/workflows/build.yaml)
22

3-
This module builds an ESPHome lock platform for Nuki Smartlocks (nuki_lock) that creates [23 entities](#entites) in Home Assistant.
3+
This module builds an ESPHome lock platform for Nuki Smartlocks (nuki_lock) that creates [24 entities](#entites) in Home Assistant.
44

55
The lock entity is updated whenever the look changes state (via Nuki App, HA, or manually) using the Nuki BLE advertisement mechanism.
66

@@ -46,6 +46,8 @@ lock:
4646
name: "Nuki Door Sensor"
4747
door_sensor_state:
4848
name: "Nuki Door Sensor: State"
49+
last_unlock_user:
50+
name: "Nuki Last Unlock User"
4951
unpair:
5052
name: "Nuki Unpair Device"
5153
pairing_mode:
@@ -157,6 +159,7 @@ on_paired_action:
157159

158160
**Text Sensor:**
159161
- Door Sensor State
162+
- Last Unlock User
160163

161164
**Switch:**
162165
- Pairing Mode

components/nuki_lock/lock.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
CONF_BATTERY_LEVEL = "battery_level"
2626

2727
CONF_DOOR_SENSOR_STATE = "door_sensor_state"
28+
CONF_LAST_UNLOCK_USER_TEXT_SENSOR = "last_unlock_user"
2829

2930
CONF_UNPAIR_BUTTON = "unpair"
3031

@@ -215,6 +216,9 @@
215216
entity_category=ENTITY_CATEGORY_CONFIG,
216217
icon="mdi:gesture-tap",
217218
),
219+
cv.Optional(CONF_LAST_UNLOCK_USER_TEXT_SENSOR): text_sensor.text_sensor_schema(
220+
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
221+
),
218222
cv.Optional(CONF_PAIRING_MODE_TIMEOUT, default="300s"): cv.positive_time_period_seconds,
219223
cv.Optional(CONF_ON_PAIRING_MODE_ON): automation.validate_automation(
220224
{
@@ -273,6 +277,10 @@ async def to_code(config):
273277
sens = await text_sensor.new_text_sensor(door_sensor_state)
274278
cg.add(var.set_door_sensor_state_text_sensor(sens))
275279

280+
if last_unlock_user := config.get(CONF_LAST_UNLOCK_USER_TEXT_SENSOR):
281+
sens = await text_sensor.new_text_sensor(last_unlock_user)
282+
cg.add(var.set_last_unlock_user_text_sensor(sens))
283+
276284
# Button
277285
if unpair := config.get(CONF_UNPAIR_BUTTON):
278286
b = await button.new_button(unpair)

components/nuki_lock/nuki_lock.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ void NukiLockComponent::processLogEntries(const std::list<NukiLock::LogEntry>& l
323323
char str[50];
324324
char authName[33];
325325
uint32_t authIndex = 0;
326-
327326

328327
for(const auto& log : logEntries)
329328
{
@@ -448,15 +447,20 @@ void NukiLockComponent::processLogEntries(const std::list<NukiLock::LogEntry>& l
448447
}
449448
break;
450449
}
451-
452-
/*if(log.index > _lastRollingLog)
450+
451+
// Send as Home Assistant Event
452+
if(log.index > this->last_rolling_log_id)
453453
{
454-
_lastRollingLog = log.index;
455-
serializeJson(entry, _buffer, _bufferSize);
456-
publishString(mqtt_topic_lock_log_rolling, _buffer, true);
457-
publishInt(mqtt_topic_lock_log_rolling_last, log.index, true);
458-
}*/
454+
this->last_rolling_log_id = log.index;
455+
456+
// TODO
457+
}
459458
}
459+
460+
#ifdef USE_TEXT_SENSOR
461+
if (this->last_unlock_user_text_sensor_ != nullptr)
462+
this->last_unlock_user_text_sensor_->publish_state(this->auth_name_);
463+
#endif
460464
}
461465

462466
bool NukiLockComponent::executeLockAction(NukiLock::LockAction lockAction) {
@@ -845,6 +849,7 @@ void NukiLockComponent::dump_config() {
845849
#endif
846850
#ifdef USE_TEXT_SENSOR
847851
LOG_TEXT_SENSOR(TAG, "Door Sensor State", this->door_sensor_state_text_sensor_);
852+
LOG_TEXT_SENSOR(TAG, "Last Unlock User", this->last_unlock_user_text_sensor_);
848853
#endif
849854
#ifdef USE_SENSOR
850855
LOG_SENSOR(TAG, "Battery Level", this->battery_level_sensor_);

components/nuki_lock/nuki_lock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class NukiLockComponent : public lock::Lock, public PollingComponent, public api
4848
#endif
4949
#ifdef USE_TEXT_SENSOR
5050
SUB_TEXT_SENSOR(door_sensor_state)
51+
SUB_TEXT_SENSOR(last_unlock_user)
5152
#endif
5253
#ifdef USE_NUMBER
5354
SUB_NUMBER(led_brightness)
@@ -178,6 +179,8 @@ class NukiLockComponent : public lock::Lock, public PollingComponent, public api
178179
bool pairing_mode_ = false;
179180
uint32_t pairing_mode_timer_ = 0;
180181

182+
uint32_t last_rolling_log_id = 0;
183+
181184
private:
182185
NukiLock::NukiLock nukiLock_;
183186

0 commit comments

Comments
 (0)