Adapt homekit to color temperatures in K (#79713)

This commit is contained in:
Erik Montnemery 2022-10-06 21:20:10 +02:00 committed by GitHub
parent aa5575ba65
commit 51e6d49451
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 27 deletions

View File

@ -2,7 +2,6 @@
from __future__ import annotations
import logging
import math
from pyhap.const import CATEGORY_LIGHTBULB
@ -10,10 +9,10 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_MODE,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR,
ATTR_MAX_MIREDS,
ATTR_MIN_MIREDS,
ATTR_MAX_COLOR_TEMP_KELVIN,
ATTR_MIN_COLOR_TEMP_KELVIN,
ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR,
ATTR_SUPPORTED_COLOR_MODES,
@ -33,6 +32,7 @@ from homeassistant.const import (
from homeassistant.core import callback
from homeassistant.helpers.event import async_call_later
from homeassistant.util.color import (
color_temperature_kelvin_to_mired,
color_temperature_mired_to_kelvin,
color_temperature_to_hs,
color_temperature_to_rgbww,
@ -55,8 +55,8 @@ _LOGGER = logging.getLogger(__name__)
CHANGE_COALESCE_TIME_WINDOW = 0.01
DEFAULT_MIN_MIREDS = 153
DEFAULT_MAX_MIREDS = 500
DEFAULT_MIN_COLOR_TEMP = 2000 # 500 mireds
DEFAULT_MAX_COLOR_TEMP = 6500 # 153 mireds
COLOR_MODES_WITH_WHITES = {ColorMode.RGBW, ColorMode.RGBWW, ColorMode.WHITE}
@ -110,11 +110,11 @@ class Light(HomeAccessory):
self.char_brightness = serv_light.configure_char(CHAR_BRIGHTNESS, value=100)
if CHAR_COLOR_TEMPERATURE in self.chars:
self.min_mireds = math.floor(
attributes.get(ATTR_MIN_MIREDS, DEFAULT_MIN_MIREDS)
self.min_mireds = color_temperature_kelvin_to_mired(
attributes.get(ATTR_MAX_COLOR_TEMP_KELVIN, DEFAULT_MAX_COLOR_TEMP)
)
self.max_mireds = math.ceil(
attributes.get(ATTR_MAX_MIREDS, DEFAULT_MAX_MIREDS)
self.max_mireds = color_temperature_kelvin_to_mired(
attributes.get(ATTR_MIN_COLOR_TEMP_KELVIN, DEFAULT_MIN_COLOR_TEMP)
)
if not self.color_temp_supported and not self.rgbww_supported:
self.max_mireds = self.min_mireds
@ -190,7 +190,7 @@ class Light(HomeAccessory):
((brightness_pct or self.char_brightness.value) * 255) / 100
)
if self.color_temp_supported:
params[ATTR_COLOR_TEMP] = temp
params[ATTR_COLOR_TEMP_KELVIN] = color_temperature_mired_to_kelvin(temp)
elif self.rgbww_supported:
params[ATTR_RGBWW_COLOR] = color_temperature_to_rgbww(
color_temperature_mired_to_kelvin(temp),
@ -261,10 +261,8 @@ class Light(HomeAccessory):
# Handle Color - color must always be set before color temperature
# or the iOS UI will not display it correctly.
if self.color_supported:
if color_temp := attributes.get(ATTR_COLOR_TEMP):
hue, saturation = color_temperature_to_hs(
color_temperature_mired_to_kelvin(color_temp)
)
if color_temp := attributes.get(ATTR_COLOR_TEMP_KELVIN):
hue, saturation = color_temperature_to_hs(color_temp)
elif color_mode == ColorMode.WHITE:
hue, saturation = 0, 0
else:
@ -281,7 +279,9 @@ class Light(HomeAccessory):
if CHAR_COLOR_TEMPERATURE in self.chars:
color_temp = None
if self.color_temp_supported:
color_temp = attributes.get(ATTR_COLOR_TEMP)
color_temp_kelvin = attributes.get(ATTR_COLOR_TEMP_KELVIN)
if color_temp_kelvin is not None:
color_temp = color_temperature_kelvin_to_mired(color_temp_kelvin)
elif color_mode == ColorMode.WHITE:
color_temp = self.min_mireds
if isinstance(color_temp, (int, float)):

View File

@ -18,7 +18,7 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_MODE,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR,
ATTR_MAX_MIREDS,
ATTR_MIN_MIREDS,
@ -250,7 +250,7 @@ async def test_light_color_temperature(hass, hk_driver, events):
hass.states.async_set(
entity_id,
STATE_ON,
{ATTR_SUPPORTED_COLOR_MODES: ["color_temp"], ATTR_COLOR_TEMP: 190},
{ATTR_SUPPORTED_COLOR_MODES: ["color_temp"], ATTR_COLOR_TEMP_KELVIN: 5263},
)
await hass.async_block_till_done()
acc = Light(hass, hk_driver, "Light", entity_id, 1, None)
@ -282,7 +282,7 @@ async def test_light_color_temperature(hass, hk_driver, events):
await _wait_for_light_coalesce(hass)
assert call_turn_on
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[0].data[ATTR_COLOR_TEMP] == 250
assert call_turn_on[0].data[ATTR_COLOR_TEMP_KELVIN] == 4000
assert len(events) == 1
assert events[-1].data[ATTR_VALUE] == "color temperature at 250"
@ -302,7 +302,7 @@ async def test_light_color_temperature_and_rgb_color(
STATE_ON,
{
ATTR_SUPPORTED_COLOR_MODES: supported_color_modes,
ATTR_COLOR_TEMP: 190,
ATTR_COLOR_TEMP_KELVIN: 5263,
ATTR_HS_COLOR: (260, 90),
},
)
@ -316,7 +316,7 @@ async def test_light_color_temperature_and_rgb_color(
assert hasattr(acc, "char_color_temp")
hass.states.async_set(entity_id, STATE_ON, {ATTR_COLOR_TEMP: 224})
hass.states.async_set(entity_id, STATE_ON, {ATTR_COLOR_TEMP_KELVIN: 4464})
await hass.async_block_till_done()
await acc.run()
await hass.async_block_till_done()
@ -324,7 +324,7 @@ async def test_light_color_temperature_and_rgb_color(
assert acc.char_hue.value == 27
assert acc.char_saturation.value == 27
hass.states.async_set(entity_id, STATE_ON, {ATTR_COLOR_TEMP: 352})
hass.states.async_set(entity_id, STATE_ON, {ATTR_COLOR_TEMP_KELVIN: 2840})
await hass.async_block_till_done()
await acc.run()
await hass.async_block_till_done()
@ -373,7 +373,7 @@ async def test_light_color_temperature_and_rgb_color(
assert call_turn_on[0]
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[0].data[ATTR_BRIGHTNESS_PCT] == 20
assert call_turn_on[0].data[ATTR_COLOR_TEMP] == 250
assert call_turn_on[0].data[ATTR_COLOR_TEMP_KELVIN] == 4000
assert len(events) == 1
assert (
@ -446,7 +446,7 @@ async def test_light_color_temperature_and_rgb_color(
)
await _wait_for_light_coalesce(hass)
assert call_turn_on[3]
assert call_turn_on[3].data[ATTR_COLOR_TEMP] == 320
assert call_turn_on[3].data[ATTR_COLOR_TEMP_KELVIN] == 3125
assert events[-1].data[ATTR_VALUE] == "color temperature at 320"
# Generate a conflict by setting color temp then saturation
@ -991,7 +991,7 @@ async def test_light_rgb_with_white_switch_to_temp(
await _wait_for_light_coalesce(hass)
assert call_turn_on
assert call_turn_on[-1].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[-1].data[ATTR_COLOR_TEMP] == 500
assert call_turn_on[-1].data[ATTR_COLOR_TEMP_KELVIN] == 2000
assert len(events) == 2
assert events[-1].data[ATTR_VALUE] == "color temperature at 500"
assert acc.char_brightness.value == 100
@ -1335,7 +1335,7 @@ async def test_light_set_brightness_and_color_temp(hass, hk_driver, events):
await hass.async_block_till_done()
assert acc.char_brightness.value == 40
hass.states.async_set(entity_id, STATE_ON, {ATTR_COLOR_TEMP: (224.14)})
hass.states.async_set(entity_id, STATE_ON, {ATTR_COLOR_TEMP_KELVIN: (4461)})
await hass.async_block_till_done()
assert acc.char_color_temp.value == 224
@ -1364,7 +1364,7 @@ async def test_light_set_brightness_and_color_temp(hass, hk_driver, events):
assert call_turn_on[0]
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[0].data[ATTR_BRIGHTNESS_PCT] == 20
assert call_turn_on[0].data[ATTR_COLOR_TEMP] == 250
assert call_turn_on[0].data[ATTR_COLOR_TEMP_KELVIN] == 4000
assert len(events) == 1
assert (