Skip to content

Commit

Permalink
Add Pairing Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Mar 24, 2024
1 parent de87269 commit 0ab6e52
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 91 deletions.
38 changes: 36 additions & 2 deletions components/nuki_lock/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import esphome.config_validation as cv
from esphome import automation
from esphome.components import lock, binary_sensor, text_sensor, sensor, switch, button
from esphome.const import CONF_ID, CONF_BATTERY_LEVEL, DEVICE_CLASS_CONNECTIVITY, DEVICE_CLASS_BATTERY, DEVICE_CLASS_DOOR, UNIT_PERCENT, ENTITY_CATEGORY_CONFIG
from esphome.const import CONF_ID, CONF_BATTERY_LEVEL, DEVICE_CLASS_CONNECTIVITY, DEVICE_CLASS_BATTERY, DEVICE_CLASS_DOOR, UNIT_PERCENT, ENTITY_CATEGORY_CONFIG, ENTITY_CATEGORY_DIAGNOSTIC

AUTO_LOAD = ["binary_sensor", "text_sensor", "sensor", "switch", "button"]

CONF_IS_CONNECTED = "is_connected"
CONF_IS_PAIRED = "is_paired"
CONF_IS_PAIRING = "is_pairing"
CONF_UNPAIR_BUTTON = "unpair"
CONF_PAIRING_MODE_SWITCH = "pairing_mode"
CONF_BATTERY_CRITICAL = "battery_critical"
CONF_BATTERY_LEVEL = "battery_level"
CONF_DOOR_SENSOR = "door_sensor"
Expand All @@ -19,20 +20,29 @@
NukiLock = nuki_lock_ns.class_('NukiLockComponent', lock.Lock, switch.Switch, cg.Component)

NukiLockUnpairButton = nuki_lock_ns.class_("NukiLockUnpairButton", button.Button, cg.Component)
NukiLockPairingModeSwitch = nuki_lock_ns.class_("NukiLockUnpairButton", switch.Switch, cg.Component)

NukiLockUnpairAction = nuki_lock_ns.class_(
"NukiLockUnpairAction", automation.Action
)

NukiLockPairingModeAction = nuki_lock_ns.class_(
"NukiLockPairingModeAction", automation.Action
)

CONFIG_SCHEMA = lock.LOCK_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(NukiLock),
cv.Required(CONF_IS_CONNECTED): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_CONNECTIVITY,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Required(CONF_IS_PAIRED): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_CONNECTIVITY,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Required(CONF_IS_PAIRING): binary_sensor.binary_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Required(CONF_IS_PAIRING): binary_sensor.binary_sensor_schema(),
cv.Optional(CONF_BATTERY_CRITICAL): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_BATTERY,
),
Expand All @@ -49,6 +59,11 @@
entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:link-off",
),
cv.Optional(CONF_PAIRING_MODE_SWITCH): switch.switch_schema(
NukiLockPairingModeSwitch,
entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:bluetooth",
),
}).extend(cv.polling_component_schema("500ms"))


Expand Down Expand Up @@ -89,6 +104,11 @@ async def to_code(config):
btn = await button.new_button(config[CONF_UNPAIR_BUTTON])
await cg.register_parented(btn, config[CONF_ID])
cg.add(var.set_unpair_button(btn))

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])
cg.add(var.set_pairing_mode_switch(sw))

@automation.register_action(
"nuki_lock.unpair",
Expand All @@ -100,5 +120,19 @@ async def to_code(config):
),
)
async def nuki_lock_unpair_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)


@automation.register_action(
"nuki_lock.pairing_mode",
NukiLockPairingModeAction,
automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(NukiLock),
}
),
)
async def nuki_lock_pairing_mode_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
Loading

0 comments on commit 0ab6e52

Please sign in to comment.