|
1 | 1 | import asyncio
|
2 | 2 | import sys
|
3 | 3 |
|
4 |
| -from bleak.backends.device import BLEDevice |
| 4 | +from bleak import BleakScanner |
5 | 5 |
|
6 | 6 | from pyfreshintellivent import FreshIntelliVent
|
7 | 7 |
|
8 | 8 |
|
9 | 9 | async def main():
|
10 |
| - sky = FreshIntelliVent() |
| 10 | + client = FreshIntelliVent() |
11 | 11 | address = sys.argv[1]
|
12 |
| - authentication_code = sys.argv[2] |
| 12 | + authentication_code = None |
| 13 | + if len(sys.argv) == 3: |
| 14 | + sys.argv[2] |
13 | 15 |
|
14 |
| - ble_device = BLEDevice(address=address) |
| 16 | + ble_device = await BleakScanner.find_device_by_address(address) |
| 17 | + |
| 18 | + if ble_device is None: |
| 19 | + print("Couldn't find any devices") |
| 20 | + return |
15 | 21 |
|
16 | 22 | try:
|
17 |
| - async with sky.connect(ble_device) as client: |
| 23 | + print("Connecting...") |
| 24 | + await client.connect(ble_device) |
| 25 | + print("Connected") |
| 26 | + |
| 27 | + await client.fetch_device_information() |
| 28 | + |
| 29 | + if authentication_code is None: |
| 30 | + print("No authentication code, skipping authentication") |
| 31 | + else: |
18 | 32 | await client.authenticate(authentication_code=authentication_code)
|
| 33 | + print("Authenticated") |
19 | 34 |
|
20 |
| - sensors = await client.fetch_sensor_data() |
21 |
| - print(f"Status: {sensors.as_dict()}") |
| 35 | + sensors = await client.fetch_sensor_data() |
| 36 | + print(f"Status: {sensors.as_dict()}") |
22 | 37 |
|
23 |
| - boost = await client.fetch_boost() |
24 |
| - print(f"Boost: {boost}") |
| 38 | + boost = await client.fetch_boost() |
| 39 | + print(f"Boost: {boost}") |
25 | 40 |
|
26 |
| - constant_speed = await client.fetch_constant_speed() |
27 |
| - print(f"Constant speed: {constant_speed}") |
| 41 | + constant_speed = await client.fetch_constant_speed() |
| 42 | + print(f"Constant speed: {constant_speed}") |
28 | 43 |
|
29 |
| - humidity = await client.fetch_humidity() |
30 |
| - print(f"Humidity: {humidity}") |
| 44 | + humidity = await client.fetch_humidity() |
| 45 | + print(f"Humidity: {humidity}") |
31 | 46 |
|
32 |
| - light_and_voc = await client.fetch_light_and_voc() |
33 |
| - print(f"Light and VOC: {light_and_voc}") |
| 47 | + light_and_voc = await client.fetch_light_and_voc() |
| 48 | + print(f"Light and VOC: {light_and_voc}") |
34 | 49 |
|
35 |
| - pause = await client.fetch_pause() |
36 |
| - print(f"Pause: {pause}") |
| 50 | + pause = await client.fetch_pause() |
| 51 | + print(f"Pause: {pause}") |
37 | 52 |
|
38 |
| - timer = await client.fetch_timer() |
39 |
| - print(f"Timer: {timer}") |
| 53 | + timer = await client.fetch_timer() |
| 54 | + print(f"Timer: {timer}") |
40 | 55 | except Exception as e:
|
| 56 | + await client.disconnect() |
41 | 57 | print(e)
|
42 | 58 |
|
43 | 59 |
|
44 |
| -asyncio.run(main()) |
| 60 | +if __name__ == "__main__": |
| 61 | + asyncio.run(main()) |
0 commit comments