predavalnice-kontroler/lucke/luckeControl.py

50 lines
No EOL
1.6 KiB
Python

from http import HTTPStatus
import aiomqtt
import asyncio
import toml
import aiohttp
lucke_bearer_token = ""
room = ""
url = ""
roomId = 0
async def sendSceneRequest(client, scene):
global roomId
endpoint = url.format(roomId=roomId, sceneId=scene)
# Content-Type needs to be JSON, but the content itself is ignored
async with aiohttp.request("POST", endpoint, headers={"Authorization": "Bearer " + lucke_bearer_token}, json={}) as resp:
#if 204 change was made
if resp.status != 204:
print(resp.status, await resp.text())
await client.publish(f'{room}/lucke/preset/current', payload=scene, qos=1, retain=True)
async def task_luckeCommand(controlClient):
await controlClient.subscribe(f"{room}/lucke/preset/recall")
msgs = controlClient.messages
async for mesg in msgs:
mesg: aiomqtt.Message
msgTopicArr = mesg.topic.value.split('/')
sceneNum = mesg.payload.decode()
print("Received: " + str(msgTopicArr) + " payload: [" + sceneNum + "]")
print('sending request to endpoint..')
await sendSceneRequest(controlClient, sceneNum)
await asyncio.sleep(0.01)
async def main():
global room, lucke_bearer_token, url, roomId
conf = toml.load('./malinaConfig.toml')
room = conf.get("global")['room']
url = conf.get("lucke")['url']
roomId = conf.get("lucke")['roomId']
lucke_bearer_token = conf.get("lucke")['bearer_token']
async with aiomqtt.Client('localhost', 1883) as client:
task_control = asyncio.create_task(task_luckeCommand(client))
await asyncio.gather(task_control)
if __name__ == '__main__':
asyncio.run(main())