-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_hap.py
More file actions
executable file
·88 lines (68 loc) · 3.16 KB
/
_hap.py
File metadata and controls
executable file
·88 lines (68 loc) · 3.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
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
import logging
import signal
from threading import Thread
from pyhap.accessory_driver import AccessoryDriver
import pyhap.loader as loader
from pyhap.accessory import Accessory
from pyhap.const import CATEGORY_THERMOSTAT
from _utils import flags, get_temperature
from _ir_tx import transmit, convert_to_pulses
class Thermostat(Accessory):
category = CATEGORY_THERMOSTAT
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
serv_Thermostat = self.add_preload_service('Thermostat')
self.char_CurrentHeatingCoolingState = serv_Thermostat.configure_char(
'CurrentHeatingCoolingState', getter_callback=self.get_current_heatingcoolingstate)
#[0, 1, 2, 3] ([off, heat, cool, auto])
self.char_TargetTemperature = serv_Thermostat.configure_char(
'TargetTemperature', getter_callback=self.get_target_temperature, setter_callback=self.set_target_temperature)
new_properties = { 'minValue': flags.status.get("min_temperature", 15),
'maxValue': flags.status.get("max_temperature", 35),
'step': 1
}
self.char_TargetTemperature.override_properties(properties=new_properties)
self.char_TemperatureDisplayUnits = serv_Thermostat.configure_char(
'TemperatureDisplayUnits')
self.char_CurrentTemperature = serv_Thermostat.configure_char(
'CurrentTemperature')
self.char_TargetHeatingCoolingState = serv_Thermostat.configure_char(
'TargetHeatingCoolingState', getter_callback=self.get_target_heatingcoolingstate, setter_callback=self.set_target_heatingcoolingstate)
#[0, 1, 2, 3] ([off, heat, cool, auto])
@Accessory.run_at_interval(3)
def run(self):
self.char_CurrentTemperature.set_value(get_temperature())
def set_target_temperature(self, value):
flags.status['target_temperature'] = value
if flags.status['target_heatingcoolingstate'] != 0: # Not OFF
transmit(convert_to_pulses(create_command()))
def get_target_temperature(self):
return flags.status['target_temperature']
def set_target_heatingcoolingstate(self, value):
flags.status['target_heatingcoolingstate'] = value
transmit(convert_to_pulses(create_command()))
def get_target_heatingcoolingstate(self):
return flags.status['target_heatingcoolingstate']
def get_current_heatingcoolingstate(self):
return flags.status['current_heatingcoolingstate']
def start_hap():
driver = AccessoryDriver(port=51826)
driver.add_accessory(accessory=Thermostat(driver, flags.status.get("device_name", "HAP_NAME")))
signal.signal(signal.SIGTERM, driver.signal_handler)
t = Thread(target=driver.start)
t.stop_main = driver.stop
return t
def create_command():
temp = flags.status['target_temperature']
hc_state = flags.status['target_heatingcoolingstate']
states = {
"0": "OFF",
"1": "HEAT",
"2": "COOL",
"3": "AUTO"
}
state = states.get(hc_state, "OFF")
if state == "OFF":
return "AC_OFF"
state = "AUTO" # hardcoded as I won't use HEAT or COOL mode.
return "AC_ON_{}DEG_{}".format(temp, state)