Passed
Push — smart_home ( 9fecf3 )
by Matt
02:40
created

PyDMXControl.smart.HomeKitLight.set_brightness()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import signal
2
from colorsys import hsv_to_rgb
3
4
from pyhap.accessory import Accessory, Bridge
5
from pyhap.accessory_driver import AccessoryDriver
6
from pyhap.const import CATEGORY_LIGHTBULB
7
8
9
class HomeKitLight(Accessory):
10
    category = CATEGORY_LIGHTBULB
11
12
    def __init__(self, fixture, *args, **kwargs):
13
14
        super().__init__(*args, **kwargs)
15
16
        serv_light = self.add_preload_service(
17
            'Lightbulb', chars=['On', 'Hue', 'Saturation', 'Brightness'])
18
19
        self.fixture = fixture
20
21
        # Configure our callbacks
22
        self.char_hue = serv_light.configure_char(
23
            'Hue', setter_callback=self.set_hue)
24
        self.char_saturation = serv_light.configure_char(
25
            'Saturation', setter_callback=self.set_saturation)
26
        self.char_on = serv_light.configure_char(
27
            'On', setter_callback=self.set_state)
28
        self.char_on = serv_light.configure_char(
29
            'Brightness', setter_callback=self.set_brightness)
30
31
        # Set our instance variables
32
        self.accessory_state = 0  # State of On/Off
33
        self.hue = 0  # Hue Value 0 - 360 Homekit API
34
        self.saturation = 100  # Saturation Values 0 - 100 Homekit API
35
        self.brightness = 100  # Brightness value 0 - 100 Homekit API
36
37
    def set_state(self, value):
38
        print("state", value)
39
        self.accessory_state = value
40
        if value == 1:  # On
41
            self.set_color()
42
            self.fixture.dim(self.brightness)
43
        else:  # Off
44
            self.fixture.dim(0)
45
46
    def set_color(self):
47
        h = self.hue / 360
48
        s = self.saturation / 100
49
        v = 1
50
        print("hsv", h, s, v)
51
        r, g, b = hsv_to_rgb(h, s, v)
52
        print("rgb", r, g, b)
53
        self.fixture.color([r * 255, g * 255, b * 255])
54
55
    def set_brightness(self, value):
56
        self.brightness = value
57
        print("bright", value)
58
        self.fixture.dim(self.brightness * 255 / 100)
59
60
    def set_hue(self, value):
61
        self.hue = value
62
        print("hue", value)
63
        self.set_color()
64
65
    def set_saturation(self, value):
66
        self.saturation = value
67
        print("sat", value)
68
        self.set_color()
69
70
71
def run(controller):
72
    start_port = 51826
73
    driver = AccessoryDriver(port=start_port)
74
    bridge = Bridge(driver, 'PyDMXControl')
75
76
    for i, fixture in enumerate(controller.get_all_fixtures()):
77
        print(fixture.name)
78
        bridge.add_accessory(HomeKitLight(fixture, driver, fixture.name))
79
80
    signal.signal(signal.SIGTERM, driver.signal_handler)
81
    driver.add_accessory(accessory=bridge)
82
83
    driver.start()
84
    # thread = Thread(target=driver.start)
85
    # thread.daemon = True
86