Migrate google_assistant color_temp handlers to use Kelvin (#132997)

This commit is contained in:
epenet 2024-12-12 11:13:24 +01:00 committed by GitHub
parent 7dc31dec3b
commit 0e45ccb956
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 28 deletions

View File

@ -553,15 +553,9 @@ class ColorSettingTrait(_Trait):
response["colorModel"] = "hsv" response["colorModel"] = "hsv"
if light.color_temp_supported(color_modes): if light.color_temp_supported(color_modes):
# Max Kelvin is Min Mireds K = 1000000 / mireds
# Min Kelvin is Max Mireds K = 1000000 / mireds
response["colorTemperatureRange"] = { response["colorTemperatureRange"] = {
"temperatureMaxK": color_util.color_temperature_mired_to_kelvin( "temperatureMaxK": int(attrs.get(light.ATTR_MAX_COLOR_TEMP_KELVIN)),
attrs.get(light.ATTR_MIN_MIREDS) "temperatureMinK": int(attrs.get(light.ATTR_MIN_COLOR_TEMP_KELVIN)),
),
"temperatureMinK": color_util.color_temperature_mired_to_kelvin(
attrs.get(light.ATTR_MAX_MIREDS)
),
} }
return response return response
@ -583,7 +577,7 @@ class ColorSettingTrait(_Trait):
} }
if light.color_temp_supported([color_mode]): if light.color_temp_supported([color_mode]):
temp = self.state.attributes.get(light.ATTR_COLOR_TEMP) temp = self.state.attributes.get(light.ATTR_COLOR_TEMP_KELVIN)
# Some faulty integrations might put 0 in here, raising exception. # Some faulty integrations might put 0 in here, raising exception.
if temp == 0: if temp == 0:
_LOGGER.warning( _LOGGER.warning(
@ -592,9 +586,7 @@ class ColorSettingTrait(_Trait):
temp, temp,
) )
elif temp is not None: elif temp is not None:
color["temperatureK"] = color_util.color_temperature_mired_to_kelvin( color["temperatureK"] = temp
temp
)
response = {} response = {}
@ -606,11 +598,9 @@ class ColorSettingTrait(_Trait):
async def execute(self, command, data, params, challenge): async def execute(self, command, data, params, challenge):
"""Execute a color temperature command.""" """Execute a color temperature command."""
if "temperature" in params["color"]: if "temperature" in params["color"]:
temp = color_util.color_temperature_kelvin_to_mired( temp = params["color"]["temperature"]
params["color"]["temperature"] max_temp = self.state.attributes[light.ATTR_MAX_COLOR_TEMP_KELVIN]
) min_temp = self.state.attributes[light.ATTR_MIN_COLOR_TEMP_KELVIN]
min_temp = self.state.attributes[light.ATTR_MIN_MIREDS]
max_temp = self.state.attributes[light.ATTR_MAX_MIREDS]
if temp < min_temp or temp > max_temp: if temp < min_temp or temp > max_temp:
raise SmartHomeError( raise SmartHomeError(
@ -621,7 +611,10 @@ class ColorSettingTrait(_Trait):
await self.hass.services.async_call( await self.hass.services.async_call(
light.DOMAIN, light.DOMAIN,
SERVICE_TURN_ON, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: self.state.entity_id, light.ATTR_COLOR_TEMP: temp}, {
ATTR_ENTITY_ID: self.state.entity_id,
light.ATTR_COLOR_TEMP_KELVIN: temp,
},
blocking=not self.config.should_report_state, blocking=not self.config.should_report_state,
context=data.context, context=data.context,
) )

View File

@ -491,7 +491,7 @@ async def test_execute_request(hass_fixture, assistant_client, auth_header) -> N
assert kitchen.attributes.get(light.ATTR_RGB_COLOR) == (255, 0, 0) assert kitchen.attributes.get(light.ATTR_RGB_COLOR) == (255, 0, 0)
bed = hass_fixture.states.get("light.bed_light") bed = hass_fixture.states.get("light.bed_light")
assert bed.attributes.get(light.ATTR_COLOR_TEMP) == 212 assert bed.attributes.get(light.ATTR_COLOR_TEMP_KELVIN) == 4700
assert hass_fixture.states.get("switch.decorative_lights").state == "off" assert hass_fixture.states.get("switch.decorative_lights").state == "off"

View File

@ -1450,7 +1450,7 @@ async def test_sync_message_recovery(
"light.bad_light", "light.bad_light",
"on", "on",
{ {
"min_mireds": "badvalue", "max_color_temp_kelvin": "badvalue",
"supported_color_modes": ["color_temp"], "supported_color_modes": ["color_temp"],
}, },
) )

View File

@ -77,7 +77,7 @@ from homeassistant.const import (
) )
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, State from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, State
from homeassistant.core_config import async_process_ha_core_config from homeassistant.core_config import async_process_ha_core_config
from homeassistant.util import color, dt as dt_util from homeassistant.util import dt as dt_util
from homeassistant.util.unit_conversion import TemperatureConverter from homeassistant.util.unit_conversion import TemperatureConverter
from . import BASIC_CONFIG, MockConfig from . import BASIC_CONFIG, MockConfig
@ -870,10 +870,10 @@ async def test_color_setting_temperature_light(hass: HomeAssistant) -> None:
"light.bla", "light.bla",
STATE_ON, STATE_ON,
{ {
light.ATTR_MIN_MIREDS: 200, light.ATTR_MAX_COLOR_TEMP_KELVIN: 5000,
light.ATTR_COLOR_MODE: "color_temp", light.ATTR_COLOR_MODE: "color_temp",
light.ATTR_COLOR_TEMP: 300, light.ATTR_COLOR_TEMP_KELVIN: 3333,
light.ATTR_MAX_MIREDS: 500, light.ATTR_MIN_COLOR_TEMP_KELVIN: 2000,
"supported_color_modes": ["color_temp"], "supported_color_modes": ["color_temp"],
}, },
), ),
@ -906,7 +906,7 @@ async def test_color_setting_temperature_light(hass: HomeAssistant) -> None:
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data == { assert calls[0].data == {
ATTR_ENTITY_ID: "light.bla", ATTR_ENTITY_ID: "light.bla",
light.ATTR_COLOR_TEMP: color.color_temperature_kelvin_to_mired(2857), light.ATTR_COLOR_TEMP_KELVIN: 2857,
} }
@ -924,9 +924,9 @@ async def test_color_light_temperature_light_bad_temp(hass: HomeAssistant) -> No
"light.bla", "light.bla",
STATE_ON, STATE_ON,
{ {
light.ATTR_MIN_MIREDS: 200, light.ATTR_MAX_COLOR_TEMP_KELVIN: 5000,
light.ATTR_COLOR_TEMP: 0, light.ATTR_COLOR_TEMP_KELVIN: 0,
light.ATTR_MAX_MIREDS: 500, light.ATTR_MIN_COLOR_TEMP_KELVIN: 2000,
}, },
), ),
BASIC_CONFIG, BASIC_CONFIG,