Migrate opple lights to use Kelvin (#132697)

This commit is contained in:
epenet 2024-12-09 15:55:07 +01:00 committed by GitHub
parent 8d72443fd6
commit ac791bdd20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,7 +10,7 @@ import voluptuous as vol
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
ColorMode,
LightEntity,
@ -20,10 +20,6 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.color import (
color_temperature_kelvin_to_mired as kelvin_to_mired,
color_temperature_mired_to_kelvin as mired_to_kelvin,
)
_LOGGER = logging.getLogger(__name__)
@ -58,6 +54,8 @@ class OppleLight(LightEntity):
_attr_color_mode = ColorMode.COLOR_TEMP
_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
_attr_min_color_temp_kelvin = 3000 # 333 Mireds
_attr_max_color_temp_kelvin = 5700 # 175 Mireds
def __init__(self, name, host):
"""Initialize an Opple light."""
@ -67,7 +65,6 @@ class OppleLight(LightEntity):
self._name = name
self._is_on = None
self._brightness = None
self._color_temp = None
@property
def available(self) -> bool:
@ -94,21 +91,6 @@ class OppleLight(LightEntity):
"""Return the brightness of the light."""
return self._brightness
@property
def color_temp(self):
"""Return the color temperature of this light."""
return kelvin_to_mired(self._color_temp)
@property
def min_mireds(self):
"""Return minimum supported color temperature."""
return 175
@property
def max_mireds(self):
"""Return maximum supported color temperature."""
return 333
def turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
_LOGGER.debug("Turn on light %s %s", self._device.ip, kwargs)
@ -118,9 +100,11 @@ class OppleLight(LightEntity):
if ATTR_BRIGHTNESS in kwargs and self.brightness != kwargs[ATTR_BRIGHTNESS]:
self._device.brightness = kwargs[ATTR_BRIGHTNESS]
if ATTR_COLOR_TEMP in kwargs and self.color_temp != kwargs[ATTR_COLOR_TEMP]:
color_temp = mired_to_kelvin(kwargs[ATTR_COLOR_TEMP])
self._device.color_temperature = color_temp
if (
ATTR_COLOR_TEMP_KELVIN in kwargs
and self.color_temp_kelvin != kwargs[ATTR_COLOR_TEMP_KELVIN]
):
self._device.color_temperature = kwargs[ATTR_COLOR_TEMP_KELVIN]
def turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off."""
@ -136,7 +120,7 @@ class OppleLight(LightEntity):
prev_available == self.available
and self._is_on == self._device.power_on
and self._brightness == self._device.brightness
and self._color_temp == self._device.color_temperature
and self._attr_color_temp_kelvin == self._device.color_temperature
):
return
@ -146,7 +130,7 @@ class OppleLight(LightEntity):
self._is_on = self._device.power_on
self._brightness = self._device.brightness
self._color_temp = self._device.color_temperature
self._attr_color_temp_kelvin = self._device.color_temperature
if not self.is_on:
_LOGGER.debug("Update light %s success: power off", self._device.ip)
@ -155,5 +139,5 @@ class OppleLight(LightEntity):
"Update light %s success: power on brightness %s color temperature %s",
self._device.ip,
self._brightness,
self._color_temp,
self._attr_color_temp_kelvin,
)