api: process mqtt messages to keep a current local lock state
This commit is contained in:
parent
44d484cfc1
commit
4103c0ca5f
2 changed files with 53 additions and 15 deletions
|
|
@ -1,4 +1,4 @@
|
|||
from typing import List, Tuple, Optional, Any
|
||||
from typing import List, Tuple, Optional, Any, Dict
|
||||
from aiohttp import ClientSession, BasicAuth, TCPConnector
|
||||
import logging
|
||||
import asyncio
|
||||
|
|
@ -70,6 +70,8 @@ LockData = List[Tuple[CCUDeviceInfo, List[Tuple[CCUChannelInfo, List[CCUParamInf
|
|||
class CCUJackClient:
|
||||
base_uri: str
|
||||
locks: LockData
|
||||
param_values: Dict[str, Any]
|
||||
task_process_messages: asyncio.Task
|
||||
|
||||
def __init__(self, base_uri: str, auth: BasicAuth, mqtt_conn: str):
|
||||
self.http = ClientSession(
|
||||
|
|
@ -78,17 +80,21 @@ class CCUJackClient:
|
|||
raise_for_status=True,
|
||||
connector=TCPConnector(ssl=False),
|
||||
)
|
||||
self.locks = None
|
||||
self.mqtt = AsyncMqttClient(mqtt_conn, auth.login, auth.password)
|
||||
self.locks = None
|
||||
self.param_values = dict()
|
||||
self.task_process_messages = None
|
||||
|
||||
async def connect_mqtt(self):
|
||||
await self.mqtt.connect()
|
||||
self.task_process_messages = asyncio.get_running_loop().create_task(
|
||||
self.process_mqt_messages(), name="process-mqtt-messages"
|
||||
)
|
||||
|
||||
async def close_connections(self):
|
||||
await asyncio.gather(
|
||||
self.mqtt.disconnect(),
|
||||
self.http.close()
|
||||
)
|
||||
await asyncio.gather(self.mqtt.disconnect(), self.http.close())
|
||||
self.task_process_messages.cancel()
|
||||
self.task_process_messages = None
|
||||
|
||||
async def find_locks(self):
|
||||
logger.debug("Inspecting lock devices present in CCUJack")
|
||||
|
|
@ -112,10 +118,30 @@ class CCUJackClient:
|
|||
for i_lock, lock_channels in self.locks:
|
||||
for i_channel, channel_params in lock_channels:
|
||||
for i_param in channel_params:
|
||||
mqtt_topics.add(f"device/status/{i_lock.address}/{i_channel.index}/{i_param.id}")
|
||||
# await self.mqtt.update_subscriptions(mqtt_topics)
|
||||
mqtt_topics.add(
|
||||
f"device/status/{i_lock.address}/{i_channel.index}/{i_param.id}"
|
||||
)
|
||||
await self.mqtt.update_subscriptions(mqtt_topics)
|
||||
|
||||
async def process_mqt_messages(self):
|
||||
while True:
|
||||
try:
|
||||
msg = await self.mqtt.messages.get()
|
||||
|
||||
param_name = msg.topic.removeprefix("device/status/")
|
||||
param_value = CCUValue.model_validate_json(msg.payload)
|
||||
logger.debug(
|
||||
f"Got new value from MQTT for parameter {param_name}: {param_value}"
|
||||
)
|
||||
self.param_values[param_name] = param_value
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(f"could not process incoming mqtt message: {e}")
|
||||
|
||||
async def query_param_value(self, address: str) -> CCUValue:
|
||||
if address in self.param_values:
|
||||
return self.param_values[address]
|
||||
|
||||
async def query_param_value(self, address: str):
|
||||
logger.debug("Querying parameter value from '%s'", address)
|
||||
async with self.http.get(f"/device/{address}/~pv") as resp:
|
||||
return CCUValue.model_validate(await resp.json())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue