forked from software-2/ha-aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaurora.py
More file actions
152 lines (115 loc) · 4.8 KB
/
aurora.py
File metadata and controls
152 lines (115 loc) · 4.8 KB
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
from nanoleaf import Aurora
import logging
import voluptuous as vol
# For instructions or bug reports, please visit
# https://github.com/software-2/ha-aurora
from homeassistant.components.light import (
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, ATTR_COLOR_TEMP, SUPPORT_COLOR_TEMP, ATTR_RGB_COLOR, SUPPORT_RGB_COLOR, Light, PLATFORM_SCHEMA, ATTR_EFFECT, ATTR_EFFECT_LIST, SUPPORT_EFFECT )
from homeassistant.const import CONF_HOST, CONF_API_KEY, CONF_NAME
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['nanoleaf==0.4.0']
SUPPORT_AURORA = ( SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP )
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_NAME, default='Aurora'): cv.string,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
host = config.get(CONF_HOST)
apikey = config.get(CONF_API_KEY)
name = config.get(CONF_NAME)
my_aurora = Aurora(host, apikey)
my_aurora.hass_name = name
if my_aurora.on is None:
_LOGGER.error("Could not connect to Nanoleaf Aurora: " + name)
add_devices([AuroraLight(my_aurora)])
# TODO: https://github.com/home-assistant/home-assistant/pull/7596
# Remove ATTR_BRIGHTNESS and replace with ATTR_BRIGHTNESS_PCT
# And switch to kelvin
def brightness_scale_nanoleaf_to_hass(range_value):
# Hass uses 0-255, Aurora uses 0-100
return range_value * 2.55
def brightness_scale_hass_to_nanoleaf(range_value):
return int(range_value / 2.55)
def color_temp_scale_nanoleaf_to_hass(range_value):
# Hass uses 154-500, Aurora uses 1200-6500
return ((range_value - 1200) / 5300) * 346 + 154
def color_temp_scale_hass_to_nanoleaf(range_value):
return int(((range_value - 154) / 346) * 5300 + 1200)
class AuroraLight(Light):
"""Representation of a Nanoleaf Aurora inside Home Assistant."""
def __init__(self, light):
"""Initialize an Aurora."""
self._light = light
self._name = light.hass_name
self._state = light.on
self._brightness = brightness_scale_nanoleaf_to_hass(light.brightness)
self._effect = light.effect
self._effects_list = light.effects_list
self._color_temp = color_temp_scale_nanoleaf_to_hass(light.color_temperature)
self._rgb_color = light.rgb
@property
def name(self):
"""Return the display name of this light."""
return self._name
@property
def brightness(self):
"""Return the brightness of the light."""
return self._brightness
@property
def effect_list(self):
"""Return the list of supported effects."""
return self._effects_list
@property
def effect(self):
"""Return the current effect."""
return self._effect
@property
def is_on(self):
"""Return true if light is on."""
return self._state
@property
def color_temp(self):
"""Return the current color temperature"""
return self._color_temp
@property
def rgb_color(self):
"""Return the color in RGB"""
return self._rgb_color
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_AURORA
def turn_on(self, **kwargs):
"""Instruct the light to turn on."""
self._light.on = True
if ATTR_BRIGHTNESS in kwargs:
new_brightness = brightness_scale_hass_to_nanoleaf(kwargs.get(ATTR_BRIGHTNESS, 255))
print("BRIGHTNESS: " + str(new_brightness))
self._light.brightness = new_brightness
if ATTR_EFFECT in kwargs:
new_effect = kwargs[ATTR_EFFECT]
print("EFFECT: " + str(new_effect))
self._light.effect = new_effect
if ATTR_COLOR_TEMP in kwargs:
new_color_temp = color_temp_scale_hass_to_nanoleaf(kwargs.get(ATTR_COLOR_TEMP, 100))
print("COLOR TEMP: " + str(new_color_temp))
self._light.color_temperature = new_color_temp
if ATTR_RGB_COLOR in kwargs:
new_rgb_color = kwargs[ATTR_RGB_COLOR]
print("COLOR RGB: " + str(new_rgb_color))
self._light.rgb = new_rgb_color
def turn_off(self, **kwargs):
"""Instruct the light to turn off."""
self._light.on = False
def update(self):
"""Fetch new state data for this light.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = self._light.on
self._brightness = brightness_scale_nanoleaf_to_hass(self._light.brightness)
self._effect = self._light.effect
self._effects_list = self._light.effects_list
self._color_temp = color_temp_scale_nanoleaf_to_hass(self._light.color_temperature)
self._rgb_color = self._rgb_color