Skip to content

Commit

Permalink
Merge pull request #158 from andvikt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
andvikt authored Oct 10, 2023
2 parents 58a4680 + 082c647 commit 5bb2425
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.1.8b0
current_version = 1.1.8b1
parse = (?P<major>\d+)(\.(?P<minor>\d+))(\.(?P<patch>\d+))(?P<release>[bf]*)(?P<build>\d*)
commit = True
tag = True
Expand Down
114 changes: 64 additions & 50 deletions custom_components/mega/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
SUPPORT_BRIGHTNESS,
LightEntity,
SUPPORT_TRANSITION,
SUPPORT_COLOR, ColorMode, LightEntityFeature,
SUPPORT_COLOR,
ColorMode,
LightEntityFeature,
# SUPPORT_WHITE_VALUE
)
from homeassistant.config_entries import ConfigEntry
Expand All @@ -36,7 +38,15 @@
CONF_SWITCH,
DOMAIN,
CONF_CUSTOM,
CONF_SKIP, CONF_LED, CONF_WS28XX, CONF_PORTS, CONF_WHITE_SEP, CONF_SMOOTH, CONF_ORDER, CONF_CHIP, RGB,
CONF_SKIP,
CONF_LED,
CONF_WS28XX,
CONF_PORTS,
CONF_WHITE_SEP,
CONF_SMOOTH,
CONF_ORDER,
CONF_CHIP,
RGB,
)
from .tools import int_ignore, map_reorder_rgb

Expand Down Expand Up @@ -64,55 +74,62 @@


async def async_setup_platform(hass, config, add_entities, discovery_info=None):
lg.warning('mega integration does not support yaml for lights, please use UI configuration')
lg.warning(
"mega integration does not support yaml for lights, please use UI configuration"
)
return True


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_devices):
async def async_setup_entry(
hass: HomeAssistant, config_entry: ConfigEntry, async_add_devices
):
mid = config_entry.data[CONF_ID]
hub: MegaD = hass.data['mega'][mid]
hub: MegaD = hass.data["mega"][mid]
devices = []
customize = hass.data.get(DOMAIN, {}).get(CONF_CUSTOM, {}).get(mid, {})
skip = []
if CONF_LED in customize:
for entity_id, conf in customize[CONF_LED].items():
ports = conf.get(CONF_PORTS) or [conf.get(CONF_PORT)]
skip.extend(ports)
devices.append(MegaRGBW(
mega=hub,
port=ports,
name=entity_id,
customize=conf,
id_suffix=entity_id,
config_entry=config_entry
))
for port, cfg in config_entry.data.get('light', {}).items():
devices.append(
MegaRGBW(
mega=hub,
port=ports,
name=entity_id,
customize=conf,
id_suffix=entity_id,
config_entry=config_entry,
)
)
for port, cfg in config_entry.data.get("light", {}).items():
port = int_ignore(port)
c = customize.get(port, {})
if c.get(CONF_SKIP, False) or port in skip or c.get(CONF_DOMAIN, 'light') != 'light':
if (
c.get(CONF_SKIP, False)
or port in skip
or c.get(CONF_DOMAIN, "light") != "light"
):
continue
for data in cfg:
hub.lg.debug(f'add light on port %s with data %s', port, data)
hub.lg.debug(f"add light on port %s with data %s", port, data)
light = MegaLight(mega=hub, port=port, config_entry=config_entry, **data)
if '<' in light.name:
if "<" in light.name:
continue
devices.append(light)

async_add_devices(devices)


class MegaLight(MegaOutPort, LightEntity):

