Skip to content

Usage

Example Usage

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from pprint import pprint
from typing import TYPE_CHECKING, Union

from pysesame3.auth import CognitoAuth, WebAPIAuth
from pysesame3.chsesame2 import CHSesame2  # For SESAME 3, 4
from pysesame3.chsesamebot import CHSesameBot  # For SESAME bot
from pysesame3.helper import CHProductModel

if TYPE_CHECKING:
    from pysesame3.device import SesameLocker
    from pysesame3.helper import CHSesame2MechStatus, CHSesameBotMechStatus


def callback(
    device: "SesameLocker", status: Union["CHSesame2MechStatus", "CHSesameBotMechStatus"]
):
    print("=" * 10)
    print("mechStatus is updated!")
    print("UUID: {}".format(device.getDeviceUUID()))
    print("Product Model: {}".format(device.productModel))
    print("Battery: {}%".format(status.getBatteryPercentage()))
    print("Battery: {:.2f}V".format(status.getBatteryVoltage()))
    print("isInLockRange: {}".format(status.isInLockRange()))
    print("isInUnlockRange: {}".format(status.isInUnlockRange()))
    if device.productModel == CHProductModel.SS2:
        print("Position: {}".format(status.getPosition()))
    elif device.productModel == CHProductModel.SesameBot1:
        print("Motor Status: {}".format(status.getMotorStatus()))
    print("=" * 10)


def main():
    """
    We have use two different authentication methods.
    `CognitoAuth` is not available by default.
    If you want to use it, run `pip install pysesame3[cognito]` instead of `pip install pysesame3`.

    WebAPIAuth: (Common) Using the Web API.
    CognitoAuth: (Optional) Behave like a mobile app.
    """
    # auth = WebAPIAuth(apikey="API_KEY")
    auth = CognitoAuth(apikey="API_KEY")

    """
    By decoding the QR code generated by the official iOS/Android app,
    you can get the information you need.
    https://sesame-qr-reader.vercel.app/
    """
    your_key_uuid = "UUID"
    your_key_secret = "SECRET_KEY"

    """
    This library supports SESAME 3, SESAME 4, and SESAME bot.

    `CHSesame2`: SESAME 3, 4
    `CHSesameBot`: SESAME bot
    """
    device = CHSesame2(
        authenticator=auth, device_uuid=your_key_uuid, secret_key=your_key_secret
    )

    """
    As the name implies, `mechStatus` indicates the mechanical status of the key.
    https://doc.candyhouse.co/ja/reference#chsesameprotocolmechstatus

    Please note that `mechStatus` always queries the server for the latest status.
    Calling it too often would stress the service, which leads to rate limits
    and other restrictions.

    On the other hand, `getDeviceShadowStatus` does not query the server,
    but returns a **shadow** which is the status **stored in this library**.

    If you are operating the key only in this library, ideally,
    this shadow will be perfectly consistent with the actual state.
    """
    print("=" * 10)
    print("[Initial MechStatus]")
    print((str(device.mechStatus)))

    """
    The reality, however, is not so simple. The key must be manually operated.
    Biggest problem here is that the Candy House server will not return an error
    even if the request was not actually processed successfully.

    But if you are using `CognitoAuth`, you can achieve magic!
    The mobile app shows the status of the key in almost real time.
    In the same way, you can **subscribe** to `mechStatus`.
    """
    device.subscribeMechStatus(callback)

    """
    It also supports environments where multiple locks are being installed.

    your_2nd_key_uuid = "UUID"
    your_2nd_key_secret = "SECRET_KEY"

    device2 = CHSesame2(
        authenticator=auth,
        device_uuid=your_2nd_key_uuid,
        secret_key=your_2nd_key_secret,
    )
    device2.subscribeMechStatus(callback)
    """

    print("=" * 10)
    print("[History]")
    for entry in device.historyEntries:
        pprint(entry.to_dict())

    print("=" * 10)
    print("[Prompt]")
    while True:
        val = input("Action [lock/unlock/toggle/click]: ")

        if device.productModel in [CHProductModel.SS2, CHProductModel.SS4]:
            if val == "lock":
                device.lock(history_tag="My Script")
            elif val == "unlock":
                device.unlock(history_tag="My Script")
            elif val == "toggle":
                device.toggle(history_tag="My Script")

        if device.productModel == CHProductModel.SesameBot1:
            if val == "click":
                device.click(history_tag="My Script")

        continue


if __name__ == "__main__":
    main()