forked from sysofwan/ha-triones
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlight.py
128 lines (103 loc) · 4.23 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
import logging
import voluptuous as vol
from typing import Any, Optional, Tuple
from .beurer import BeurerInstance
from .const import DOMAIN
from homeassistant.const import CONF_MAC
import homeassistant.helpers.config_validation as cv
from homeassistant.components.light import (COLOR_MODE_RGB, PLATFORM_SCHEMA,
LightEntity, ATTR_RGB_COLOR, ATTR_BRIGHTNESS, ATTR_EFFECT, COLOR_MODE_WHITE, ATTR_WHITE, LightEntityFeature)
from homeassistant.util.color import (match_max_scale)
from homeassistant.helpers import device_registry
from .const import LOGGER
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_MAC): cv.string
})
async def async_setup_entry(hass, config_entry, async_add_devices):
LOGGER.debug(f"Setting up device from light")
instance = hass.data[DOMAIN][config_entry.entry_id]
async_add_devices([BeurerLight(instance, config_entry.data["name"], config_entry.entry_id)])
class BeurerLight(LightEntity):
def __init__(self, beurerInstance: BeurerInstance, name: str, entry_id: str) -> None:
self._instance = beurerInstance
self._entry_id = entry_id
self._attr_supported_color_modes = {COLOR_MODE_RGB, COLOR_MODE_WHITE}
self._color_mode = None
self._attr_name = name
self._attr_unique_id = self._instance.mac
async def async_added_to_hass(self) -> None:
"""Add update callback after being added to hass."""
self._instance.set_update_callback(self.update_callback)
await self._instance.update()
def update_callback(self) -> None:
"""Schedule a state update."""
#self.async_schedule_update_ha_state(False)
self.schedule_update_ha_state(False)
@property
def available(self):
return self._instance.is_on != None
#We handle update triggers manually, do not poll
@property
def should_poll(self) -> Optional[bool]:
return False
@property
def brightness(self):
if self._instance.color_mode == COLOR_MODE_WHITE:
return self._instance.white_brightness
else:
return self._instance.color_brightness
return None
@property
def is_on(self) -> Optional[bool]:
return self._instance.is_on
@property
# RGB color/brightness based on https://github.com/home-assistant/core/issues/51175
def rgb_color(self):
if self._instance.rgb_color:
return match_max_scale((255,), self._instance.rgb_color)
return None
@property
def effect(self):
if self._instance.color_mode == COLOR_MODE_WHITE:
return "Off"
else:
return self._instance.effect
@property
def effect_list(self):
return self._instance.supported_effects
@property
def supported_features(self):
return LightEntityFeature.EFFECT
@property
def color_mode(self):
return self._instance.color_mode
@property
def device_info(self):
return {
"identifiers": {
(DOMAIN, self._instance.mac)
},
"name": self.name,
"connections": {(device_registry.CONNECTION_NETWORK_MAC, self._instance.mac)}
}
def _transform_color_brightness(self, color: Tuple[int, int, int], set_brightness: int):
rgb = match_max_scale((255,), color)
res = tuple(color * set_brightness // 255 for color in rgb)
return res
async def async_turn_on(self, **kwargs: Any) -> None:
LOGGER.debug(f"Turning light on with args: {kwargs}")
#if not self.is_on:
# await self._instance.turn_on()
if len(kwargs) == 0:
await self._instance.turn_on()
if ATTR_BRIGHTNESS in kwargs and kwargs[ATTR_BRIGHTNESS]:
await self._instance.set_white(kwargs[ATTR_BRIGHTNESS])
if ATTR_RGB_COLOR in kwargs and kwargs[ATTR_RGB_COLOR]:
color = kwargs[ATTR_RGB_COLOR]
await self._instance.set_color(color)
if ATTR_EFFECT in kwargs and kwargs[ATTR_EFFECT]:
await self._instance.set_effect(kwargs[ATTR_EFFECT])
async def async_turn_off(self, **kwargs: Any) -> None:
await self._instance.turn_off()
async def async_update(self) -> None:
await self._instance.update()