1 """Utils for Magic Home."""
3 from __future__
import annotations
5 from flux_led.aio
import AIOWifiLedBulb
6 from flux_led.const
import COLOR_MODE_DIM
as FLUX_COLOR_MODE_DIM, MultiColorEffects
11 from .const
import FLUX_COLOR_MODE_TO_HASS, MIN_RGB_BRIGHTNESS
15 color_modes = device.color_modes
17 return {ColorMode.ONOFF}
22 """Convert a device registry formatted mac to flux mac."""
23 return None if mac
is None else mac.replace(
":",
"").upper()
27 return const_option.replace(
"_",
" ").title()
31 """Check if a mac address is only one digit off.
33 Some of the devices have two mac addresses which are
34 one off from each other. We need to treat them as the same
35 since its the same device.
37 mac_int_1 =
int(formatted_mac_1.replace(
":",
""), 16)
38 mac_int_2 =
int(formatted_mac_2.replace(
":",
""), 16)
39 return abs(mac_int_1 - mac_int_2) < 2
43 flux_color_mode: str |
None, flux_color_modes: set[str]
45 """Map the flux color mode to Home Assistant color mode."""
46 if flux_color_mode
is None:
47 return ColorMode.ONOFF
48 if flux_color_mode == FLUX_COLOR_MODE_DIM:
49 if len(flux_color_modes) > 1:
50 return ColorMode.WHITE
51 return ColorMode.BRIGHTNESS
52 return FLUX_COLOR_MODE_TO_HASS.get(flux_color_mode, ColorMode.ONOFF)
56 """Convert hass brightness to effect brightness."""
57 return round(brightness / 255 * 100)
61 """Convert an multicolor effect string to MultiColorEffects."""
62 for effect
in MultiColorEffects:
63 if effect.name.lower() == effect_str:
70 """RGB brightness is zero."""
71 return all(byte == 0
for byte
in rgb)
75 """Ensure the RGB value will not turn off the device from a turn on command."""
77 return (MIN_RGB_BRIGHTNESS, MIN_RGB_BRIGHTNESS, MIN_RGB_BRIGHTNESS)
82 """Scale an RGB tuple to minimum brightness."""
87 rgbw: tuple[int, int, int, int], current_rgbw: tuple[int, int, int, int]
88 ) -> tuple[int, int, int, int]:
89 """Ensure the RGBW value will not turn off the device from a turn on command.
91 For RGBW, we also need to ensure that there is at least one
92 value in the RGB fields or the device will switch to CCT mode unexpectedly.
94 If the new value being set is all zeros, scale the current
95 color to brightness of 1 so we do not unexpected switch to white
103 rgbwc: tuple[int, int, int, int, int], current_rgbwc: tuple[int, int, int, int, int]
104 ) -> tuple[int, int, int, int, int]:
105 """Ensure the RGBWC value will not turn off the device from a turn on command.
107 For RGBWC, we also need to ensure that there is at least one
108 value in the RGB fields or the device will switch to CCT mode unexpectedly
110 If the new value being set is all zeros, scale the current
111 color to brightness of 1 so we do not unexpected switch to white
str _human_readable_option(str const_option)
bool mac_matches_by_one(str formatted_mac_1, str formatted_mac_2)
str|None format_as_flux_mac(str|None mac)
bool _is_zero_rgb_brightness(tuple[int, int, int] rgb)
int _effect_brightness(int brightness)
tuple[int, int, int] _min_scaled_rgb_brightness(tuple[int, int, int] rgb)
ColorMode _flux_color_mode_to_hass(str|None flux_color_mode, set[str] flux_color_modes)
tuple[int, int, int] _min_rgb_brightness(tuple[int, int, int] rgb)
tuple[int, int, int, int] _min_rgbw_brightness(tuple[int, int, int, int] rgbw, tuple[int, int, int, int] current_rgbw)
tuple[int, int, int, int, int] _min_rgbwc_brightness(tuple[int, int, int, int, int] rgbwc, tuple[int, int, int, int, int] current_rgbwc)
MultiColorEffects _str_to_multi_color_effect(str effect_str)
set[str] _hass_color_modes(AIOWifiLedBulb device)
tuple[int, int, int] color_hsv_to_RGB(float iH, float iS, float iV)
tuple[float, float, float] color_RGB_to_hsv(float iR, float iG, float iB)