forked from chiragd/lightwaverf-home-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightwave.py
140 lines (105 loc) · 4.23 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
134
135
136
137
138
139
140
"""
homeassistant.components.light.lightwave
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Implements LightwaveRF lights.
My understanding of the LightWave Hub is that devices cannot be discovered so must be
registered manually. This is done in the configuration file:
light:
- platform: lightwave
devices:
R1D2:
name: Room one Device two
R2D1:
name: Room two Device one
Each device requires an id and a name. The id takes the form R#D# where R# is the room number
and D# is the device number.
If devices are missing the default is to generate 15 rooms with 8 lights. From this you will
be able to determine the room and device number for each light.
TODO:
Add a registration button. Until then the following command needs to be sent to the LightwaveRF hub:
echo -ne "100,\!F*p." | nc -u -w1 LW_HUB_IP_ADDRESS 9760
When this is sent you have 12 seconds to acknowledge the message on the hub.
For more details on the api see: https://api.lightwaverf.com/
"""
import socket
import voluptuous as vol
from homeassistant.const import CONF_DEVICES, CONF_NAME
from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, PLATFORM_SCHEMA)
import homeassistant.helpers.config_validation as cv
DEVICE_SCHEMA = vol.Schema({
vol.Required(CONF_NAME): cv.string
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DEVICES, default={}): {cv.string: DEVICE_SCHEMA}
})
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return LightWave lights """
lights = []
for device_id, device_config in config.get(CONF_DEVICES, {}).items():
name = device_config[CONF_NAME]
lights.append(LRFLight(name, device_id))
if len(lights) == 0:
# Config is empty so generate a default set of switches
for room in range(1, 15):
for device in range(1, 8):
name = "Room " + str(room) + " Device " + str(device)
device_id = "R" + str(room) + "D" + str(device)
lights.append(LRFLight(name, device_id))
add_devices_callback(lights)
def send_command(msg):
""" send message to LightwaveRF hub."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(msg.encode('UTF-8'), ('255.255.255.255', 9760))
sock.close
def calculate_brightness(brightness):
"""Scale brightness from 0..255 to 0..32"""
return round((brightness * 32) / 255)
class LRFLight(Light):
""" Provides a LightWave light. """
def __init__(self, name, device_id):
self._name = name
self._device_id = device_id
self._state = None
self._brightness = 255
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
@property
def should_poll(self):
""" No polling needed for a LightWave light. """
return False
@property
def name(self):
""" Returns the name of the LightWave light. """
return self._name
@property
def brightness(self):
""" Brightness of this light between 0..255. """
return self._brightness
@property
def is_on(self):
""" True if the LightWave light is on. """
return self._state
def turn_on(self, **kwargs):
""" Turn the LightWave light on. """
self._state = True
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
brightness_value = calculate_brightness(self._brightness)
msg = '321, !%sFdP%d|Lights %d|%s ' % (
self._device_id, brightness_value, brightness_value, self._name)
send_command(msg)
# F1 = Light on and F0 = light off. FdP[0..32] is brightness. 32 is
# full. We want that when turning the light on.
msg = '321, !%sFdP32|Turn On|%s ' % (self._device_id, self._name)
send_command(msg)
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
""" Turn the LightWave light off. """
self._state = False
msg = "321, !%sF0|Turn Off|%s " % (self._device_id, self._name)
send_command(msg)
self.schedule_update_ha_state()