forked from ikalchev/HAP-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·51 lines (40 loc) · 1.61 KB
/
main.py
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
44
45
46
47
48
49
50
51
"""An example of how to setup and start an Accessory.
This is:
1. Create the Accessory object you want.
2. Add it to an AccessoryDriver, which will advertise it on the local network,
setup a server to answer client queries, etc.
"""
import logging
import signal
import time
import random
from pyhap.accessories.TemperatureSensor import TemperatureSensor
from pyhap.accessory import Bridge
from pyhap.accessory_driver import AccessoryDriver
import pyhap.loader as loader
logging.basicConfig(level=logging.INFO)
def get_bridge():
"""Call this method to get a Bridge instead of a standalone accessory."""
bridge = Bridge(display_name="Bridge")
temp_sensor = TemperatureSensor("Sensor 2")
temp_sensor2 = TemperatureSensor("Sensor 1")
bridge.add_accessory(temp_sensor)
bridge.add_accessory(temp_sensor2)
# Uncomment if you have RPi module and want a LED LightBulb service on pin 16.
# from pyhap.accessories.LightBulb import LightBulb
# bulb = LightBulb("Desk LED", pin=16)
# bridge.add_accessory(bulb)
return bridge
def get_accessory():
"""Call this method to get a standalone Accessory."""
acc = TemperatureSensor("MyTempSensor")
return acc
acc = get_accessory() # Change to get_bridge() if you want to run a Bridge.
# Start the accessory on port 51826
driver = AccessoryDriver(acc, port=51826)
# We want KeyboardInterrupts and SIGTERM (kill) to be handled by the driver itself,
# so that it can gracefully stop the accessory, server and advertising.
signal.signal(signal.SIGINT, driver.signal_handler)
signal.signal(signal.SIGTERM, driver.signal_handler)
# Start it!
driver.start()