Migrate alexa color_temp handlers to use Kelvin (#132995)

This commit is contained in:
epenet 2024-12-12 21:17:31 +01:00 committed by GitHub
parent b9a7307df8
commit d02bceb6f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 14 deletions

View File

@ -376,14 +376,14 @@ async def async_api_decrease_color_temp(
) -> AlexaResponse: ) -> AlexaResponse:
"""Process a decrease color temperature request.""" """Process a decrease color temperature request."""
entity = directive.entity entity = directive.entity
current = int(entity.attributes[light.ATTR_COLOR_TEMP]) current = int(entity.attributes[light.ATTR_COLOR_TEMP_KELVIN])
max_mireds = int(entity.attributes[light.ATTR_MAX_MIREDS]) min_kelvin = int(entity.attributes[light.ATTR_MIN_COLOR_TEMP_KELVIN])
value = min(max_mireds, current + 50) value = max(min_kelvin, current - 500)
await hass.services.async_call( await hass.services.async_call(
entity.domain, entity.domain,
SERVICE_TURN_ON, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity.entity_id, light.ATTR_COLOR_TEMP: value}, {ATTR_ENTITY_ID: entity.entity_id, light.ATTR_COLOR_TEMP_KELVIN: value},
blocking=False, blocking=False,
context=context, context=context,
) )
@ -400,14 +400,14 @@ async def async_api_increase_color_temp(
) -> AlexaResponse: ) -> AlexaResponse:
"""Process an increase color temperature request.""" """Process an increase color temperature request."""
entity = directive.entity entity = directive.entity
current = int(entity.attributes[light.ATTR_COLOR_TEMP]) current = int(entity.attributes[light.ATTR_COLOR_TEMP_KELVIN])
min_mireds = int(entity.attributes[light.ATTR_MIN_MIREDS]) max_kelvin = int(entity.attributes[light.ATTR_MAX_COLOR_TEMP_KELVIN])
value = max(min_mireds, current - 50) value = min(max_kelvin, current + 500)
await hass.services.async_call( await hass.services.async_call(
entity.domain, entity.domain,
SERVICE_TURN_ON, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity.entity_id, light.ATTR_COLOR_TEMP: value}, {ATTR_ENTITY_ID: entity.entity_id, light.ATTR_COLOR_TEMP_KELVIN: value},
blocking=False, blocking=False,
context=context, context=context,
) )

View File

@ -163,7 +163,7 @@ async def test_api_set_color_temperature(hass: HomeAssistant) -> None:
assert msg["header"]["name"] == "Response" assert msg["header"]["name"] == "Response"
@pytest.mark.parametrize(("result", "initial"), [(383, "333"), (500, "500")]) @pytest.mark.parametrize(("result", "initial"), [(2500, "3000"), (2000, "2000")])
async def test_api_decrease_color_temp( async def test_api_decrease_color_temp(
hass: HomeAssistant, result: int, initial: str hass: HomeAssistant, result: int, initial: str
) -> None: ) -> None:
@ -176,7 +176,11 @@ async def test_api_decrease_color_temp(
hass.states.async_set( hass.states.async_set(
"light.test", "light.test",
"off", "off",
{"friendly_name": "Test light", "color_temp": initial, "max_mireds": 500}, {
"friendly_name": "Test light",
"color_temp_kelvin": initial,
"min_color_temp_kelvin": 2000,
},
) )
call_light = async_mock_service(hass, "light", "turn_on") call_light = async_mock_service(hass, "light", "turn_on")
@ -189,11 +193,11 @@ async def test_api_decrease_color_temp(
assert len(call_light) == 1 assert len(call_light) == 1
assert call_light[0].data["entity_id"] == "light.test" assert call_light[0].data["entity_id"] == "light.test"
assert call_light[0].data["color_temp"] == result assert call_light[0].data["color_temp_kelvin"] == result
assert msg["header"]["name"] == "Response" assert msg["header"]["name"] == "Response"
@pytest.mark.parametrize(("result", "initial"), [(283, "333"), (142, "142")]) @pytest.mark.parametrize(("result", "initial"), [(3500, "3000"), (7000, "7000")])
async def test_api_increase_color_temp( async def test_api_increase_color_temp(
hass: HomeAssistant, result: int, initial: str hass: HomeAssistant, result: int, initial: str
) -> None: ) -> None:
@ -206,7 +210,11 @@ async def test_api_increase_color_temp(
hass.states.async_set( hass.states.async_set(
"light.test", "light.test",
"off", "off",
{"friendly_name": "Test light", "color_temp": initial, "min_mireds": 142}, {
"friendly_name": "Test light",
"color_temp_kelvin": initial,
"max_color_temp_kelvin": 7000,
},
) )
call_light = async_mock_service(hass, "light", "turn_on") call_light = async_mock_service(hass, "light", "turn_on")
@ -219,7 +227,7 @@ async def test_api_increase_color_temp(
assert len(call_light) == 1 assert len(call_light) == 1
assert call_light[0].data["entity_id"] == "light.test" assert call_light[0].data["entity_id"] == "light.test"
assert call_light[0].data["color_temp"] == result assert call_light[0].data["color_temp_kelvin"] == result
assert msg["header"]["name"] == "Response" assert msg["header"]["name"] == "Response"