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

PyDMXControl.smart._Master   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 37.14 %

Importance

Changes 0
Metric Value
eloc 51
dl 26
loc 70
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A MasterLight.set_saturation() 0 3 1
A MasterLight.get_brightness() 0 3 1
A MasterLight.get_state() 0 3 1
A MasterLight.get_saturation() 0 3 1
A MasterLight.get_hue() 0 3 1
A MasterLight.set_hue() 0 3 1
A MasterLight.set_brightness() 0 3 1
A MasterLight.set_color() 0 5 1
A MasterLight.__init__() 26 26 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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