Skip to content

Commit

Permalink
Restructure Component
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Oct 23, 2024
1 parent 819e5a0 commit 52bb5d7
Show file tree
Hide file tree
Showing 20 changed files with 534 additions and 335 deletions.
117 changes: 117 additions & 0 deletions components/nuki_lock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import lock
from esphome.const import (
CONF_ID,
CONF_TRIGGER_ID
)

AUTO_LOAD = []

CONF_NUKI_LOCK_ID = "nuki_lock_id"

CONF_SECURITY_PIN = "security_pin"
CONF_PAIRING_TIMEOUT = "pairing_timeout"

CONF_SET_PAIRING_MODE = "pairing_mode"

CONF_ON_PAIRING_MODE_ON = "on_pairing_mode_on_action"
CONF_ON_PAIRING_MODE_OFF = "on_pairing_mode_off_action"
CONF_ON_PAIRED = "on_paired_action"

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

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.Optional(CONF_PAIRING_TIMEOUT, default="300s"): cv.positive_time_period_seconds,
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.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PairedTrigger),
}
),
}).extend(cv.polling_component_schema("500ms"))


async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await lock.register_lock(var, config)
if CONF_SECURITY_PIN in config:
cg.add(var.set_security_pin(config[CONF_SECURITY_PIN]))

if CONF_PAIRING_TIMEOUT in config:
cg.add(var.set_pairing_timeout(config[CONF_PAIRING_TIMEOUT]))

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)



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

NUKI_LOCK_UNPAIR_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.use_id(NukiLock)
}
)

@automation.register_action(
"nuki_lock.unpair", NukiLockUnpairAction, NUKI_LOCK_UNPAIR_SCHEMA
)

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)




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

NUKI_LOCK_SET_PAIRING_MODE_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.use_id(NukiLock),
cv.Required(CONF_SET_PAIRING_MODE): cv.templatable(cv.boolean)
}
)

@automation.register_action(
"nuki_lock.set_pairing_mode", NukiLockPairingModeAction, NUKI_LOCK_SET_PAIRING_MODE_SCHEMA
)

async def nuki_lock_set_pairing_mode_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_SET_PAIRING_MODE], args, cg.bool_)
cg.add(var.set_pairing_mode(template_))
return var
53 changes: 53 additions & 0 deletions components/nuki_lock/binary_sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import (
ENTITY_CATEGORY_DIAGNOSTIC,
DEVICE_CLASS_CONNECTIVITY,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_DOOR
)
from .. import CONF_NUKI_LOCK_ID, NukiLock, nuki_lock_ns

CONF_IS_CONNECTED = "is_connected"
CONF_IS_PAIRED = "is_paired"
CONF_BATTERY_CRITICAL = "battery_critical"
CONF_BATTERY_LEVEL = "battery_level"
CONF_DOOR_SENSOR = "door_sensor"

CONFIG_SCHEMA = {
cv.GenerateID(CONF_NUKI_LOCK_ID): cv.use_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.Optional(CONF_BATTERY_CRITICAL): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_BATTERY,
),
cv.Optional(CONF_DOOR_SENSOR): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_DOOR,
),
}

async def to_code(config):
nuki_lock_component = await cg.get_variable(config[CONF_NUKI_LOCK_ID])

if is_connected := config.get(CONF_IS_CONNECTED):
sens = await binary_sensor.new_binary_sensor(is_connected)
cg.add(nuki_lock_component.set_is_connected(sens))

if is_paired := config.get(CONF_IS_PAIRED):
sens = await binary_sensor.new_binary_sensor(is_paired)
cg.add(nuki_lock_component.set_is_paired(sens))

if battery_critical := config.get(CONF_BATTERY_CRITICAL):
sens = await binary_sensor.new_binary_sensor(battery_critical)
cg.add(nuki_lock_component.set_battery_critical(sens))

if door_sensor := config.get(CONF_DOOR_SENSOR):
sens = await binary_sensor.new_binary_sensor(door_sensor)
cg.add(nuki_lock_component.set_door_sensor(sens))
28 changes: 28 additions & 0 deletions components/nuki_lock/button/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import esphome.codegen as cg
from esphome.components import button
import esphome.config_validation as cv
from esphome.const import (
ENTITY_CATEGORY_CONFIG,
)
from .. import CONF_NUKI_LOCK_ID, NukiLock, nuki_lock_ns

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

CONF_UNPAIR_BUTTON = "unpair"

CONFIG_SCHEMA = {
cv.GenerateID(CONF_NUKI_LOCK_ID): cv.use_id(NukiLock),
cv.Optional(CONF_UNPAIR_BUTTON): button.button_schema(
NukiLockUnpairButton,
entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:link-off",
),
}

async def to_code(config):
nuki_lock_component = await cg.get_variable(config[CONF_NUKI_LOCK_ID])

if unpair := config.get(CONF_UNPAIR_BUTTON):
b = await button.new_button(unpair)
await cg.register_parented(b, config[CONF_NUKI_LOCK_ID])
cg.add(nuki_lock_component.set_unpair_button(b))
15 changes: 15 additions & 0 deletions components/nuki_lock/button/unpair_button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "unpair_button.h"

namespace esphome {
namespace nuki_lock {

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

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

} // namespace nuki_lock
} // namespace esphome
19 changes: 19 additions & 0 deletions components/nuki_lock/button/unpair_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "esphome/components/button/button.h"
#include "../nuki_lock.h"

namespace esphome {
namespace nuki_lock {

class NukiLockUnpairButton : public button::Button, public Parented<NukiLockComponent> {
public:
NukiLockUnpairButton() = default;

protected:
void press_action() override;
void dump_config() override;
};

} // namespace nuki_lock
} // namespace esphome
Loading

0 comments on commit 52bb5d7

Please sign in to comment.