Skip to content

Commit 707c53b

Browse files
committed
Use Kelvin as the preferred color temperature unit
1 parent 249e000 commit 707c53b

File tree

1 file changed

+10
-7
lines changed
  • custom_components/xiaomi_gateway3

1 file changed

+10
-7
lines changed

custom_components/xiaomi_gateway3/light.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from homeassistant.components.light import (
66
ATTR_BRIGHTNESS,
77
ATTR_COLOR_MODE,
8-
ATTR_COLOR_TEMP,
98
ATTR_EFFECT,
109
ATTR_HS_COLOR,
1110
ATTR_RGB_COLOR,
@@ -15,11 +14,15 @@
1514
LightEntityFeature,
1615
)
1716
from homeassistant.helpers.restore_state import RestoreEntity
17+
from homeassistant.util import color as color_util
1818

1919
from .core.gate.base import XGateway
2020
from .hass.entity import XEntity
2121

2222

23+
ATTR_COLOR_TEMP = "color_temp"
24+
25+
2326
# noinspection PyUnusedLocal
2427
async def async_setup_entry(hass, entry, async_add_entities) -> None:
2528
XEntity.ADD[entry.entry_id + "light"] = async_add_entities
@@ -39,11 +42,11 @@ def on_init(self):
3942
self._attr_color_mode = ColorMode.COLOR_TEMP
4043
modes.add(ColorMode.COLOR_TEMP)
4144
if hasattr(conv, "minm") and hasattr(conv, "maxm"):
42-
self._attr_min_mireds = conv.minm
43-
self._attr_max_mireds = conv.maxm
45+
self._attr_max_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(conv.minm)
46+
self._attr_min_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(conv.maxm)
4447
elif hasattr(conv, "mink") and hasattr(conv, "maxk"):
45-
self._attr_min_mireds = int(1000000 / conv.maxk)
46-
self._attr_max_mireds = int(1000000 / conv.mink)
48+
self._attr_max_color_temp_kelvin = conv.maxk
49+
self._attr_min_color_temp_kelvin = conv.mink
4750
elif conv.attr == ATTR_HS_COLOR:
4851
self.listen_attrs.add(conv.attr)
4952
modes.add(ColorMode.HS)
@@ -65,7 +68,7 @@ def set_state(self, data: dict):
6568
if ATTR_BRIGHTNESS in data:
6669
self._attr_brightness = data[ATTR_BRIGHTNESS]
6770
if ATTR_COLOR_TEMP in data:
68-
self._attr_color_temp = data[ATTR_COLOR_TEMP]
71+
self._attr_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(data[ATTR_COLOR_TEMP])
6972
self._attr_color_mode = ColorMode.COLOR_TEMP
7073
if ATTR_HS_COLOR in data:
7174
self._attr_hs_color = data[ATTR_HS_COLOR]
@@ -82,7 +85,7 @@ def get_state(self) -> dict:
8285
return {
8386
self.attr: self._attr_is_on,
8487
ATTR_BRIGHTNESS: self._attr_brightness,
85-
ATTR_COLOR_TEMP: self._attr_color_temp,
88+
ATTR_COLOR_TEMP: color_util.color_temperature_kelvin_to_mired(self._attr_color_temp_kelvin) if self._attr_color_temp_kelvin is not None else None,
8689
}
8790

8891
async def async_turn_on(self, **kwargs):

0 commit comments

Comments
 (0)