1
|
|
|
from colorsys import hsv_to_rgb |
2
|
|
|
|
3
|
|
|
from pyhap.accessory import Accessory |
4
|
|
|
from pyhap.const import CATEGORY_LIGHTBULB |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class MasterDimmer(Accessory): |
8
|
|
|
category = CATEGORY_LIGHTBULB |
9
|
|
|
|
10
|
|
|
def __init__(self, controller, driver): |
11
|
|
|
super().__init__(driver, "Global Dimmer") |
12
|
|
|
|
13
|
|
|
serv_light = self.add_preload_service( |
14
|
|
|
'Lightbulb', chars=['On', 'Brightness']) |
15
|
|
|
|
16
|
|
|
# Set our instance variables |
17
|
|
|
self.controller = controller |
18
|
|
|
self.brightness = 50 # Brightness value 0 - 100 Homekit API |
19
|
|
|
|
20
|
|
|
# Configure our callbacks |
21
|
|
|
self.char_state = serv_light.configure_char( |
22
|
|
|
'On', setter_callback=self.set_state, value=1) |
23
|
|
|
self.char_brightness = serv_light.configure_char( |
24
|
|
|
'Brightness', setter_callback=self.set_brightness, value=self.brightness) |
25
|
|
|
|
26
|
|
|
# Set model info |
27
|
|
|
self.set_info_service(manufacturer="PyDMXControl", |
28
|
|
|
model="Global Dimmer", |
29
|
|
|
serial_number="Chans: 1->{} (All)".format(controller.next_channel - 1)) |
30
|
|
|
|
31
|
|
|
def set_state(self, value): |
32
|
|
|
if value == 1: # On |
33
|
|
|
self.set_brightness(100) |
34
|
|
|
else: # Off |
35
|
|
|
self.set_brightness(0) |
36
|
|
|
|
37
|
|
|
def set_brightness(self, value): |
38
|
|
|
self.brightness = value |
39
|
|
|
self.controller.all_dim(self.brightness * 255 / 100) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
class MasterColor(Accessory): |
43
|
|
|
category = CATEGORY_LIGHTBULB |
44
|
|
|
|
45
|
|
|
def __init__(self, controller, driver): |
46
|
|
|
super().__init__(driver, "Global Color") |
47
|
|
|
|
48
|
|
|
serv_light = self.add_preload_service( |
49
|
|
|
'Lightbulb', chars=['On', 'Hue', 'Saturation']) |
50
|
|
|
|
51
|
|
|
# Set our instance variables |
52
|
|
|
self.controller = controller |
53
|
|
|
self.state = 1 |
54
|
|
|
self.hue = 0 # Hue Value 0 - 360 Homekit API |
55
|
|
|
self.saturation = 0 # Saturation Values 0 - 100 Homekit API |
56
|
|
|
|
57
|
|
|
# Configure our callbacks |
58
|
|
|
self.char_state = serv_light.configure_char( |
59
|
|
|
'On', getter_callback=self.get_state, value=self.state) |
60
|
|
|
self.char_hue = serv_light.configure_char( |
61
|
|
|
'Hue', setter_callback=self.set_hue, getter_callback=self.get_hue) |
62
|
|
|
self.char_saturation = serv_light.configure_char( |
63
|
|
|
'Saturation', setter_callback=self.set_saturation, getter_callback=self.get_saturation) |
64
|
|
|
|
65
|
|
|
# Set model info |
66
|
|
|
self.set_info_service(manufacturer="PyDMXControl", |
67
|
|
|
model="Global Color", |
68
|
|
|
serial_number="Chans: 1->{} (All)".format(controller.next_channel - 1)) |
69
|
|
|
|
70
|
|
|
def get_state(self): |
71
|
|
|
return self.state |
72
|
|
|
|
73
|
|
|
def set_color(self): |
74
|
|
|
h = self.hue / 360 |
75
|
|
|
s = self.saturation / 100 |
76
|
|
|
r, g, b = hsv_to_rgb(h, s, 1) |
77
|
|
|
self.controller.all_color([r * 255, g * 255, b * 255]) |
78
|
|
|
|
79
|
|
|
def get_hue(self): |
80
|
|
|
return self.hue |
81
|
|
|
|
82
|
|
|
def set_hue(self, value): |
83
|
|
|
self.hue = value |
84
|
|
|
self.set_color() |
85
|
|
|
|
86
|
|
|
def get_saturation(self): |
87
|
|
|
return self.saturation |
88
|
|
|
|
89
|
|
|
def set_saturation(self, value): |
90
|
|
|
self.saturation = value |
91
|
|
|
self.set_color() |
92
|
|
|
|