Skip to content

[Question] Locks - how does event subscription work? #489

@Talkabout

Description

@Talkabout

HI guys,

thanks for the library, it works quite nice with my lock ultra. There is one thing I seem to not understand: how do I register for events when the lock is triggered via app or manually?
There seem to be a "subscribe" method available, which I can use to receive event data. But this only works for actions executed via the library itself. Is this expected when using bluetooth connection or can I also receive events for actions that are triggered elsewhere? My assumption would be that if I unlock manually, the bluetooth connection receives the corresponding event and triggers a notification, but it seems to not work that way. Anybody has a solution for this use use?
The current test script I am using:

import asyncio
import time
from switchbot.discovery import GetSwitchbotDevices
from switchbot.devices import lock
from switchbot.const import SwitchbotModel

BLE_MAC="---------------------" # The MAC of your lock
KEY_ID="--------------" # The key-ID of your encryption-key for your lock
ENC_KEY="-----------------------" # The encryption-key with key-ID "XX"
LOCK_MODEL=SwitchbotModel.LOCK_ULTRA # Your lock model (here we use the Lock-Pro)

async def main():
    wolock = await GetSwitchbotDevices().discover()
    #wolock = await GetSwitchbotDevices().get_locks()
    device = lock.SwitchbotLock(
        wolock[BLE_MAC].device, KEY_ID, ENC_KEY, model=LOCK_MODEL
    )
    loop = asyncio.get_running_loop()
    def on():
        asyncio.run_coroutine_threadsafe(get_device_info(device), loop)
    device.subscribe(on);
    #await device.lock()
    #await device.unlock()
    await device.unlock_without_unlatch()
    while True:
        time.sleep(1)

async def get_device_info(device):
    data = await device.get_basic_info();
    print(data)


asyncio.run(main())

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions