From 1c20eb3263def048f4f86277bfa83c902cb89b07 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Wed, 28 Jul 2021 00:16:23 -0700 Subject: [PATCH] Skip 'None' values when restoring climate scenes (#53484) --- .../components/climate/reproduce_state.py | 4 +- .../climate/test_reproduce_state.py | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/climate/reproduce_state.py b/homeassistant/components/climate/reproduce_state.py index 767a38b2e57..f7e63f475ea 100644 --- a/homeassistant/components/climate/reproduce_state.py +++ b/homeassistant/components/climate/reproduce_state.py @@ -41,8 +41,8 @@ async def _async_reproduce_states( data = data or {} data["entity_id"] = state.entity_id for key in keys: - if key in state.attributes: - data[key] = state.attributes[key] + if (value := state.attributes.get(key)) is not None: + data[key] = value await hass.services.async_call( DOMAIN, service, data, blocking=True, context=context diff --git a/tests/components/climate/test_reproduce_state.py b/tests/components/climate/test_reproduce_state.py index bdcf0441b9f..af1b14299ae 100644 --- a/tests/components/climate/test_reproduce_state.py +++ b/tests/components/climate/test_reproduce_state.py @@ -117,3 +117,57 @@ async def test_attribute(hass, service, attribute): assert len(calls_1) == 1 assert calls_1[0].data == {"entity_id": ENTITY_1, attribute: value} + + +async def test_attribute_partial_temperature(hass): + """Test that service call ignores null attributes.""" + calls_1 = async_mock_service(hass, DOMAIN, SERVICE_SET_TEMPERATURE) + + await async_reproduce_states( + hass, + [ + State( + ENTITY_1, + None, + { + ATTR_TEMPERATURE: 23.1, + ATTR_TARGET_TEMP_HIGH: None, + ATTR_TARGET_TEMP_LOW: None, + }, + ) + ], + ) + + await hass.async_block_till_done() + + assert len(calls_1) == 1 + assert calls_1[0].data == {"entity_id": ENTITY_1, ATTR_TEMPERATURE: 23.1} + + +async def test_attribute_partial_high_low_temperature(hass): + """Test that service call ignores null attributes.""" + calls_1 = async_mock_service(hass, DOMAIN, SERVICE_SET_TEMPERATURE) + + await async_reproduce_states( + hass, + [ + State( + ENTITY_1, + None, + { + ATTR_TEMPERATURE: None, + ATTR_TARGET_TEMP_HIGH: 30.1, + ATTR_TARGET_TEMP_LOW: 20.2, + }, + ) + ], + ) + + await hass.async_block_till_done() + + assert len(calls_1) == 1 + assert calls_1[0].data == { + "entity_id": ENTITY_1, + ATTR_TARGET_TEMP_HIGH: 30.1, + ATTR_TARGET_TEMP_LOW: 20.2, + }