-
Notifications
You must be signed in to change notification settings - Fork 119
/
main.py
executable file
·64 lines (45 loc) · 1.89 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
52
53
54
55
56
57
58
59
60
61
62
63
64
"""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 random
from pyhap.accessory import Accessory, Bridge
from pyhap.accessory_driver import AccessoryDriver
import pyhap.loader as loader
from pyhap import camera
from pyhap.const import CATEGORY_SENSOR
logging.basicConfig(level=logging.INFO, format="[%(module)s] %(message)s")
class TemperatureSensor(Accessory):
"""Fake Temperature sensor, measuring every 3 seconds."""
category = CATEGORY_SENSOR
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
serv_temp = self.add_preload_service('TemperatureSensor')
self.char_temp = serv_temp.configure_char('CurrentTemperature')
@Accessory.run_at_interval(3)
async def run(self):
self.char_temp.set_value(random.randint(18, 26))
def get_bridge(driver):
"""Call this method to get a Bridge instead of a standalone accessory."""
bridge = Bridge(driver, 'Bridge')
temp_sensor = TemperatureSensor(driver, 'Sensor 2')
temp_sensor2 = TemperatureSensor(driver, 'Sensor 1')
bridge.add_accessory(temp_sensor)
bridge.add_accessory(temp_sensor2)
return bridge
def get_accessory(driver):
"""Call this method to get a standalone Accessory."""
return TemperatureSensor(driver, 'MyTempSensor')
# Start the accessory on port 51826
driver = AccessoryDriver(port=51826)
# Change `get_accessory` to `get_bridge` if you want to run a Bridge.
driver.add_accessory(accessory=get_accessory(driver))
# We want SIGTERM (terminate) to be handled by the driver itself,
# so that it can gracefully stop the accessory, server and advertising.
signal.signal(signal.SIGTERM, driver.signal_handler)
# Start it!
driver.start()