Skip to content

Commit

Permalink
Merge pull request #41 from ynsgnr/set-actual-brightness
Browse files Browse the repository at this point in the history
implemented setting actual brightness
  • Loading branch information
dave-code-ruiz authored Aug 26, 2023
2 parents adcfd30 + da1d6bb commit ddba589
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
9 changes: 7 additions & 2 deletions custom_components/elkbledom/elkbledom.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ def rgb_color(self):
return self._rgb_color

@property
def white_brightness(self):
def brightness(self):
return self._brightness

@property
def color_temp(self):
return self._color_temp
Expand All @@ -280,6 +280,11 @@ def effect(self):
async def set_white(self, intensity: int):
await self._write([0x7e, 0x00, 0x01, int(intensity*100/255), 0x00, 0x00, 0x00, 0x00, 0xef])
self._brightness = intensity

@retry_bluetooth_connection_error
async def set_brightness(self, intensity: int):
await self._write([0x7e, 0x04, 0x01, int(intensity*100/255), 0xff, 0x00, 0xff, 0x00, 0xef])
self._brightness = intensity

@retry_bluetooth_connection_error
async def set_effect_speed(self, value: int):
Expand Down
24 changes: 4 additions & 20 deletions custom_components/elkbledom/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ def available(self):

@property
def brightness(self):
if self._instance.white_brightness:
return self._instance.white_brightness

if self._instance._rgb_color:
return max(self._instance.rgb_color)

return 250
return self._instance.brightness

@property
def is_on(self) -> Optional[bool]:
Expand Down Expand Up @@ -124,11 +118,6 @@ def should_poll(self):
"""No polling needed for a demo light."""
return False

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"Params turn on: {kwargs}")
if not self.is_on:
Expand All @@ -138,7 +127,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
await self._instance.set_color(self._transform_color_brightness((255, 255, 255), 250))

if ATTR_BRIGHTNESS in kwargs and kwargs[ATTR_BRIGHTNESS] != self.brightness and self.rgb_color != None:
await self._instance.set_color(self._transform_color_brightness(self.rgb_color, kwargs[ATTR_BRIGHTNESS]))
await self._instance.set_brightness(kwargs[ATTR_BRIGHTNESS])

if ATTR_COLOR_TEMP in kwargs:
self._color_mode = ColorMode.COLOR_TEMP
Expand All @@ -148,18 +137,13 @@ async def async_turn_on(self, **kwargs: Any) -> None:

if ATTR_WHITE in kwargs:
self._color_mode = ColorMode.WHITE
if kwargs[ATTR_WHITE] != self.brightness:
self._effect = None
await self._instance.set_white(kwargs[ATTR_WHITE])
self._effect = None
await self._instance.set_white(kwargs[ATTR_WHITE])

if ATTR_RGB_COLOR in kwargs:
self._color_mode = ColorMode.RGB
if kwargs[ATTR_RGB_COLOR] != self.rgb_color:
color = kwargs[ATTR_RGB_COLOR]
if ATTR_BRIGHTNESS in kwargs:
color = self._transform_color_brightness(color, kwargs[ATTR_BRIGHTNESS])
else:
color = self._transform_color_brightness(color, self.brightness)
self._effect = None
await self._instance.set_color(color)

Expand Down

0 comments on commit ddba589

Please sign in to comment.