Skip to content

Use Kelvin as the preferred color temperature unit #1558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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