|
1
|
|
|
from colorsys import hsv_to_rgb, rgb_to_hsv |
|
2
|
|
|
|
|
3
|
|
|
from pyhap.accessory import Accessory |
|
4
|
|
|
from pyhap.const import CATEGORY_LIGHTBULB |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class RGBLight(Accessory): |
|
8
|
|
|
category = CATEGORY_LIGHTBULB |
|
9
|
|
|
|
|
10
|
|
View Code Duplication |
def __init__(self, fixture, driver): |
|
11
|
|
|
super().__init__(driver, fixture.name) |
|
12
|
|
|
|
|
13
|
|
|
serv_light = self.add_preload_service( |
|
14
|
|
|
'Lightbulb', chars=['On', 'Hue', 'Saturation', 'Brightness']) |
|
15
|
|
|
|
|
16
|
|
|
# Set our instance variables |
|
17
|
|
|
self.fixture = fixture |
|
18
|
|
|
self.accessory_state = self.get_state() # State of On/Off |
|
19
|
|
|
self.hue = self.get_hue() # Hue Value 0 - 360 Homekit API |
|
20
|
|
|
self.saturation = self.get_saturation() # Saturation Values 0 - 100 Homekit API |
|
21
|
|
|
self.brightness = self.get_brightness() # Brightness value 0 - 100 Homekit API |
|
22
|
|
|
|
|
23
|
|
|
# Configure our callbacks |
|
24
|
|
|
self.char_on = serv_light.configure_char( |
|
25
|
|
|
'On', setter_callback=self.set_state, getter_callback=self.get_state) |
|
26
|
|
|
self.char_hue = serv_light.configure_char( |
|
27
|
|
|
'Hue', setter_callback=self.set_hue, getter_callback=self.get_hue) |
|
28
|
|
|
self.char_saturation = serv_light.configure_char( |
|
29
|
|
|
'Saturation', setter_callback=self.set_saturation, getter_callback=self.get_saturation) |
|
30
|
|
|
self.char_brightness = serv_light.configure_char( |
|
31
|
|
|
'Brightness', setter_callback=self.set_brightness, getter_callback=self.get_brightness) |
|
32
|
|
|
|
|
33
|
|
|
# Set model info |
|
34
|
|
|
self.set_info_service(manufacturer=fixture.manufacturer, |
|
35
|
|
|
model=fixture.model, |
|
36
|
|
|
serial_number="Chans: {}".format(fixture.channel_usage)) |
|
37
|
|
|
|
|
38
|
|
|
def set_state(self, value): |
|
39
|
|
|
self.accessory_state = value |
|
40
|
|
|
if value == 1: # On |
|
41
|
|
|
self.set_brightness(100) |
|
42
|
|
|
else: # Off |
|
43
|
|
|
self.set_brightness(0) |
|
44
|
|
|
|
|
45
|
|
|
def get_state(self): |
|
46
|
|
|
self.accessory_state = 1 if self.get_brightness() > 0 else 0 |
|
47
|
|
|
return self.accessory_state |
|
48
|
|
|
|
|
49
|
|
|
def set_color(self): |
|
50
|
|
|
h = self.hue / 360 |
|
51
|
|
|
s = self.saturation / 100 |
|
52
|
|
|
r, g, b = hsv_to_rgb(h, s, 1) |
|
53
|
|
|
self.fixture.color([r * 255, g * 255, b * 255]) |
|
54
|
|
|
|
|
55
|
|
|
def get_color(self): |
|
56
|
|
|
return self.fixture.get_color()[:3] |
|
57
|
|
|
|
|
58
|
|
|
def set_brightness(self, value): |
|
59
|
|
|
self.accessory_state = 1 if value > 0 else 0 |
|
60
|
|
|
self.brightness = value |
|
61
|
|
|
self.fixture.dim(self.brightness * 255 / 100) |
|
62
|
|
|
|
|
63
|
|
|
def get_brightness(self): |
|
64
|
|
|
return self.fixture.get_channel_value(self.fixture.get_channel_id("dimmer"))[0] * 100 / 255 |
|
65
|
|
|
|
|
66
|
|
|
def set_hue(self, value): |
|
67
|
|
|
self.hue = value |
|
68
|
|
|
self.set_color() |
|
69
|
|
|
|
|
70
|
|
|
def get_hue(self): |
|
71
|
|
|
h, s, v = rgb_to_hsv(*self.get_color()) |
|
72
|
|
|
return h * 360 |
|
73
|
|
|
|
|
74
|
|
|
def set_saturation(self, value): |
|
75
|
|
|
self.saturation = value |
|
76
|
|
|
self.set_color() |
|
77
|
|
|
|
|
78
|
|
|
def get_saturation(self): |
|
79
|
|
|
h, s, v = rgb_to_hsv(*self.get_color()) |
|
80
|
|
|
return s * 100 |
|
81
|
|
|
|