Skip to content

Commit ba7f15d

Browse files
committed
Fixed a bunch of bugs ahead of the next beta release
1 parent ffc9bf7 commit ba7f15d

File tree

10 files changed

+88
-39
lines changed

10 files changed

+88
-39
lines changed

custom_components/lednetwf_ble/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from homeassistant.const import CONF_MAC, EVENT_HOMEASSISTANT_STOP
66
from homeassistant.const import Platform
77

8-
from .const import DOMAIN, CONF_RESET, CONF_DELAY, CONF_LEDCOUNT, CONF_LEDTYPE, CONF_COLORORDER
8+
from .const import DOMAIN, CONF_DELAY
99
from .lednetwf import LEDNETWFInstance
1010
import logging
1111

@@ -20,9 +20,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2020
config = entry.data
2121
options = entry.options
2222
instance = LEDNETWFInstance(entry.data[CONF_MAC], hass, config, options)
23-
# reset = entry.options.get(CONF_RESET, None) or entry.data.get(CONF_RESET, None)
2423
delay = entry.options.get(CONF_DELAY, None) or entry.data.get(CONF_DELAY, None)
25-
# LOGGER.debug("Config Reset data: %s and config delay data: %s", reset, delay)
2624
hass.data.setdefault(DOMAIN, {})
2725
hass.data[DOMAIN][entry.entry_id] = instance
2826

custom_components/lednetwf_ble/config_flow.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from home_assistant_bluetooth import BluetoothServiceInfo
1919
import importlib
2020
import pkgutil
21+
import sys
2122

2223
from .const import (
2324
DOMAIN,
@@ -36,12 +37,35 @@
3637
SUPPORTED_MODELS = []
3738

3839
package = __package__
39-
for _, module_name, _ in pkgutil.iter_modules([f"{package.replace('.', '/')}/models"]):
40+
filename = __file__
41+
42+
# LOGGER.debug(f"Package: {package}")
43+
LOGGER.debug(f"File: {__file__}")
44+
my_name = filename[filename.rfind('/')+1:]
45+
models_path = f"{filename.replace(my_name, 'models')}"
46+
LOGGER.debug(f"Models path: {models_path}")
47+
48+
# old Working vvvv
49+
# for _, module_name, _ in pkgutil.iter_modules([f"{package.replace('.', '/')}/models"]):
50+
# # for _, module_name, _ in pkgutil.iter_modules([models_path]):
51+
# LOGGER.debug(f"Module name: {module_name}")
52+
# if module_name.startswith('model_0x'):
53+
# module = importlib.import_module(f'.models.{module_name}', package)
54+
# LOGGER.debug(f"Importing module: {models_path}/{module_name}")
55+
# # module = importlib.import_module(f'{models_path}/{module_name}')
56+
# if hasattr(module, "SUPPORTED_MODELS"):
57+
# LOGGER.debug(f"Supported models: {getattr(module, 'SUPPORTED_MODELS')}")
58+
# SUPPORTED_MODELS.extend(getattr(module, "SUPPORTED_MODELS"))
59+
# old Working ^^^^
60+
61+
for _, module_name, _ in pkgutil.iter_modules([models_path]):
62+
LOGGER.debug(f"Module name: {module_name}")
4063
if module_name.startswith('model_0x'):
41-
module = importlib.import_module(f'.models.{module_name}', package)
42-
if hasattr(module, "SUPPORTED_MODELS"):
43-
LOGGER.debug(f"Supported models: {getattr(module, 'SUPPORTED_MODELS')}")
44-
SUPPORTED_MODELS.extend(getattr(module, "SUPPORTED_MODELS"))
64+
m = importlib.import_module(f'{package}.models.{module_name}')
65+
if hasattr(m, "SUPPORTED_MODELS"):
66+
LOGGER.debug(f"Supported models: {getattr(m, 'SUPPORTED_MODELS')}")
67+
SUPPORTED_MODELS.extend(getattr(m, "SUPPORTED_MODELS"))
68+
4569
LOGGER.debug(f"All supported modules: {SUPPORTED_MODELS}")
4670

4771
class DeviceData(BluetoothData):
@@ -216,7 +240,7 @@ async def toggle_light(self):
216240
await self._instance.update()
217241
LOGGER.debug(f"Sending initial packets")
218242
await self._instance.send_initial_packets()
219-
await self._instance._write_packet(self._instance._model_interface.GET_LED_SETTINGS_PACKET)
243+
await self._instance._write(self._instance._model_interface.GET_LED_SETTINGS_PACKET)
220244
# await asyncio.sleep(1)
221245
for n in range(3):
222246
LOGGER.debug(f"Turning on and off: {n}")

custom_components/lednetwf_ble/const.py

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
CONF_LEDTYPE = "ledtype"
99
CONF_COLORORDER = "colororder"
1010
CONF_MODEL = "model"
11-
# SUPPORTED_MODELS = [0x00, 0x53, 0x54, 0x56, 0x62]
12-
1311

1412
class LedTypes_StripLight(Enum):
1513
WS2812B = 0x01

custom_components/lednetwf_ble/lednetwf.py

+30-9
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,38 @@
3838

3939
# Iterate through all modules in the current package
4040
package = __package__
41-
for _, module_name, _ in pkgutil.iter_modules([f"{package.replace('.', '/')}/models"]):
41+
filename = __file__
42+
# LOGGER.debug(f"Package: {package}")
43+
LOGGER.debug(f"File: {__file__}")
44+
my_name = filename[filename.rfind('/')+1:]
45+
models_path = f"{filename.replace(my_name, 'models')}"
46+
LOGGER.debug(f"Models path: {models_path}")
47+
48+
# for _, module_name, _ in pkgutil.iter_modules([f"{package.replace('.', '/')}/models"]):
49+
# LOGGER.debug(f"Module name: {module_name}")
50+
# if module_name.startswith('model_0x'):
51+
# module = importlib.import_module(f'.models.{module_name}', package)
52+
# # module = importlib.import_module(f'{models_path}/{module_name}.py')
53+
# LOGGER.debug(f"Module: {module}")
54+
# LOGGER.debug(f"Dir: {dir(module)}")
55+
# class_name = f'Model{module_name.split("_")[1]}'
56+
# if hasattr(module, class_name):
57+
# globals()[class_name] = getattr(module, class_name)
58+
# if hasattr(module, "SUPPORTED_MODELS"):
59+
# LOGGER.debug(f"Supported models: {getattr(module, 'SUPPORTED_MODELS')}")
60+
# SUPPORTED_MODELS[class_name] = getattr(module, "SUPPORTED_MODELS")
61+
62+
63+
for _, module_name, _ in pkgutil.iter_modules([models_path]):
64+
LOGGER.debug(f"Module name: {module_name}")
4265
if module_name.startswith('model_0x'):
43-
module = importlib.import_module(f'.models.{module_name}', package)
44-
LOGGER.debug(f"Module: {module}")
45-
LOGGER.debug(f"Dir: {dir(module)}")
66+
m = importlib.import_module(f'{package}.models.{module_name}')
4667
class_name = f'Model{module_name.split("_")[1]}'
47-
if hasattr(module, class_name):
48-
globals()[class_name] = getattr(module, class_name)
49-
if hasattr(module, "SUPPORTED_MODELS"):
50-
LOGGER.debug(f"Supported models: {getattr(module, 'SUPPORTED_MODELS')}")
51-
SUPPORTED_MODELS[class_name] = getattr(module, "SUPPORTED_MODELS")
68+
if hasattr(m, class_name):
69+
globals()[class_name] = getattr(m, class_name)
70+
if hasattr(m, "SUPPORTED_MODELS"):
71+
LOGGER.debug(f"Supported models: {getattr(m, 'SUPPORTED_MODELS')}")
72+
SUPPORTED_MODELS[class_name] = getattr(m, "SUPPORTED_MODELS")
5273

5374
LOGGER.debug(f"All supported modules: {SUPPORTED_MODELS}")
5475

custom_components/lednetwf_ble/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"bleak>=0.17.0",
2424
"bluetooth-sensor-state-data>=1.5.0"
2525
],
26-
"version": "0.0.13"
26+
"version": "0.0.14-beta.1"
2727
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Creating this file should help things recognize the models folder as a module

