Skip to content

Commit

Permalink
Use Kelvin as the preferred color temperature unit
Browse files Browse the repository at this point in the history
  • Loading branch information
caibinqing committed Feb 4, 2025
1 parent 249e000 commit 707c53b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions custom_components/xiaomi_gateway3/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_MODE,
ATTR_COLOR_TEMP,
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_RGB_COLOR,
Expand All @@ -15,11 +14,15 @@
LightEntityFeature,
)
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util import color as color_util

from .core.gate.base import XGateway
from .hass.entity import XEntity


ATTR_COLOR_TEMP = "color_temp"


# noinspection PyUnusedLocal
async def async_setup_entry(hass, entry, async_add_entities) -> None:
XEntity.ADD[entry.entry_id + "light"] = async_add_entities
Expand All @@ -39,11 +42,11 @@ def on_init(self):
self._attr_color_mode = ColorMode.COLOR_TEMP
modes.add(ColorMode.COLOR_TEMP)
if hasattr(conv, "minm") and hasattr(conv, "maxm"):
self._attr_min_mireds = conv.minm
self._attr_max_mireds = conv.maxm
self._attr_max_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(conv.minm)
self._attr_min_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(conv.maxm)
elif hasattr(conv, "mink") and hasattr(conv, "maxk"):
self._attr_min_mireds = int(1000000 / conv.maxk)
self._attr_max_mireds = int(1000000 / conv.mink)
self._attr_max_color_temp_kelvin = conv.maxk
self._attr_min_color_temp_kelvin = conv.mink
elif conv.attr == ATTR_HS_COLOR:
self.listen_attrs.add(conv.attr)
modes.add(ColorMode.HS)
Expand All @@ -65,7 +68,7 @@ def set_state(self, data: dict):
if ATTR_BRIGHTNESS in data:
self._attr_brightness = data[ATTR_BRIGHTNESS]
if ATTR_COLOR_TEMP in data:
self._attr_color_temp = data[ATTR_COLOR_TEMP]
self._attr_color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(data[ATTR_COLOR_TEMP])
self._attr_color_mode = ColorMode.COLOR_TEMP
if ATTR_HS_COLOR in data:
self._attr_hs_color = data[ATTR_HS_COLOR]
Expand All @@ -82,7 +85,7 @@ def get_state(self) -> dict:
return {
self.attr: self._attr_is_on,
ATTR_BRIGHTNESS: self._attr_brightness,
ATTR_COLOR_TEMP: self._attr_color_temp,
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,
}

async def async_turn_on(self, **kwargs):
Expand Down

0 comments on commit 707c53b

Please sign in to comment.