Skip to content

Commit

Permalink
init and login commands
Browse files Browse the repository at this point in the history
Creating effective differents between init and login commands
  • Loading branch information
dave-code-ruiz authored Aug 30, 2023
1 parent 82438de commit f1f90e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
2 changes: 2 additions & 0 deletions custom_components/elkbledom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ async def toggle_light(self):
try:
if not self._instance:
self._instance = BLEDOMInstance(self.mac, False, 120, self.hass)
#init commands
await self._instance._init_command()
await self._instance.update()
if self._instance.is_on:
await self._instance.turn_off()
Expand Down
51 changes: 30 additions & 21 deletions custom_components/elkbledom/elkbledom.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,16 @@ async def set_color_temp_kelvin(self, value: int, brightness: int):
await self._write([0x7e, 0x00, 0x05, 0x02, color_temp_percent, brightness_percent, 0x00, 0x00, 0xef])

@retry_bluetooth_connection_error
async def set_white(self):
await self._write([0x7e, 0x00, 0x01, 0x99, 0x00, 0x00, 0x00, 0x00, 0xef])
self._brightness = 255
async def set_color(self, rgb: Tuple[int, int, int]):
r, g, b = rgb
await self._write([0x7e, 0x00, 0x05, 0x03, r, g, b, 0x00, 0xef])
self._rgb_color = rgb

@DeprecationWarning
@retry_bluetooth_connection_error
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):
Expand Down Expand Up @@ -368,21 +375,6 @@ async def turn_off(self):
await self._write(self._turn_off_cmd)
self._is_on = False

@retry_bluetooth_connection_error
async def set_color(self, rgb: Tuple[int, int, int]):
r, g, b = rgb
await self._write([0x7e, 0x00, 0x05, 0x03, r, g, b, 0x00, 0xef])
self._rgb_color = rgb

@retry_bluetooth_connection_error
async def set_color_temp(self, value: int):
if value > 100:
value = 100
warm = value
cold = 100 - value
await self._write([0x7e, 0x00, 0x05, 0x02, warm, cold, 0x00, 0x00, 0xef])
self._color_temp = warm

@retry_bluetooth_connection_error
async def set_scheduler_on(self, days: int, hours: int, minutes: int, enabled: bool):
if enabled:
Expand Down Expand Up @@ -426,6 +418,7 @@ async def update(self):
#await self._device.start_notify(self._read_uuid, create_status_callback(future))
#await self._write([0x7e, 0x00, 0x01, 0xfa, 0x00, 0x00, 0x00, 0x00, 0xef])
#await self._write([0x7e, 0x00, 0x10])
#await self._write([0xef, 0x01, 0x77])
#await self._write([0x10])
#await asyncio.wait_for(future, 5.0)
#await self._device.stop_notify(self._read_uuid)
Expand Down Expand Up @@ -486,11 +479,11 @@ async def _ensure_connected(self) -> None:

async def _login_command(self):
try:
if self._device.name.lower().startswith("melk"):
if self._device.name.lower().startswith("modelx"):
LOGGER.debug("Executing login command for: %s; RSSI: %s", self.name, self.rssi)
self._write([0x7e, 0x07, 0x83])
await self._write([0x7e, 0x07, 0x83])
await asyncio.sleep(1)
self._write([0x7e, 0x04, 0x04])
await self._write([0x7e, 0x04, 0x04])
await asyncio.sleep(1)
else:
LOGGER.debug("login command for: %s not needed; RSSI: %s", self.name, self.rssi)
Expand All @@ -500,6 +493,22 @@ async def _login_command(self):
track = traceback.format_exc()
LOGGER.debug(track)

async def _init_command(self):
try:
if self._device.name.lower().startswith("melk"):
LOGGER.debug("Executing init command for: %s; RSSI: %s", self.name, self.rssi)
await self._write([0x7e, 0x07, 0x83])
await asyncio.sleep(1)
await self._write([0x7e, 0x04, 0x04])
await asyncio.sleep(1)
else:
LOGGER.debug("init command for: %s not needed; RSSI: %s", self.name, self.rssi)

except (Exception) as error:
LOGGER.error("Error login command: %s", error)
track = traceback.format_exc()
LOGGER.debug(track)

def _notification_handler(self, _sender: int, data: bytearray) -> None:
"""Handle notification responses."""
LOGGER.debug("%s: Notification received: %s", self.name, data.hex())
Expand Down
33 changes: 8 additions & 25 deletions custom_components/elkbledom/light.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import voluptuous as vol
from typing import Any, Optional, Tuple
Expand Down Expand Up @@ -73,31 +75,12 @@ def min_color_temp_kelvin(self):
def effect_list(self):
return EFFECTS_list

@property
def effect(self):
return self._attr_effect

@property
def supported_features(self) -> int:
"""Flag supported features."""
return self._attr_supported_features

@property
def supported_color_modes(self) -> int:
"""Flag supported color modes."""
return self._attr_supported_color_modes

@property
def rgb_color(self):
if self._instance.rgb_color:
return match_max_scale((255,), self._instance.rgb_color)
return None

@property
def color_mode(self) -> ColorMode | str | None:
"""Return the color mode of the light."""
return self._attr_color_mode

@property
def device_info(self):
"""Return device info."""
Expand All @@ -115,10 +98,10 @@ 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
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} color mode: {self._attr_color_mode}")
Expand All @@ -128,7 +111,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
LOGGER.debug("Change color to white to reset led strip when other infrared control interact")
self._attr_color_mode = ColorMode.WHITE
self._attr_effect = None
await self._instance.set_white()
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_brightness(kwargs[ATTR_BRIGHTNESS])
Expand All @@ -142,7 +125,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
if ATTR_WHITE in kwargs:
self._attr_color_mode = ColorMode.WHITE
self._attr_effect = None
await self._instance.set_white()
await self._instance.set_color(self._transform_color_brightness((255, 255, 255), kwargs[ATTR_WHITE]))

if ATTR_RGB_COLOR in kwargs:
self._attr_color_mode = ColorMode.RGB
Expand Down

0 comments on commit f1f90e4

Please sign in to comment.