custom_components/lednetwf_ble/models/model_0x53.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
EFFECT_OFF
1111
)
1212

13-
SUPPORTED_MODELS = [0x00, 0x53, 0x55]
13+
SUPPORTED_MODELS = [0x00, 0x53, 0x55] # Probably 0x55 is not supported here, but in 0x54 instead
1414

1515
EFFECTS_LIST_0x53 = [
1616
"Gold Ring",

custom_components/lednetwf_ble/models/model_0x56.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def __init__(self, manu_data):
4242
self.icon = "mdi:led-strip-variant"
4343
self.effect_list = EFFECT_LIST_0x56
4444

45+
if isinstance(self.manu_data, str):
46+
self.manu_data = [ord(c) for c in self.manu_data]
4547
LOGGER.debug(f"Manu data: {[hex(x) for x in self.manu_data]}")
4648
LOGGER.debug(f"Manu data 15: {hex(self.manu_data[15])}")
4749
LOGGER.debug(f"Manu data 16: {hex(self.manu_data[16])}")
@@ -57,10 +59,10 @@ def __init__(self, manu_data):
5759
if self.manu_data[16] != 0xf0:
5860
# We're not in a colour mode, so set the effect
5961
self.effect_speed = self.manu_data[17]
60-
if 0x02 <= self.manu_data[16] <= 0x0a:
62+
if 0x01 <= self.manu_data[16] <= 0x0a:
6163
self.effect = EFFECT_ID_TO_NAME_0x56[self.manu_data[16] << 8]
6264
else:
63-
self._effect = EFFECT_OFF
65+
self.effect = EFFECT_OFF
6466
elif self.manu_data[15] == 0x62:
6567
# Music reactive mode.
6668
self._color_mode = ColorMode.BRIGHTNESS
@@ -89,7 +91,7 @@ def set_color(self, hs_color, brightness):
8991
self.color_mode = ColorMode.HS
9092
self.hs_color = hs_color
9193
self.brightness = brightness
92-
self.effect = EFFECT_OFF
94+
#self.effect = EFFECT_OFF # The effect is NOT actually off when setting a colour. Static effect 1 is close to effect off, but it's still an effect.
9395
rgb_color = self.hsv_to_rgb((hs_color[0], hs_color[1], self.brightness))
9496
LOGGER.debug(f"Setting RGB colour: {rgb_color}")
9597
background_col = [0,0,0] # Consider adding support for this in the future? For now, set black
@@ -209,7 +211,7 @@ def notification_handler(self, data):
209211
return None
210212
payload = bytearray.fromhex(payload)
211213
LOGGER.debug(f"N: Response Payload: {' '.join([f'{byte:02X}' for byte in payload])}")
212-
# return
214+
213215
if payload[0] == 0x81:
214216
# Status request response
215217
power = payload[2]
@@ -225,23 +227,29 @@ def notification_handler(self, data):
225227
hsv_color = super().rgb_to_hsv(rgb_color)
226228
self.hs_color = tuple(hsv_color[0:2])
227229
self.brightness = int(hsv_color[2])
230+
LOGGER.debug("Light is in colour mode")
228231
LOGGER.debug(f"RGB colour: {rgb_color}")
229232
LOGGER.debug(f"HS colour: {self.hs_color}")
230233
LOGGER.debug(f"Brightness: {self.brightness}")
231234
self.effect = EFFECT_OFF
232235
self.color_mode = ColorMode.HS
233236
self.color_temperature_kelvin = None
234-
elif selected_effect == 0x01:
237+
# elif selected_effect == 0x01: # We don't really need this any more, deal with it below instead
238+
# self.color_mode = ColorMode.HS
239+
# # self.effect = EFFECT_OFF
240+
# self.effect = EFFECT_ID_TO_NAME_0x56[selected_effect << 8] # Effect 1 is still an effect
241+
# hs_color = self.rgb_to_hsv(payload[6:9])
242+
# self.hs_color = hs_color[0:2]
243+
# self.brightness = hs_color[2]
244+
elif 0x01 <= selected_effect <= 0x0a:
235245
self.color_mode = ColorMode.HS
236-
self.effect = EFFECT_OFF
246+
self.effect = EFFECT_ID_TO_NAME_0x56[selected_effect << 8]
247+
self.effect_speed = payload[5]
237248
hs_color = self.rgb_to_hsv(payload[6:9])
249+
rgb_color = tuple(int(b) for b in payload[6:9])
250+
LOGGER.debug(f"RGB Color: {rgb_color}, HS colour: {hs_color}, Brightness: {hs_color[2]}")
238251
self.hs_color = hs_color[0:2]
239252
self.brightness = hs_color[2]
240-
elif 0x02 <= selected_effect <= 0x0a:
241-
self.color_mode = ColorMode.HS
242-
self.effect = EFFECT_ID_TO_NAME_0x56[selected_effect << 8]
243-
self.effect_speed = payload[5]
244-
# TODO: What about colours and brightness?
245253
elif mode == 0x62:
246254
# Music reactive mode
247255
# TODO: Brightness?

custom_components/lednetwf_ble/models/model_abstractions.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def process_manu_data(self, manu_data):
5050
self.is_on = True if self.manu_data[14] == 0x23 else False
5151
else:
5252
LOGGER.debug("No manu data")
53-
self.manu_data = chr(0) * 25 # Mock manu_data to prevent truth testing errors in the future
53+
self.manu_data = bytearray(25)
5454
self.fw_major = 0x00
5555
self.fw_minor = "Unknown version"
5656
self.led_count = None
@@ -87,7 +87,6 @@ def set_brightness(self):
8787
def set_color_temp_kelvin(self):
8888
return NotImplementedError("This method should be implemented by the subclass")
8989
def notification_handler(self):
90-
LOGGER.debug("ZZZ Notification handler not implemented")
9190
raise NotImplementedError("This method should be implemented by the subclass")
9291
def rgb_to_hsv(self,rgb):
9392
# Home Assistant expects HS in the range 0-360, 0-100

custom_components/lednetwf_ble/translations/en.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"mac": "Bluetooth MAC address",
77
"name": "Name"
88
},
9-
"title": "Pick LEDnetWF light"
9+
"title": "Select LEDnetWF light"
1010
},
1111
"validate": {
1212
"data": {
13-
"retry": "Retry validate connection?",
13+
"retry": "Retry?",
1414
"flicker": "Did the light blink?"
1515
},
16-
"title": "Validate LEDnetWF connection"
16+
"title": "Validate connection"
1717
},
1818
"manual": {
1919
"data": {
@@ -45,7 +45,7 @@
4545
},
4646
"init" : {
4747
"data": {
48-
"reset": "Reset color when led turn on",
48+
"reset": "Reset color when led turns on",
4949
"delay": "Disconnect delay (0 = never disconnect)",
5050
"ledcount": "Number of LEDs",
5151
"name": "Name",

0 commit comments

Comments
 (0)