-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
43 lines (32 loc) · 1.16 KB
/
main.py
File metadata and controls
43 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""main script to run the fan. call with `python3 main.py`."""
import asyncio
import signal
import pathlib
import logging
import configparser
from pyhap.accessory_driver import AccessoryDriver
from purelinkhomekit import homekit_fan
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
CONFIG_FILEPATH = str(pathlib.PurePath(__file__).parent) + "/user.ini"
PERSIST_FILEPATH = (str(pathlib.PurePath(__file__).parent)
+ "/fan_device.state")
# read fan config file
config = configparser.ConfigParser()
config.read(CONFIG_FILEPATH)
user = config['User']
username = user['id']
password = user['pass']
loop = asyncio.get_event_loop()
driver = AccessoryDriver(persist_file=PERSIST_FILEPATH,
loop=loop)
fan = homekit_fan.HomekitFan(username, password, driver)
driver.add_accessory(accessory=fan)
def signal_handler(_sig, _frame):
"""custom signal handler with time to shut down the fan connection."""
driver.stop()
# kill the run loop later with some time for
# things to shut down
loop.call_later(4, loop.stop)
signal.signal(signal.SIGINT, signal_handler)
driver.start()