Skip to content

Commit d60130c

Browse files
epenetabmantis
andauthored
Blog post about Kelvin as the preferred color temperature unit (#2495)
Co-authored-by: Abílio Costa <[email protected]>
1 parent 3b2df37 commit d60130c

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
author: epenet
3+
authorURL: https://github.com/epenet
4+
title: "Use Kelvin as the preferred color temperature unit"
5+
---
6+
7+
### Summary of changes
8+
9+
In October 2022, Home Assistant migrated the preferred color temperature unit from mired to kelvin.
10+
11+
It is now time to add deprecation warnings for the corresponding attributes, constants and properties:
12+
* Deprecate state and capability attributes `ATTR_COLOR_TEMP`, `ATTR_MIN_MIREDS` and `ATTR_MAX_MIREDS`
13+
* Deprecate constants `ATTR_KELVIN` and `ATTR_COLOR_TEMP` from the `light.turn_on` service call
14+
* Deprecate properties `LightEntity.color_temp`, `LightEntity.min_mireds` and `LightEntity.max_mireds`
15+
* Deprecate corresponding attributes `LightEntity._attr_color_temp`, `LightEntity._attr_min_mired` and `LightEntity._attr_max_mired`
16+
17+
### Examples
18+
19+
#### Custom minimum/maximum color temperature
20+
21+
```python
22+
class MyLight(LightEntity):
23+
"""Representation of a light."""
24+
25+
# Old
26+
# _attr_min_mireds = 200 # 5000K
27+
# _attr_max_mireds = 400 # 2500K
28+
29+
# New
30+
_attr_min_color_temp_kelvin = 2500 # 400 mireds
31+
_attr_max_color_temp_kelvin = 5000 # 200 mireds
32+
```
33+
34+
#### Default minimum/maximum color temperature
35+
36+
```python
37+
from homeassistant.components.light import DEFAULT_MAX_KELVIN, DEFAULT_MIN_KELVIN
38+
39+
class MyLight(LightEntity):
40+
"""Representation of a light."""
41+
42+
# Old did not need to have _attr_min_mireds / _attr_max_mireds set
43+
# New needs to set the default explicitly
44+
_attr_min_color_temp_kelvin = DEFAULT_MIN_KELVIN
45+
_attr_max_color_temp_kelvin = DEFAULT_MAX_KELVIN
46+
```
47+
48+
#### Dynamic minimum/maximum color temperature
49+
50+
```python
51+
from homeassistant.util import color as color_util
52+
53+
class MyLight(LightEntity):
54+
"""Representation of a light."""
55+
56+
# Old
57+
# def min_mireds(self) -> int:
58+
# """Return the coldest color_temp that this light supports."""
59+
# return device.coldest_temperature
60+
#
61+
# def max_mireds(self) -> int:
62+
# """Return the warmest color_temp that this light supports."""
63+
# return device.warmest_temperature
64+
65+
# New
66+
def min_color_temp_kelvin(self) -> int:
67+
"""Return the warmest color_temp that this light supports."""
68+
return color_util.color_temperature_mired_to_kelvin(device.warmest_temperature)
69+
70+
def max_color_temp_kelvin(self) -> int:
71+
"""Return the coldest color_temp that this light supports."""
72+
return color_util.color_temperature_mired_to_kelvin(device.coldest_temperature)
73+
```
74+
75+
#### Service call
76+
77+
```python
78+
from homeassistant.components.light import ATTR_COLOR_TEMP_KELVIN
79+
from homeassistant.util import color as color_util
80+
81+
class MyLight(LightEntity):
82+
"""Representation of a light."""
83+
def turn_on(self, **kwargs: Any) -> None:
84+
"""Turn on the light."""
85+
# Old
86+
# if ATTR_COLOR_TEMP in kwargs:
87+
# color_temp_mired = kwargs[ATTR_COLOR_TEMP]
88+
# color_temp_kelvin = color_util.color_temperature_mired_to_kelvin(color_temp_mired)
89+
90+
# Old
91+
# if ATTR_KELVIN in kwargs:
92+
# color_temp_kelvin = kwargs[ATTR_KELVIN]
93+
# color_temp_mired = color_util.color_temperature_kelvin_to_mired(color_temp_kelvin)
94+
95+
# New
96+
if ATTR_COLOR_TEMP_KELVIN in kwargs:
97+
color_temp_kelvin = kwargs[ATTR_COLOR_TEMP_KELVIN]
98+
color_temp_mired = color_util.color_temperature_kelvin_to_mired(color_temp_kelvin)
99+
```
100+
101+
### Background information
102+
103+
* [Community discussion about Kelvin temperature](https://community.home-assistant.io/t/wth-is-light-temperature-not-in-kelvin/467449/6)
104+
* [Core PR #79591: Migration to Kelvin](https://github.com/home-assistant/core/pull/79591)
105+
* [Architecture discussion #564](https://github.com/home-assistant/architecture/discussions/564)

0 commit comments

Comments
 (0)