-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlightwave.py
133 lines (106 loc) · 4.25 KB
/
lightwave.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
"""
homeassistant.components.light.lightwave
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo platform that implements lights.
"""
import random
import socket
from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS)
LIGHT_COLORS = [
[237, 224, 33],
[255, 63, 111],
]
LIGHT_TEMPS = [240, 380]
# Below are generic rooms, with sample lightwave device IDs e.g. R1D1.
# At the moment we have to manually configure your LightWave Devices. If you need help getting the codes for your devices, let me know.
# You could try my devices and see how they work. Basically R represents a room. D represents a device, as far as I can tell. Look at the readme file for more details.
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return lightwave lights """
add_devices_callback([
LRFLight("Guest Bedroom Light", False, 'R1D1'),
LRFLight("Office Ceiling Light", False, 'R2D1'),
LRFLight("Office Wall Light", False, 'R2D2'),
LRFLight("Living Room Wall Light", False, 'R3D1'),
LRFLight("Living Room Light", False, 'R3D2'),
LRFLight("Master Bedroom Light", False, 'R4D1')
])
class LRFLight(Light):
""" Provides a demo switch. """
# pylint: disable=too-many-arguments
def __init__(self, name, state, deviceid, brightness=255):
self._name = name
self._state = state
#self._rgb = rgb or random.choice(LIGHT_COLORS)
#self._ct = ct or random.choice(LIGHT_TEMPS)
self._brightness = brightness
self._deviceid = deviceid
#self._device_id = device_id
@property
def should_poll(self):
""" No polling needed for a demo light. """
return False
@property
def name(self):
""" Returns the name of the device if any. """
return self._name
@property
def brightness(self):
""" Brightness of this light between 0..255. """
return self._brightness
#@property
#def rgb_color(self):
# """ rgb color value. """
# return self._rgb
#@property
#def color_temp(self):
# """ CT color temperature. """
# return self._ct
@property
def deviceid(self):
""" The Lightwave Device ID """
return self._deviceid
@property
def is_on(self):
""" True if device is on. """
return self._state
def send_command(self, msg):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
#command = "666, !R2D1F1|Turn On|Babas Lights"
#register = "666, !F*p"
sock.sendto(msg.encode('UTF-8'), ('255.255.255.255', 9760))
sock.close
def calculate_brightness(self, brightness):
# the scale is 0 to 255 so we need to normalise to 0 to 100 first.
old_range = 255 # 255 - 0 = 255
new_range = 100 # 100 - 9 = 100
new_value = (((brightness - 0) * new_range) / old_range)
brightness32 = round(new_value * 0.32)
return brightness32
def turn_on(self, **kwargs):
""" Turn the device on. """
self._state = True
#if ATTR_RGB_COLOR in kwargs:
# self._rgb = kwargs[ATTR_RGB_COLOR]
#if ATTR_COLOR_TEMP in kwargs:
# self._ct = kwargs[ATTR_COLOR_TEMP]
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
brightness_value = self.calculate_brightness(self._brightness)
msg = '666, !%sFdP%d|Lights %d|%s ' % (self._deviceid, brightness_value, brightness_value, self._name)
self.send_command(msg)
print("LIGHTWAVE: Turning Lights On")
#F1 = Light on and F0 = light off. FdP[0..32] is brightness. 32 is full. We want that when turning the light on.
msg = '666, !%sFdP32|Turn On|%s ' % (self._deviceid, self._name)
self.send_command(msg)
print("LIGHTWAVE: finished")
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
""" Turn the device off. """
print("LIGHTWAVE: Turning Lights Off")
msg = "666, !%sF0|Turn Off|%s " % (self._deviceid, self._name)
self.send_command(msg)
print("LIGHTWAVE: finished")
self._state = False
self.schedule_update_ha_state()