Migrate nanoleaf lights to use Kelvin (#132797)

This commit is contained in:
epenet 2024-12-10 10:17:55 +01:00 committed by GitHub
parent 4880849074
commit 28d01d88a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,12 +2,11 @@
from __future__ import annotations
import math
from typing import Any
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_TRANSITION,
@ -17,10 +16,6 @@ from homeassistant.components.light import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.color import (
color_temperature_kelvin_to_mired as kelvin_to_mired,
color_temperature_mired_to_kelvin as mired_to_kelvin,
)
from . import NanoleafConfigEntry
from .coordinator import NanoleafCoordinator
@ -51,10 +46,8 @@ class NanoleafLight(NanoleafEntity, LightEntity):
"""Initialize the Nanoleaf light."""
super().__init__(coordinator)
self._attr_unique_id = self._nanoleaf.serial_no
self._attr_min_mireds = math.ceil(
1000000 / self._nanoleaf.color_temperature_max
)
self._attr_max_mireds = kelvin_to_mired(self._nanoleaf.color_temperature_min)
self._attr_max_color_temp_kelvin = self._nanoleaf.color_temperature_max
self._attr_min_color_temp_kelvin = self._nanoleaf.color_temperature_min
@property
def brightness(self) -> int:
@ -62,9 +55,9 @@ class NanoleafLight(NanoleafEntity, LightEntity):
return int(self._nanoleaf.brightness * 2.55)
@property
def color_temp(self) -> int:
"""Return the current color temperature."""
return kelvin_to_mired(self._nanoleaf.color_temperature)
def color_temp_kelvin(self) -> int | None:
"""Return the color temperature value in Kelvin."""
return self._nanoleaf.color_temperature
@property
def effect(self) -> str | None:
@ -106,7 +99,7 @@ class NanoleafLight(NanoleafEntity, LightEntity):
"""Instruct the light to turn on."""
brightness = kwargs.get(ATTR_BRIGHTNESS)
hs_color = kwargs.get(ATTR_HS_COLOR)
color_temp_mired = kwargs.get(ATTR_COLOR_TEMP)
color_temp_kelvin = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
effect = kwargs.get(ATTR_EFFECT)
transition = kwargs.get(ATTR_TRANSITION)
@ -120,10 +113,8 @@ class NanoleafLight(NanoleafEntity, LightEntity):
hue, saturation = hs_color
await self._nanoleaf.set_hue(int(hue))
await self._nanoleaf.set_saturation(int(saturation))
elif color_temp_mired:
await self._nanoleaf.set_color_temperature(
mired_to_kelvin(color_temp_mired)
)
elif color_temp_kelvin:
await self._nanoleaf.set_color_temperature(color_temp_kelvin)
if transition:
if brightness: # tune to the required brightness in n seconds
await self._nanoleaf.set_brightness(