|
| 1 | +"""Example script demonstrating xComfort Bridge usage.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import asyncio |
| 5 | +import logging |
| 6 | + |
| 7 | +from xcomfort import Bridge |
| 8 | + |
| 9 | +_LOGGER = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def observe_device(device): |
| 13 | + """Subscribe to device state changes and log them.""" |
| 14 | + device.state.subscribe( |
| 15 | + lambda state: _LOGGER.info( |
| 16 | + "Device state [%s] '%s': %s", device.device_id, device.name, state |
| 17 | + ) |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +async def main(ip: str, auth_key: str): |
| 22 | + """Run the main example demonstrating bridge connection and device observation.""" |
| 23 | + bridge = Bridge(ip, auth_key) |
| 24 | + |
| 25 | + runTask = asyncio.create_task(bridge.run()) |
| 26 | + |
| 27 | + devices = await bridge.get_devices() |
| 28 | + |
| 29 | + for device in devices.values(): |
| 30 | + observe_device(device) |
| 31 | + |
| 32 | + # Wait 50 seconds. Try flipping the light switch manually while you wait |
| 33 | + await asyncio.sleep(50) |
| 34 | + |
| 35 | + # Turn off all the lights. |
| 36 | + # for device in devices.values(): |
| 37 | + # await device.switch(False) |
| 38 | + # |
| 39 | + # await asyncio.sleep(5) |
| 40 | + |
| 41 | + await bridge.close() |
| 42 | + await runTask |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + # Configure debug logging |
| 46 | + logging.basicConfig( |
| 47 | + level=logging.DEBUG, |
| 48 | + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
| 49 | + ) |
| 50 | + |
| 51 | + parser = argparse.ArgumentParser(description="Test xComfort Bridge connection") |
| 52 | + parser.add_argument("--ip", required=True, help="IP address of the xComfort Bridge") |
| 53 | + parser.add_argument("--auth-key", required=True, help="Authentication key for the xComfort Bridge") |
| 54 | + |
| 55 | + args = parser.parse_args() |
| 56 | + |
| 57 | + asyncio.run(main(args.ip, args.auth_key)) |
0 commit comments