@property
def supported_features(self):
return (
(SUPPORT_BRIGHTNESS if self.dimmer else 0) |
(SUPPORT_TRANSITION if self.dimmer else 0)
return (SUPPORT_BRIGHTNESS if self.dimmer else 0) | (
SUPPORT_TRANSITION if self.dimmer else 0
)


class MegaRGBW(LightEntity, BaseMegaEntity):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._is_on = None
Expand All @@ -123,7 +140,7 @@ def __init__(self, *args, **kwargs):
self._task: asyncio.Task = None
self._restore = None
self.smooth: timedelta = self.customize[CONF_SMOOTH]
self._color_order = self.customize.get(CONF_ORDER, 'rgb')
self._color_order = self.customize.get(CONF_ORDER, "rgb")
self._last_called: float = 0
self._max_values = None

Expand All @@ -146,12 +163,11 @@ def chip(self) -> int:
def is_ws(self):
return self.customize.get(CONF_WS28XX)


@property
def supported_color_modes(self) -> set[ColorMode] | set[str] | None:
return {
ColorMode.BRIGHTNESS,
ColorMode.RGB if len(self.port) != 4 else ColorMode.RGBW
ColorMode.RGB if len(self.port) != 4 else ColorMode.RGBW,
}

@property
Expand All @@ -164,7 +180,7 @@ def color_mode(self) -> ColorMode | str | None:
@property
def white_value(self):
# if self.supported_features & SUPPORT_WHITE_VALUE:
return float(self.get_attribute('white_value', 0))
return float(self.get_attribute("white_value", 0))

@property
def rgb_color(self) -> tuple[int, int, int] | None:
Expand All @@ -177,15 +193,15 @@ def rgbw_color(self) -> tuple[int, int, int, int] | None:

@property
def brightness(self):
return float(self.get_attribute('brightness', 0))
return float(self.get_attribute("brightness", 0))

@property
def hs_color(self):
return self.get_attribute('hs_color', [0, 0])
return self.get_attribute("hs_color", [0, 0])

@property
def is_on(self):
return self.get_attribute('is_on', False)
return self.get_attribute("is_on", False)

@property
def supported_features(self):
Expand All @@ -195,17 +211,15 @@ def get_rgbw(self):
if not self.is_on:
return [0 for x in range(len(self.port))] if not self.is_ws else [0] * 3
rgb = colorsys.hsv_to_rgb(
self.hs_color[0]/360, self.hs_color[1]/100, self.brightness / 255
self.hs_color[0] / 360, self.hs_color[1] / 100, self.brightness / 255
)
rgb = [x for x in rgb]
if self.white_value is not None:
white = self.white_value
if not self.customize.get(CONF_WHITE_SEP):
white = white * (self.brightness / 255)
rgb.append(white / 255)
rgb = [
round(x * self.max_values[i]) for i, x in enumerate(rgb)
]
rgb = [round(x * self.max_values[i]) for i, x in enumerate(rgb)]
if self.is_ws:
# восстанавливаем мэпинг
rgb = map_reorder_rgb(rgb, RGB, self._color_order)
Expand All @@ -215,7 +229,7 @@ async def async_turn_on(self, **kwargs):
if (time.time() - self._last_called) < 0.1:
return
self._last_called = time.time()
self.lg.debug(f'turn on %s with kwargs %s', self.entity_id, kwargs)
self.lg.debug(f"turn on %s with kwargs %s", self.entity_id, kwargs)
if self._restore is not None:
self._restore.update(kwargs)
kwargs = self._restore
Expand All @@ -231,9 +245,9 @@ async def async_turn_off(self, **kwargs):
return
self._last_called = time.time()
self._restore = {
'hs_color': self.hs_color,
'brightness': self.brightness,
'white_value': self.white_value,
"hs_color": self.hs_color,
"brightness": self.brightness,
"white_value": self.white_value,
}
_before = self.get_rgbw()
self._is_on = False
Expand All @@ -242,21 +256,21 @@ async def async_turn_off(self, **kwargs):
self._task = asyncio.create_task(self.set_color(_before, **kwargs))

async def set_color(self, _before, **kwargs):
transition = kwargs.get('transition')
transition = kwargs.get("transition")
update_state = transition is not None and transition > 3
_after = None
for item, value in kwargs.items():
setattr(self, f'_{item}', value)
if item == 'rgb_color':
_after = value
setattr(self, f"_{item}", value)
if item == "rgb_color":
_after = map_reorder_rgb(value, RGB, self._color_order)
_after = _after or self.get_rgbw()
self._rgb_color = tuple(_after[:3])
if transition is None:
transition = self.smooth.total_seconds()
ratio = self.calc_speed_ratio(_before, _after)
transition = transition * ratio
self.async_write_ha_state()
ports = self.port if not self.is_ws else self.port*3
ports = self.port if not self.is_ws else self.port * 3
config = [(port, _before[i], _after[i]) for i, port in enumerate(ports)]
try:
await self.mega.smooth_dim(
Expand All @@ -272,7 +286,7 @@ async def set_color(self, _before, **kwargs):
except asyncio.CancelledError:
return
except:
self.lg.exception('while dimming')
self.lg.exception("while dimming")

async def async_will_remove_from_hass(self) -> None:
await super().async_will_remove_from_hass()
Expand All @@ -287,10 +301,10 @@ def _update_from_rgb(self, rgbw, update_state=False):
w = None
rgb = rgbw
if self.is_ws:
rgb = map_reorder_rgb(
rgb, self._color_order, RGB
)
h, s, v = colorsys.rgb_to_hsv(*[x/self.max_values[i] for i, x in enumerate(rgb)])
rgb = map_reorder_rgb(rgb, self._color_order, RGB)
h, s, v = colorsys.rgb_to_hsv(
*[x / self.max_values[i] for i, x in enumerate(rgb)]
)
h *= 360
s *= 100
v *= 255
Expand All @@ -299,7 +313,7 @@ def _update_from_rgb(self, rgbw, update_state=False):
self._brightness = v
if w is not None:
if not self.customize.get(CONF_WHITE_SEP):
w = w/(self._brightness / 255)
w = w / (self._brightness / 255)
else:
w = w
w = w / (self.max_values[-1] / 255)
Expand All @@ -324,7 +338,7 @@ async def async_update(self):
return
data = data.get(x, None)
if isinstance(data, dict):
data = data.get('value')
data = data.get("value")
data = safe_int(data)
if data is None:
return
Expand All @@ -341,4 +355,4 @@ def calc_speed_ratio(self, _before, _after):
ret = r
else:
ret = max([r, ret])
return ret
return ret
2 changes: 1 addition & 1 deletion custom_components/mega/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"@andvikt"
],
"issue_tracker": "https://github.com/andvikt/mega_hacs/issues",
"version": "v1.1.8b0"
"version": "v1.1.8b1"
}

0 comments on commit 5bb2425

Please sign in to comment.