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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
from __future__ import annotations
import datetime
from enum import Enum
from ._compat import cached_property
from .bridge import BridgeDetail, BridgeStatus
from .device import Device, DeviceDetail
from .keypad import KeypadDetail
from .time import parse_datetime
from .users import cache_user_info
LOCKED_STATUS = ("lock", "locked", "kAugLockState_Locked", "kAugLockState_SecureMode")
LOCKING_STATUS = ("kAugLockState_Locking",)
UNLATCHED_STATUS = ("unlatched", "kAugLockState_Unlatched")
UNLATCHING_STATUS = ("kAugLockState_Unlatching",)
UNLOCKED_STATUS = ("unlock", "unlocked", "kAugLockState_Unlocked")
UNLOCKING_STATUS = ("kAugLockState_Unlocking",)
JAMMED_STATUS = (
"kAugLockState_UnknownStaticPosition",
"FAILED_BRIDGE_ERROR_LOCK_JAMMED",
)
CLOSED_STATUS = ("closed", "kAugLockDoorState_Closed", "kAugDoorState_Closed")
OPEN_STATUS = ("open", "kAugLockDoorState_Open", "kAugDoorState_Open")
DISABLE_STATUS = ("init", "kAugDoorState_Init", "", None)
LOCK_STATUS_KEY = "status"
DOOR_STATE_KEY = "doorState"
DOORMAN_MODEL_TYPES = {7, 10}
UNLATCH_MODEL_TYPES = {17}
# Type 7 is a doorman
# Type 10 is a doorman in the Swedish market
# Type 17 is a Linus L2
class Lock(Device):
def __init__(self, device_id, data):
super().__init__(
device_id,
data["LockName"],
data["HouseID"],
)
self._user_type = data["UserType"]
@cached_property
def is_operable(self):
return self._user_type == "superuser"
def __repr__(self):
return f"Lock(id={self.device_id}, name={self.device_name}, house_id={self.house_id})"
class LockDetail(DeviceDetail):
def __init__(self, data):
super().__init__(
data["LockID"],
data["LockName"],
data["HouseID"],
data["SerialNumber"],
data["currentFirmwareVersion"],
data.get("pubsubChannel"),
data,
)
if "Bridge" in data:
self._bridge = BridgeDetail(self.house_id, data["Bridge"])
else:
self._bridge = None
self._doorsense = False
self._lock_status = LockStatus.UNKNOWN
self._door_state = LockDoorStatus.UNKNOWN
self._lock_status_datetime = None
self._door_state_datetime = None
self._model = None
if "LockStatus" in data:
lock_status = data["LockStatus"]
self._lock_status = determine_lock_status(lock_status.get(LOCK_STATUS_KEY))
self._door_state = determine_door_state(lock_status.get(DOOR_STATE_KEY))
if "dateTime" in lock_status:
self._lock_status_datetime = parse_datetime(lock_status["dateTime"])
self._door_state_datetime = self._lock_status_datetime
if (
DOOR_STATE_KEY in lock_status
and self._door_state != LockDoorStatus.DISABLED
):
self._doorsense = True
if "keypad" in data:
keypad_name = data["LockName"] + " Keypad"
self._keypad_detail = KeypadDetail(
self.house_id, keypad_name, data["keypad"]
)
else:
self._keypad_detail = None
self._battery_level = int(100 * data["battery"])
if "users" in data:
for uuid, user_data in data["users"].items():
cache_user_info(uuid, user_data)
if "skuNumber" in data:
self._model = data["skuNumber"]
self._data = data
@cached_property
def model(self):
return self._model
@cached_property
def doorbell(self) -> bool:
return self._data["Type"] in DOORMAN_MODEL_TYPES
@cached_property
def unlatch_supported(self) -> bool:
return self._data["Type"] in UNLATCH_MODEL_TYPES
@cached_property
def battery_level(self):
return self._battery_level
@cached_property
def keypad(self):
return self._keypad_detail
@cached_property
def bridge(self):
return self._bridge
@property
def bridge_is_online(self):
if self._bridge is None:
return False
# Old style bridge that does not report current status
# This may have been updated but I do not have a Gen2
# doorbell to test with yet.
if self._bridge.status is None and self._bridge.operative:
return True
if (
self._bridge.status is not None
and self._bridge.status.current == BridgeStatus.ONLINE
):
return True
return False
@cached_property
def doorsense(self):
return self._doorsense
@property
def lock_status(self):
return self._lock_status
@lock_status.setter
def lock_status(self, var):
"""Update the lock status (usually form the activity log)."""
if var not in LockStatus:
raise ValueError
self._lock_status = var
@property
def lock_status_datetime(self):
return self._lock_status_datetime
@lock_status_datetime.setter
def lock_status_datetime(self, var):
"""Update the lock status datetime (usually form the activity log)."""
if not isinstance(var, datetime.date):
raise ValueError
self._lock_status_datetime = var
@property
def door_state(self):
return self._door_state
@door_state.setter
def door_state(self, var):
"""Update the door state (usually form the activity log)."""
if var not in LockDoorStatus:
raise ValueError
self._door_state = var
if var != LockDoorStatus.UNKNOWN:
self._doorsense = True
@property
def door_state_datetime(self):
return self._door_state_datetime
@door_state_datetime.setter
def door_state_datetime(self, var):
"""Update the door state datetime (usually form the activity log)."""
if not isinstance(var, datetime.date):
raise ValueError
self._door_state_datetime = var
def set_online(self, state):
"""Called when the lock comes back online or goes offline."""
if not self._bridge:
return
self._bridge.set_online(state)
def get_user(self, user_id):
"""Lookup user data by id."""
return self._data.get("users", {}).get(user_id)
@cached_property
def offline_keys(self) -> dict:
return self._data.get("OfflineKeys", {})
@cached_property
def loaded_offline_keys(self) -> list[dict]:
return self.offline_keys.get("loaded", [])
@cached_property
def offline_key(self) -> str | None:
loaded_offline_keys = self.loaded_offline_keys
if loaded_offline_keys and "key" in loaded_offline_keys[0]:
return loaded_offline_keys[0]["key"]
return None
@cached_property
def offline_slot(self) -> int | None:
loaded_offline_keys = self.loaded_offline_keys
if loaded_offline_keys and "slot" in loaded_offline_keys[0]:
return loaded_offline_keys[0]["slot"]
return None
@cached_property
def mac_address(self) -> str | None:
mac = self._data.get("macAddress")
return mac.upper() if mac else None
class LockStatus(Enum):
LOCKED = "locked"
UNLATCHED = "unlatched"
UNLOCKED = "unlocked"
UNKNOWN = "unknown"
LOCKING = "locking"
UNLATCHING = "unlatching"
UNLOCKING = "unlocking"
JAMMED = "jammed"
class LockDoorStatus(Enum):
CLOSED = "closed"
OPEN = "open"
UNKNOWN = "unknown"
DISABLED = "disabled"
def determine_lock_status(status: str) -> LockStatus:
if status in LOCKED_STATUS:
return LockStatus.LOCKED
if status in UNLATCHED_STATUS:
return LockStatus.UNLATCHED
if status in UNLOCKED_STATUS:
return LockStatus.UNLOCKED
if status in UNLATCHING_STATUS:
return LockStatus.UNLATCHING
if status in UNLOCKING_STATUS:
return LockStatus.UNLOCKING
if status in LOCKING_STATUS:
return LockStatus.LOCKING
if status in JAMMED_STATUS:
return LockStatus.JAMMED
return LockStatus.UNKNOWN
def determine_door_state(status):
if status in CLOSED_STATUS:
return LockDoorStatus.CLOSED
if status in OPEN_STATUS:
return LockDoorStatus.OPEN
if status in DISABLE_STATUS:
return LockDoorStatus.DISABLED
return LockDoorStatus.UNKNOWN
def door_state_to_string(door_status):
"""Returns the normalized value that determine_door_state represents."""
if door_status == LockDoorStatus.OPEN:
return "dooropen"
if door_status == LockDoorStatus.CLOSED:
return "doorclosed"
raise ValueError
|