Examples
Monitoring Example
Simple demonstration of connecting to a known device and printing out telemetry updates.
1"""Example usage of SolixBLE.
2
3.. moduleauthor:: Harvey Lelliott (flip-dots) <harveylelliott@duck.com>
4
5"""
6
7import asyncio
8import logging
9
10from SolixBLE import C300, discover_devices
11
12logging.basicConfig(level=logging.DEBUG)
13
14
15async def main():
16
17 # Find device
18 devices = await discover_devices()
19
20 selected_device = None
21 for device in devices:
22 if device.name is not None and "C300" in device.name:
23 selected_device = device
24 break
25
26 if selected_device is None:
27 print("Device not found!")
28 return
29
30 # Initialize the device
31 device = C300(selected_device)
32 # device = C1000(selected_device)
33
34 # Connect
35 connected = await device.connect()
36
37 if not connected:
38 raise Exception
39
40 # Do nothing, the library will print status updates in debug mode
41 await asyncio.sleep(900)
42
43
44if __name__ == "__main__":
45 asyncio.run(main())
Control Example
Simple demonstration of connecting to a known device and sending commands to control its outputs.
1"""Example usage of SolixBLE for controlling a C1000.
2
3.. moduleauthor:: Harvey Lelliott (flip-dots) <harveylelliott@duck.com>
4
5"""
6
7import asyncio
8import logging
9
10from SolixBLE import C1000, SolixBLEDevice, discover_devices
11from SolixBLE.states import LightStatus
12
13logging.basicConfig(level=logging.DEBUG)
14
15
16async def test_ac_output(device: SolixBLEDevice):
17
18 await asyncio.sleep(10)
19 await device.turn_ac_on()
20
21 await asyncio.sleep(10)
22 await device.turn_ac_off()
23
24
25async def test_light_mode(device: SolixBLEDevice):
26
27 await asyncio.sleep(5)
28 await device.set_light_mode(LightStatus.LOW)
29
30 await asyncio.sleep(5)
31 await device.set_light_mode(LightStatus.MEDIUM)
32
33 await asyncio.sleep(5)
34 await device.set_light_mode(LightStatus.HIGH)
35
36 await asyncio.sleep(5)
37 await device.set_light_mode(LightStatus.SOS)
38
39 await asyncio.sleep(5)
40 await device.set_light_mode(LightStatus.OFF)
41
42
43async def main():
44
45 # Find device
46 devices = await discover_devices()
47
48 selected_device = None
49 for device in devices:
50 if device.name is not None and "C1000" in device.name:
51 selected_device = device
52 break
53
54 if selected_device is None:
55 print("Device not found!")
56 return
57
58 # Initialize the device
59 # device = C300(selected_device)
60 device = C1000(selected_device)
61
62 # Connect
63 connected = await device.connect()
64
65 if not connected:
66 raise Exception
67
68 await test_light_mode(device)
69
70 await asyncio.sleep(300)
71
72
73if __name__ == "__main__":
74 asyncio.run(main())
Complex Example
This is a more advanced demonstration program which prompts the user for the device to connect to, its model, and then prints out the telemetry data on demand and when there is an update. This can be used to add support for new devices, see New devices.
1"""More advanced usage example of SolixBLE.
2
3.. moduleauthor:: Harvey Lelliott (flip-dots) <harveylelliott@duck.com>
4
5"""
6
7import asyncio
8import logging
9import sys
10
11# Allows for reading and writing to the console at the same time
12# pip3 install aioconsole
13from aioconsole import ainput
14from bleak import BLEDevice
15
16from SolixBLE import (
17 C300,
18 C300DC,
19 C800,
20 C1000,
21 C1000G2,
22 F2000,
23 F3800,
24 Generic,
25 PrimeCharger160w,
26 PrimeCharger250w,
27 Solarbank2,
28 Solarbank3,
29 SolixBLEDevice,
30 discover_devices,
31)
32
33MODELS = {
34 "C300": C300,
35 "C300DC": C300DC,
36 "C800": C800,
37 "C1000": C1000,
38 "C1000 G2": C1000G2,
39 "F2000 (767 PowerHouse)": F2000,
40 "F3800": F3800,
41 "Solarbank 2": Solarbank2,
42 "Solarbank 3": Solarbank3,
43 "PrimeCharger160w": PrimeCharger160w,
44 "PrimeCharger250w": PrimeCharger250w,
45 "Unknown": Generic,
46}
47
48
49async def prompt_debug_mode():
50 """
51 Prompt the user to enable/disable debug logging.
52 """
53 while True:
54
55 print("Show debug info? [Y/N]")
56 response = input().lower()
57
58 if response == "y":
59 logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
60 break
61
62 elif response == "n":
63 break
64
65
66async def prompt_select_device(devices: list[BLEDevice]) -> SolixBLEDevice:
67 """
68 Prompt the user to select the device and model they want to connect to.
69
70 :param devices: List of discovered power stations.
71 :returns: Selected power station with initialized class.
72 """
73
74 # Get Bluetooth device
75 print("Which device would you like to connect to?")
76 for i in range(1, len(devices) + 1):
77 device = devices[i - 1]
78 print(f"""{i}. "{device.name}" ({device.address}) """)
79 ble_device = devices[int(await ainput("> ")) - 1]
80
81 # Get model/class of device
82 print("What model is this device?")
83 model_keys = list(MODELS.keys())
84 model_values = list(MODELS.values())
85 for i in range(1, len(MODELS) + 1):
86 print(f"""{i}. "{model_keys[i - 1]}" """)
87 device_class = model_values[int(await ainput("> ")) - 1]
88
89 # Return instantiated device
90 return device_class(ble_device)
91
92
93async def main():
94 """
95 Main program loop.
96
97 This prompts the user for logging mode.
98 Looks for power stations.
99 Asks user to select a power station.
100 Asks user to select the model of the power station.
101 Connects to the power station.
102 Prints status updates of the power station.
103 """
104
105 # Ask user for desired logging mode
106 await prompt_debug_mode()
107
108 # Find power stations
109 devices: list[BLEDevice] = []
110 while not devices:
111
112 print("Looking for power station...")
113 devices = await discover_devices()
114 if not devices:
115 print("No devices found! Trying again...")
116
117 # Ask user to select power station and model
118 device = await prompt_select_device(devices)
119
120 # Register callback
121 def my_callback():
122 """
123 Callback executed by library whenever there is a telemetry update.
124 """
125 print("==== REMOTE STATE CHANGE DETECTED ====")
126 print(device)
127 print("==== END OF STATE CHANGE REPORT ====")
128
129 device.add_callback(my_callback)
130
131 # Connect to device
132 await device.connect()
133
134 # Prompt user for action
135 while True:
136 print("What action would you like to perform?")
137 print("1. Print state")
138 print("2. Exit")
139
140 try:
141 action = int(await ainput("> ")) - 1
142 match action:
143
144 # Print state of device
145 case 0:
146 print(device)
147
148 # Exit program
149 case 1:
150 await device.disconnect()
151 print("Goodbye :)")
152 return
153
154 except ValueError:
155 pass
156
157
158if __name__ == "__main__":
159 asyncio.run(main())