forked from ikalchev/HAP-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
busy_home.py
104 lines (72 loc) · 3.22 KB
/
busy_home.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Starts a fake fan, lightbulb, garage door and a TemperatureSensor
"""
import logging
import signal
import random
from pyhap.accessory import Accessory, Bridge
from pyhap.accessory_driver import AccessoryDriver
from pyhap.const import (CATEGORY_FAN,
CATEGORY_LIGHTBULB,
CATEGORY_GARAGE_DOOR_OPENER,
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))
class FakeFan(Accessory):
"""Fake Fan, only logs whatever the client set."""
category = CATEGORY_FAN
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Add the fan service. Also add optional characteristics to it.
serv_fan = self.add_preload_service(
'Fan', chars=['RotationSpeed', 'RotationDirection'])
self.char_rotation_speed = serv_fan.configure_char(
'RotationSpeed', setter_callback=self.set_rotation_speed)
self.char_rotation_direction = serv_fan.configure_char(
'RotationDirection', setter_callback=self.set_rotation_direction)
def set_rotation_speed(self, value):
logging.debug("Rotation speed changed: %s", value)
def set_rotation_direction(self, value):
logging.debug("Rotation direction changed: %s", value)
class LightBulb(Accessory):
"""Fake lightbulb, logs what the client sets."""
category = CATEGORY_LIGHTBULB
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
serv_light = self.add_preload_service('Lightbulb')
self.char_on = serv_light.configure_char(
'On', setter_callback=self.set_bulb)
def set_bulb(self, value):
logging.info("Bulb value: %s", value)
class GarageDoor(Accessory):
"""Fake garage door."""
category = CATEGORY_GARAGE_DOOR_OPENER
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_preload_service('GarageDoorOpener')\
.configure_char(
'TargetDoorState', setter_callback=self.change_state)
def change_state(self, value):
logging.info("Bulb value: %s", value)
self.get_service('GarageDoorOpener')\
.get_characteristic('CurrentDoorState')\
.set_value(value)
def get_bridge(driver):
bridge = Bridge(driver, 'Bridge')
bridge.add_accessory(LightBulb(driver, 'Lightbulb'))
bridge.add_accessory(FakeFan(driver, 'Big Fan'))
bridge.add_accessory(GarageDoor(driver, 'Garage'))
bridge.add_accessory(TemperatureSensor(driver, 'Sensor'))
return bridge
driver = AccessoryDriver(port=51826, persist_file='busy_home.state')
driver.add_accessory(accessory=get_bridge(driver))
signal.signal(signal.SIGTERM, driver.signal_handler)
driver.start()