api: automatically rediscover locks from CCUJack
All checks were successful
Build Container / Build Container (push) Successful in 1m35s

closes CCCHH/dooris#5
This commit is contained in:
lilly 2026-05-19 15:03:26 +02:00
commit 2349e58924
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
3 changed files with 24 additions and 5 deletions

View file

@ -72,6 +72,7 @@ class CCUJackClient:
locks: LockData
param_values: Dict[str, Any]
task_process_messages: asyncio.Task
task_find_locks: asyncio.Task
data_updated: asyncio.Event
def __init__(self, base_uri: str, auth: BasicAuth, mqtt_conn: str):
@ -85,10 +86,16 @@ class CCUJackClient:
self.locks = None
self.param_values = dict()
self.task_process_messages = None
self.task_find_locks = None
self.data_updated = asyncio.Event()
async def connect_mqtt(self):
async def start(self):
await self.mqtt.connect()
await self.find_locks()
self.task_find_locks = asyncio.get_running_loop().create_task(
self.cron(), name="ccujack-cron"
)
self.task_process_messages = asyncio.get_running_loop().create_task(
self.process_mqt_messages(), name="process-mqtt-messages"
)
@ -116,6 +123,7 @@ class CCUJackClient:
# save the result
new_locks = [i for i in device_infos if i[0].type == DEVICE_TYPE_LOCK]
if new_locks != self.locks:
logger.info("Found new locks, updating state")
self.locks = new_locks
self.data_updated.set()
self.data_updated.clear()
@ -148,6 +156,17 @@ class CCUJackClient:
finally:
self.data_updated.clear()
async def cron(self):
while True:
try:
await asyncio.sleep(60 * 60) # 1 hour
logger.info("Running CCUJack cron")
await self.find_locks()
except Exception as e:
logger.exception(f"Error in CCUJack cron task: {e}")
async def query_param_value(self, address: str) -> CCUValue:
if address in self.param_values:
return self.param_values[address]