Passed
Pull Request — master (#25)
by Matt
09:18 queued 07:55
created

MasterLight.get_saturation()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 MasterLight(Accessory):
8
    category = CATEGORY_LIGHTBULB
9
10 View Code Duplication
    def __init__(self, controller, driver):
11
        super().__init__(driver, "> Master")
12
13
        serv_light = self.add_preload_service(
14
            'Lightbulb', chars=['On', 'Hue', 'Saturation', 'Brightness'])
15
16
        # Set our instance variables
17
        self.controller = controller
18
        self.hue = self.get_hue()  # Hue Value 0 - 360 Homekit API
19
        self.saturation = self.get_saturation()  # Saturation Values 0 - 100 Homekit API
20
        self.brightness = self.get_brightness()  # Brightness value 0 - 100 Homekit API
21
22
        # Configure our callbacks
23
        self.char_state = serv_light.configure_char(
24
            'On', getter_callback=self.get_state)
25
        self.char_hue = serv_light.configure_char(
26
            'Hue', setter_callback=self.set_hue, getter_callback=self.get_hue)
27
        self.char_saturation = serv_light.configure_char(
28
            'Saturation', setter_callback=self.set_saturation, getter_callback=self.get_saturation)
29
        self.char_brightness = serv_light.configure_char(
30
            'Brightness', setter_callback=self.set_brightness, getter_callback=self.get_brightness)
31
32
        # Set model info
33
        self.set_info_service(manufacturer="PyDMXControl",
34
                              model="Global Master",
35
                              serial_number="Chans: 1->{} (All)".format(controller.next_channel-1))
36
37
    @staticmethod
38
    def get_state():
39
        return 1
40
41
    def set_color(self):
42
        h = self.hue / 360
43
        s = self.saturation / 100
44
        r, g, b = hsv_to_rgb(h, s, 1)
45
        self.controller.all_color([r * 255, g * 255, b * 255])
46
47
    def set_brightness(self, value):
48
        self.brightness = value
49
        self.controller.all_dim(self.brightness * 255 / 100)
50
51
    @staticmethod
52
    def get_brightness():
53
        return 50
54
55
    def set_hue(self, value):
56
        self.hue = value
57
        self.set_color()
58
59
    @staticmethod
60
    def get_hue():
61
        return 0
62
63
    def set_saturation(self, value):
64
        self.saturation = value
65
        self.set_color()
66
67
    @staticmethod
68
    def get_saturation():
69
        return 100
70