-
Notifications
You must be signed in to change notification settings - Fork 5
/
light.py
197 lines (166 loc) · 5.82 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
"""Prismatik light."""
from typing import Any, Callable, Dict, List, Optional, Set
import homeassistant.helpers.config_validation as cv
import homeassistant.util.color as color_util
import voluptuous as vol
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_EFFECT,
ATTR_EFFECT_LIST,
ATTR_HS_COLOR,
COLOR_MODE_HS,
ColorMode,
LightEntity,
LightEntityFeature,
PLATFORM_SCHEMA,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_STATE,
CONF_API_KEY,
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_PROFILE_NAME,
)
from homeassistant.core import HomeAssistant
from .const import (
DEFAULT_ICON_OFF,
DEFAULT_ICON_ON,
DEFAULT_NAME,
DEFAULT_PORT,
DEFAULT_PROFILE_NAME,
DOMAIN
)
from .prismatik import PrismatikClient
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_API_KEY): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PROFILE_NAME, default=DEFAULT_PROFILE_NAME): cv.string,
}
)
async def async_setup_platform(
hass: HomeAssistant,
config: Dict,
async_add_entities: Callable[[List[LightEntity], bool], None],
discovery_info: Optional[Any] = None,
) -> None:
"""Set up the Prismatik Light platform."""
# pylint: disable=unused-argument
client = PrismatikClient(
config[CONF_HOST],
config[CONF_PORT],
config.get(CONF_API_KEY)
)
light = PrismatikLight(hass, config[CONF_NAME], client, config.get(CONF_PROFILE_NAME))
await light.async_update()
async_add_entities([light])
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: Callable[[List[LightEntity], bool], None],
) -> None:
"""Set up the Prismatik Light from integration."""
config = hass.data[DOMAIN][config_entry.entry_id]
if config_entry.options:
config.update(config_entry.options)
client = PrismatikClient(
config[CONF_HOST],
config[CONF_PORT],
config.get(CONF_API_KEY)
)
light = PrismatikLight(hass, config[CONF_NAME], client, config.get(CONF_PROFILE_NAME))
await light.async_update()
async_add_entities([light])
class PrismatikLight(LightEntity):
"""Representation of Prismatik."""
def __init__(
self,
hass: HomeAssistant,
name: str,
client: PrismatikClient,
profile: Optional[str]
) -> None:
"""Intialize."""
self._hass = hass
self._name = name
self._client = client
self._profile = profile
host = self._client.host.replace(".", "_")
self._unique_id = f"{host}_{self._client.port}"
self._attr_color_mode = ColorMode.HS
self._attr_supported_color_modes = set({ColorMode.HS, ColorMode.BRIGHTNESS})
self._attr_supported_features = LightEntityFeature.EFFECT
self._state = {
ATTR_STATE : False,
ATTR_EFFECT : None,
ATTR_EFFECT_LIST : None,
ATTR_BRIGHTNESS : None,
ATTR_HS_COLOR : None,
}
async def async_will_remove_from_hass(self) -> None:
"""Disconnect from update signal."""
await self._client.disconnect()
@property
def hs_color(self) -> Optional[List]:
"""Return the hue and saturation color value [float, float]."""
return self._state[ATTR_HS_COLOR]
@property
def name(self) -> str:
"""Return the name of the light."""
return self._name
@property
def available(self) -> bool:
"""Return availability of the light."""
return self._client.is_connected
@property
def is_on(self) -> bool:
"""Return light status."""
return self._state[ATTR_STATE]
@property
def icon(self) -> str:
"""Light icon."""
return DEFAULT_ICON_ON if self.available else DEFAULT_ICON_OFF
@property
def unique_id(self) -> str:
"""Unique ID."""
return self._unique_id
@property
def brightness(self) -> Optional[int]:
"""Return the brightness of this light between 0..255."""
return self._state[ATTR_BRIGHTNESS]
@property
def effect_list(self) -> Optional[List]:
"""Return profile list."""
return self._state[ATTR_EFFECT_LIST]
@property
def effect(self) -> Optional[str]:
"""Return current profile."""
return self._state[ATTR_EFFECT]
async def async_update(self) -> None:
"""Update light state."""
self._state[ATTR_STATE] = await self._client.is_on()
self._state[ATTR_EFFECT] = await self._client.get_profile()
self._state[ATTR_EFFECT_LIST] = await self._client.get_profiles()
brightness = await self._client.get_brightness()
self._state[ATTR_BRIGHTNESS] = round(brightness * 2.55) if brightness else None
rgb = await self._client.get_color()
self._state[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb) if rgb else None
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
await self._client.turn_on()
if ATTR_EFFECT in kwargs:
await self._client.set_profile(kwargs[ATTR_EFFECT])
elif ATTR_BRIGHTNESS in kwargs:
await self._client.set_brightness(round(kwargs[ATTR_BRIGHTNESS] / 2.55), self._profile)
elif ATTR_HS_COLOR in kwargs:
rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
await self._client.set_color(rgb, self._profile)
await self._client.unlock()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the light off."""
# pylint: disable=unused-argument
await self._client.turn_off()