mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Improve mqtt climate turn_on and turn_off service (#94832)
* Improve mqtt climate turn_on and turn_off service * Remove POWER_COMMAND_TOPIC when mode is changed * Call super
This commit is contained in:
@@ -355,9 +355,6 @@ async def test_set_operation_optimistic(
|
||||
assert state.state == "heat"
|
||||
|
||||
|
||||
# CONF_POWER_COMMAND_TOPIC, CONF_POWER_STATE_TOPIC and CONF_POWER_STATE_TEMPLATE are deprecated,
|
||||
# support for CONF_POWER_STATE_TOPIC and CONF_POWER_STATE_TEMPLATE was already removed or never added
|
||||
# support was deprecated with release 2023.2 and will be removed with release 2023.8
|
||||
@pytest.mark.parametrize(
|
||||
"hass_config",
|
||||
[
|
||||
@@ -377,17 +374,134 @@ async def test_set_operation_with_power_command(
|
||||
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "cool"
|
||||
mqtt_mock.async_publish.assert_has_calls(
|
||||
[call("power-command", "ON", 0, False), call("mode-topic", "cool", 0, False)]
|
||||
)
|
||||
mqtt_mock.async_publish.assert_has_calls([call("mode-topic", "cool", 0, False)])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
await common.async_set_hvac_mode(hass, "off", ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
mqtt_mock.async_publish.assert_has_calls(
|
||||
[call("power-command", "OFF", 0, False), call("mode-topic", "off", 0, False)]
|
||||
)
|
||||
mqtt_mock.async_publish.assert_has_calls([call("mode-topic", "off", 0, False)])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
await common.async_turn_on(hass, ENTITY_CLIMATE)
|
||||
# the hvac_mode is not updated optimistically as this is not supported
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
mqtt_mock.async_publish.assert_has_calls([call("power-command", "ON", 0, False)])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
await common.async_turn_off(hass, ENTITY_CLIMATE)
|
||||
mqtt_mock.async_publish.assert_has_calls([call("power-command", "OFF", 0, False)])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"hass_config",
|
||||
[
|
||||
help_custom_config(
|
||||
climate.DOMAIN,
|
||||
DEFAULT_CONFIG,
|
||||
({"power_command_topic": "power-command", "optimistic": True},),
|
||||
)
|
||||
],
|
||||
)
|
||||
async def test_turn_on_and_off_optimistic_with_power_command(
|
||||
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
|
||||
) -> None:
|
||||
"""Test setting of turn on/off with power command enabled."""
|
||||
mqtt_mock = await mqtt_mock_entry()
|
||||
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "cool"
|
||||
mqtt_mock.async_publish.assert_has_calls([call("mode-topic", "cool", 0, False)])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
await common.async_set_hvac_mode(hass, "off", ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
|
||||
await common.async_turn_on(hass, ENTITY_CLIMATE)
|
||||
# the hvac_mode is not updated optimistically as this is not supported
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
mqtt_mock.async_publish.assert_has_calls([call("power-command", "ON", 0, False)])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "cool"
|
||||
await common.async_turn_off(hass, ENTITY_CLIMATE)
|
||||
# the hvac_mode is updated optimistically
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
mqtt_mock.async_publish.assert_has_calls([call("power-command", "OFF", 0, False)])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hass_config", "climate_on", "climate_off"),
|
||||
[
|
||||
(
|
||||
help_custom_config(
|
||||
climate.DOMAIN, DEFAULT_CONFIG, ({"modes": ["heat", "cool"]},)
|
||||
),
|
||||
"heat",
|
||||
None,
|
||||
),
|
||||
(
|
||||
help_custom_config(
|
||||
climate.DOMAIN, DEFAULT_CONFIG, ({"modes": ["off", "dry"]},)
|
||||
),
|
||||
None,
|
||||
"off",
|
||||
),
|
||||
(
|
||||
help_custom_config(
|
||||
climate.DOMAIN, DEFAULT_CONFIG, ({"modes": ["off", "cool"]},)
|
||||
),
|
||||
"cool",
|
||||
"off",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_turn_on_and_off_without_power_command(
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
climate_on: str | None,
|
||||
climate_off: str | None,
|
||||
) -> None:
|
||||
"""Test setting of turn on/off with power command enabled."""
|
||||
mqtt_mock = await mqtt_mock_entry()
|
||||
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
|
||||
await common.async_turn_on(hass, ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert climate_on is None or state.state == climate_on
|
||||
if climate_on:
|
||||
mqtt_mock.async_publish.assert_has_calls(
|
||||
[call("mode-topic", climate_on, 0, False)]
|
||||
)
|
||||
else:
|
||||
mqtt_mock.async_publish.assert_has_calls([])
|
||||
|
||||
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "cool"
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
await common.async_turn_off(hass, ENTITY_CLIMATE)
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert climate_off is None or state.state == climate_off
|
||||
if climate_off:
|
||||
assert state.state == "off"
|
||||
mqtt_mock.async_publish.assert_has_calls([call("mode-topic", "off", 0, False)])
|
||||
else:
|
||||
assert state.state == "cool"
|
||||
mqtt_mock.async_publish.assert_has_calls([])
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
|
||||
@@ -1480,6 +1594,7 @@ async def test_get_with_templates(
|
||||
climate.DOMAIN: {
|
||||
"name": "test",
|
||||
"mode_command_topic": "mode-topic",
|
||||
"power_command_topic": "power-topic",
|
||||
"target_humidity_command_topic": "humidity-topic",
|
||||
"temperature_command_topic": "temperature-topic",
|
||||
"temperature_low_command_topic": "temperature-low-topic",
|
||||
@@ -1499,6 +1614,7 @@ async def test_get_with_templates(
|
||||
],
|
||||
# Create simple templates
|
||||
"fan_mode_command_template": "fan_mode: {{ value }}",
|
||||
"power_command_template": "power: {{ value }}",
|
||||
"preset_mode_command_template": "preset_mode: {{ value }}",
|
||||
"mode_command_template": "mode: {{ value }}",
|
||||
"swing_mode_command_template": "swing_mode: {{ value }}",
|
||||
@@ -1540,13 +1656,38 @@ async def test_set_and_templates(
|
||||
|
||||
# Mode
|
||||
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"mode-topic", "mode: cool", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.assert_any_call("mode-topic", "mode: cool", 0, False)
|
||||
assert mqtt_mock.async_publish.call_count == 1
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "cool"
|
||||
|
||||
await common.async_set_hvac_mode(hass, "off", ENTITY_CLIMATE)
|
||||
mqtt_mock.async_publish.assert_any_call("mode-topic", "mode: off", 0, False)
|
||||
assert mqtt_mock.async_publish.call_count == 1
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
|
||||
# Power
|
||||
await common.async_turn_on(hass, ENTITY_CLIMATE)
|
||||
mqtt_mock.async_publish.assert_any_call("power-topic", "power: ON", 0, False)
|
||||
# Only power command is sent
|
||||
# the mode is not updated when power_command_topic is set
|
||||
assert mqtt_mock.async_publish.call_count == 1
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
|
||||
await common.async_turn_off(hass, ENTITY_CLIMATE)
|
||||
mqtt_mock.async_publish.assert_any_call("power-topic", "power: OFF", 0, False)
|
||||
# Only power command is sent
|
||||
# the mode is not updated when power_command_topic is set
|
||||
assert mqtt_mock.async_publish.call_count == 1
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get(ENTITY_CLIMATE)
|
||||
assert state.state == "off"
|
||||
|
||||
# Swing Mode
|
||||
await common.async_set_swing_mode(hass, "on", ENTITY_CLIMATE)
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
@@ -2141,10 +2282,17 @@ async def test_precision_whole(
|
||||
(
|
||||
climate.SERVICE_TURN_ON,
|
||||
"power_command_topic",
|
||||
None,
|
||||
{},
|
||||
"ON",
|
||||
None,
|
||||
),
|
||||
(
|
||||
climate.SERVICE_TURN_OFF,
|
||||
"power_command_topic",
|
||||
{},
|
||||
"OFF",
|
||||
None,
|
||||
),
|
||||
(
|
||||
climate.SERVICE_SET_HVAC_MODE,
|
||||
"mode_command_topic",
|
||||
|
||||
Reference in New Issue
Block a user