/
light.py
executable file
·203 lines (166 loc) · 6.38 KB
/
light.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Shelly platform for the light component.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/shelly/
"""
import logging
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_HS_COLOR,
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT,
Light)
import homeassistant.util.color as color_util
from homeassistant.util.color import (
color_temperature_kelvin_to_mired as kelvin_to_mired,
color_temperature_mired_to_kelvin as mired_to_kelvin)
from . import ShellyDevice, get_device_from_hass
_LOGGER = logging.getLogger(__name__)
EFFECT_WHITE = {'name': "White", 'mode': 'white', 'effect': 0}
EFFECT_COLOR = {'name': "Color", 'mode': 'color', 'effect': 0}
EFFECT_METEOR = {'name': "Meteor Shower", 'effect': 1}
# EFFECT_GRADUAL_C = { 'name':"Gradual Change (Color)" , 'mode':'color', 'effect':2 }
# EFFECT_GRADUAL_W = { 'name':"Gradual Change (White)" , 'mode':'white', 'effect':2 }
EFFECT_BREATH_C = {'name': "Breath (Color)", 'mode': 'color', 'effect': 3}
EFFECT_BREATH_W = {'name': "Breath (White)", 'mode': 'white', 'effect': 3}
EFFECT_FLASH_C = {'name': "Flash (Color)", 'mode': 'color', 'effect': 4}
EFFECT_FLASH_W = {'name': "Flash (White)", 'mode': 'white', 'effect': 4}
EFFECT_ONOFF_C = {'name': "On/Off gradual (Color)", 'mode': 'color',
'effect': 5}
EFFECT_ONOFF_W = {'name': "On/Off gradual (White)", 'mode': 'white',
'effect': 5}
EFFECT_REDGREEN = {'name': "Red/Green Change", 'effect': 6}
EFFECT_LIST = [
EFFECT_WHITE,
EFFECT_COLOR,
EFFECT_METEOR,
# EFFECT_GRADUAL_C,
# EFFECT_GRADUAL_W,
EFFECT_BREATH_C,
EFFECT_BREATH_W,
EFFECT_FLASH_C,
EFFECT_FLASH_W,
EFFECT_ONOFF_C,
EFFECT_ONOFF_W,
EFFECT_REDGREEN
]
SUPPORT_SHELLYRGB_COLOR = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR)
SUPPORT_SHELLYRGB_WHITE = (SUPPORT_BRIGHTNESS)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup Shelly Light platform."""
dev = get_device_from_hass(hass, discovery_info)
if dev.devType == "RELAY":
add_devices([ShellyLight(dev, hass)])
else:
add_devices([ShellyRGB(dev, hass)])
class ShellyLight(ShellyDevice, Light):
"""Representation of an Shelly Switch."""
def __init__(self, dev, hass):
"""Initialize an ShellySwitch."""
ShellyDevice.__init__(self, dev, hass)
self._state = None
self.update()
def _updated(self):
"""Receive events when the switch state changed (by mobile,
switch etc)"""
if self.entity_id is not None:
state = self._hass.states.get(self.entity_id)
if state is not None:
self._hass.states.set(self.entity_id,
"on" if self._dev.state else "off",
state.attributes)
@property
def is_on(self):
return self._state
def turn_on(self, **kwargs):
self._dev.turnOn()
def turn_off(self, **kwargs):
self._dev.turnOff()
def update(self):
"""Fetch new state data for this light."""
self._state = self._dev.state
class ShellyRGB(ShellyDevice, Light):
"""Representation of an Shelly Light."""
def __init__(self, dev, hass):
"""Initialize an ShellyLight."""
ShellyDevice.__init__(self, dev, hass)
self._state = None
self._brightness = None
self._rgb = None
self._mode = None
self._temp = None
self.update()
def _updated(self):
"""Receive events when the light state changed (by mobile,
switch etc)"""
if self.entity_id is not None:
self.schedule_update_ha_state(True)
@property
def supported_features(self):
"""Flag supported features."""
features = SUPPORT_SHELLYRGB_WHITE \
if self._mode == 'white' else SUPPORT_SHELLYRGB_COLOR
if self._dev.supportColorTemp and self._mode == "white":
features = features | SUPPORT_COLOR_TEMP
if self._dev.supportEffects:
features = features | SUPPORT_EFFECT
return features
@property
def effect_list(self):
"""Return the list of supported effects."""
return list(map(lambda e: e['name'], EFFECT_LIST))
@property
def brightness(self):
"""Return the brightness of the light."""
return int(self._brightness * 2.55)
@property
def is_on(self):
return self._state
def turn_on(self, **kwargs):
brightness = None
rgb = None
temp = None
effect_nr = None
mode = None
if ATTR_BRIGHTNESS in kwargs:
brightness = int(kwargs[ATTR_BRIGHTNESS] / 2.55)
if ATTR_HS_COLOR in kwargs:
red, green, blue = \
color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
rgb = [red, green, blue]
if ATTR_COLOR_TEMP in kwargs:
temp = int(mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]))
if ATTR_EFFECT in kwargs:
affect_attr = kwargs.get(ATTR_EFFECT)
effect = [e for e in EFFECT_LIST if e['name'] == affect_attr][0]
if 'mode' in effect:
mode = effect['mode']
if 'effect' in effect:
effect_nr = effect['effect']
self._dev.turnOn(brightness=brightness, rgb=rgb, temp=temp, mode=mode,
effect=effect_nr)
def turn_off(self, **kwargs):
self._dev.turnOff()
def update(self):
"""Fetch new state data for this light."""
self._state = self._dev.state
self._rgb = self._dev.rgb
self._brightness = self._dev.brightness
self._temp = self._dev.temp
self._mode = self._dev.mode
@property
def hs_color(self):
"""Return the hue and saturation color value [float, float]."""
return color_util.color_RGB_to_hs(*self._rgb)
@property
def color_temp(self):
"""Return the CT color value in mireds."""
if self._temp is None:
return None
return int(kelvin_to_mired(self._temp))
@property
def min_mireds(self):
"""Return the coldest color_temp that this light supports."""
return kelvin_to_mired(6500)
@property
def max_mireds(self):
"""Return the warmest color_temp that this light supports."""
return kelvin_to_mired(3000)