From eb14345e51c21a0ec46d238ccb6bf85c3319af4b Mon Sep 17 00:00:00 2001 From: mib1185 Date: Thu, 15 Feb 2024 22:08:10 +0000 Subject: [PATCH] don't call service with attribute None --- .../components/climate/reproduce_state.py | 15 ++++++++++++--- .../climate/test_reproduce_state.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/climate/reproduce_state.py b/homeassistant/components/climate/reproduce_state.py index 2897a956fc6907..e5fb5d6004b0e5 100644 --- a/homeassistant/components/climate/reproduce_state.py +++ b/homeassistant/components/climate/reproduce_state.py @@ -68,13 +68,22 @@ async def call_service( [ATTR_TEMPERATURE, ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW], ) - if ATTR_PRESET_MODE in state.attributes: + if ( + ATTR_PRESET_MODE in state.attributes + and state.attributes[ATTR_PRESET_MODE] is not None + ): await call_service(SERVICE_SET_PRESET_MODE, [ATTR_PRESET_MODE]) - if ATTR_SWING_MODE in state.attributes: + if ( + ATTR_SWING_MODE in state.attributes + and state.attributes[ATTR_SWING_MODE] is not None + ): await call_service(SERVICE_SET_SWING_MODE, [ATTR_SWING_MODE]) - if ATTR_FAN_MODE in state.attributes: + if ( + ATTR_FAN_MODE in state.attributes + and state.attributes[ATTR_FAN_MODE] is not None + ): await call_service(SERVICE_SET_FAN_MODE, [ATTR_FAN_MODE]) if ATTR_HUMIDITY in state.attributes: diff --git a/tests/components/climate/test_reproduce_state.py b/tests/components/climate/test_reproduce_state.py index 34cf4433029278..b8719fd8fd0c52 100644 --- a/tests/components/climate/test_reproduce_state.py +++ b/tests/components/climate/test_reproduce_state.py @@ -119,6 +119,25 @@ async def test_attribute(hass: HomeAssistant, service, attribute) -> None: assert calls_1[0].data == {"entity_id": ENTITY_1, attribute: value} +@pytest.mark.parametrize( + ("service", "attribute"), + [ + (SERVICE_SET_PRESET_MODE, ATTR_PRESET_MODE), + (SERVICE_SET_SWING_MODE, ATTR_SWING_MODE), + (SERVICE_SET_FAN_MODE, ATTR_FAN_MODE), + ], +) +async def test_attribute_with_none(hass: HomeAssistant, service, attribute) -> None: + """Test that service call is not made for attributes with None value.""" + calls_1 = async_mock_service(hass, DOMAIN, service) + + await async_reproduce_states(hass, [State(ENTITY_1, None, {attribute: None})]) + + await hass.async_block_till_done() + + assert len(calls_1) == 0 + + async def test_attribute_partial_temperature(hass: HomeAssistant) -> None: """Test that service call ignores null attributes.""" calls_1 = async_mock_service(hass, DOMAIN, SERVICE_SET_TEMPERATURE)