mirror of
https://github.com/elisspace/core.git
synced 2026-09-29 22:09:55 +00:00
Add MQTT fan direction support (#91700)
* Add MQTT fan direction support * Add MQTT fan direction abbreviations * Add MQTT fan direction tests * Shorten MQTT fan test payloads
This commit is contained in:
@@ -8,6 +8,7 @@ from voluptuous.error import MultipleInvalid
|
||||
|
||||
from homeassistant.components import fan, mqtt
|
||||
from homeassistant.components.fan import (
|
||||
ATTR_DIRECTION,
|
||||
ATTR_OSCILLATING,
|
||||
ATTR_PERCENTAGE,
|
||||
ATTR_PRESET_MODE,
|
||||
@@ -15,6 +16,8 @@ from homeassistant.components.fan import (
|
||||
NotValidPresetModeError,
|
||||
)
|
||||
from homeassistant.components.mqtt.fan import (
|
||||
CONF_DIRECTION_COMMAND_TOPIC,
|
||||
CONF_DIRECTION_STATE_TOPIC,
|
||||
CONF_OSCILLATION_COMMAND_TOPIC,
|
||||
CONF_OSCILLATION_STATE_TOPIC,
|
||||
CONF_PERCENTAGE_COMMAND_TOPIC,
|
||||
@@ -111,6 +114,8 @@ async def test_fail_setup_if_no_command_topic(
|
||||
"command_topic": "command-topic",
|
||||
"payload_off": "StAtE_OfF",
|
||||
"payload_on": "StAtE_On",
|
||||
"direction_state_topic": "direction-state-topic",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
"oscillation_state_topic": "oscillation-state-topic",
|
||||
"oscillation_command_topic": "oscillation-command-topic",
|
||||
"payload_oscillation_off": "OsC_OfF",
|
||||
@@ -157,6 +162,14 @@ async def test_controlling_state_via_topic(
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get("oscillating") is False
|
||||
|
||||
async_fire_mqtt_message(hass, "direction-state-topic", "forward")
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("direction") == "forward"
|
||||
|
||||
async_fire_mqtt_message(hass, "direction-state-topic", "reverse")
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("direction") == "reverse"
|
||||
|
||||
async_fire_mqtt_message(hass, "oscillation-state-topic", "OsC_On")
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("oscillating") is True
|
||||
@@ -357,6 +370,8 @@ async def test_controlling_state_via_topic_no_percentage_topics(
|
||||
"name": "test",
|
||||
"state_topic": "state-topic",
|
||||
"command_topic": "command-topic",
|
||||
"direction_state_topic": "direction-state-topic",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
"oscillation_state_topic": "oscillation-state-topic",
|
||||
"oscillation_command_topic": "oscillation-command-topic",
|
||||
"percentage_state_topic": "percentage-state-topic",
|
||||
@@ -372,6 +387,7 @@ async def test_controlling_state_via_topic_no_percentage_topics(
|
||||
"silent",
|
||||
],
|
||||
"state_value_template": "{{ value_json.val }}",
|
||||
"direction_value_template": "{{ value_json.val }}",
|
||||
"oscillation_value_template": "{{ value_json.val }}",
|
||||
"percentage_value_template": "{{ value_json.val }}",
|
||||
"preset_mode_value_template": "{{ value_json.val }}",
|
||||
@@ -407,6 +423,14 @@ async def test_controlling_state_via_topic_and_json_message(
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get("oscillating") is False
|
||||
|
||||
async_fire_mqtt_message(hass, "direction-state-topic", '{"val":"forward"}')
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("direction") == "forward"
|
||||
|
||||
async_fire_mqtt_message(hass, "direction-state-topic", '{"val":"reverse"}')
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("direction") == "reverse"
|
||||
|
||||
async_fire_mqtt_message(hass, "oscillation-state-topic", '{"val":"oscillate_on"}')
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("oscillating") is True
|
||||
@@ -464,6 +488,8 @@ async def test_controlling_state_via_topic_and_json_message(
|
||||
"name": "test",
|
||||
"state_topic": "shared-state-topic",
|
||||
"command_topic": "command-topic",
|
||||
"direction_state_topic": "shared-state-topic",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
"oscillation_state_topic": "shared-state-topic",
|
||||
"oscillation_command_topic": "oscillation-command-topic",
|
||||
"percentage_state_topic": "shared-state-topic",
|
||||
@@ -479,6 +505,7 @@ async def test_controlling_state_via_topic_and_json_message(
|
||||
"silent",
|
||||
],
|
||||
"state_value_template": "{{ value_json.state }}",
|
||||
"direction_value_template": "{{ value_json.direction }}",
|
||||
"oscillation_value_template": "{{ value_json.oscillation }}",
|
||||
"percentage_value_template": "{{ value_json.percentage }}",
|
||||
"preset_mode_value_template": "{{ value_json.preset_mode }}",
|
||||
@@ -499,15 +526,23 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic(
|
||||
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
assert state.attributes.get("direction") is None
|
||||
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
"shared-state-topic",
|
||||
'{"state":"ON","preset_mode":"eco","oscillation":"oscillate_on","percentage": 50}',
|
||||
"""{
|
||||
"state":"ON",
|
||||
"preset_mode":"eco",
|
||||
"oscillation":"oscillate_on",
|
||||
"percentage": 50,
|
||||
"direction": "forward"
|
||||
}""",
|
||||
)
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get("direction") == "forward"
|
||||
assert state.attributes.get("oscillating") is True
|
||||
assert state.attributes.get(fan.ATTR_PERCENTAGE) == 50
|
||||
assert state.attributes.get("preset_mode") == "eco"
|
||||
@@ -515,10 +550,17 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic(
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
"shared-state-topic",
|
||||
'{"state":"ON","preset_mode":"auto","oscillation":"oscillate_off","percentage": 10}',
|
||||
"""{
|
||||
"state":"ON",
|
||||
"preset_mode":"auto",
|
||||
"oscillation":"oscillate_off",
|
||||
"percentage": 10,
|
||||
"direction": "forward"
|
||||
}""",
|
||||
)
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get("direction") == "forward"
|
||||
assert state.attributes.get("oscillating") is False
|
||||
assert state.attributes.get(fan.ATTR_PERCENTAGE) == 10
|
||||
assert state.attributes.get("preset_mode") == "auto"
|
||||
@@ -526,10 +568,17 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic(
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
"shared-state-topic",
|
||||
'{"state":"OFF","preset_mode":"auto","oscillation":"oscillate_off","percentage": 0}',
|
||||
"""{
|
||||
"state":"OFF",
|
||||
"preset_mode":"auto",
|
||||
"oscillation":"oscillate_off",
|
||||
"percentage": 0,
|
||||
"direction": "reverse"
|
||||
}""",
|
||||
)
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get("direction") == "reverse"
|
||||
assert state.attributes.get("oscillating") is False
|
||||
assert state.attributes.get(fan.ATTR_PERCENTAGE) == 0
|
||||
assert state.attributes.get("preset_mode") == "auto"
|
||||
@@ -555,6 +604,7 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic(
|
||||
"command_topic": "command-topic",
|
||||
"payload_off": "StAtE_OfF",
|
||||
"payload_on": "StAtE_On",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
"oscillation_command_topic": "oscillation-command-topic",
|
||||
"payload_oscillation_off": "OsC_OfF",
|
||||
"payload_oscillation_on": "OsC_On",
|
||||
@@ -599,6 +649,24 @@ async def test_sending_mqtt_commands_and_optimistic(
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "forward")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"direction-command-topic", "forward", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "reverse")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"direction-command-topic", "reverse", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_oscillate(hass, "fan.test", True)
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"oscillation-command-topic", "OsC_On", 0, False
|
||||
@@ -924,6 +992,8 @@ async def test_sending_mqtt_commands_and_optimistic_no_legacy(
|
||||
"name": "test",
|
||||
"command_topic": "command-topic",
|
||||
"command_template": "state: {{ value }}",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
"direction_command_template": "direction: {{ value }}",
|
||||
"oscillation_command_topic": "oscillation-command-topic",
|
||||
"oscillation_command_template": "oscillation: {{ value }}",
|
||||
"percentage_command_topic": "percentage-command-topic",
|
||||
@@ -969,6 +1039,24 @@ async def test_sending_mqtt_command_templates_(
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "forward")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"direction-command-topic", "direction: forward", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("direction") == "forward"
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "reverse")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"direction-command-topic", "direction: reverse", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.attributes.get("direction") == "reverse"
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
with pytest.raises(MultipleInvalid):
|
||||
await common.async_set_percentage(hass, "fan.test", -1)
|
||||
|
||||
@@ -1131,6 +1219,8 @@ async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic(
|
||||
"name": "test",
|
||||
"state_topic": "state-topic",
|
||||
"command_topic": "command-topic",
|
||||
"direction_state_topic": "direction-state-topic",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
"oscillation_state_topic": "oscillation-state-topic",
|
||||
"oscillation_command_topic": "oscillation-command-topic",
|
||||
"percentage_state_topic": "percentage-state-topic",
|
||||
@@ -1250,6 +1340,15 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "forward")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"direction-command-topic", "forward", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_oscillate(hass, "fan.test", True)
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"oscillation-command-topic", "oscillate_on", 0, False
|
||||
@@ -1275,6 +1374,15 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "reverse")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"direction-command-topic", "reverse", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_oscillate(hass, "fan.test", False)
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"oscillation-command-topic", "oscillate_off", 0, False
|
||||
@@ -1368,6 +1476,12 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
|
||||
ATTR_OSCILLATING,
|
||||
True,
|
||||
),
|
||||
(
|
||||
CONF_DIRECTION_STATE_TOPIC,
|
||||
"reverse",
|
||||
ATTR_DIRECTION,
|
||||
"reverse",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_encoding_subscribable_topics(
|
||||
@@ -1383,6 +1497,7 @@ async def test_encoding_subscribable_topics(
|
||||
config[ATTR_PRESET_MODES] = ["eco", "auto"]
|
||||
config[CONF_PRESET_MODE_COMMAND_TOPIC] = "fan/some_preset_mode_command_topic"
|
||||
config[CONF_PERCENTAGE_COMMAND_TOPIC] = "fan/some_percentage_command_topic"
|
||||
config[CONF_DIRECTION_COMMAND_TOPIC] = "fan/some_direction_command_topic"
|
||||
config[CONF_OSCILLATION_COMMAND_TOPIC] = "fan/some_oscillation_command_topic"
|
||||
await help_test_encoding_subscribable_topics(
|
||||
hass,
|
||||
@@ -1404,6 +1519,7 @@ async def test_encoding_subscribable_topics(
|
||||
fan.DOMAIN: {
|
||||
"name": "test",
|
||||
"command_topic": "command-topic",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
"oscillation_command_topic": "oscillation-command-topic",
|
||||
"preset_mode_command_topic": "preset-mode-command-topic",
|
||||
"percentage_command_topic": "percentage-command-topic",
|
||||
@@ -1432,18 +1548,28 @@ async def test_attributes(
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
assert state.attributes.get(fan.ATTR_OSCILLATING) is None
|
||||
assert state.attributes.get(fan.ATTR_DIRECTION) is None
|
||||
|
||||
await common.async_turn_off(hass, "fan.test")
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
assert state.attributes.get(fan.ATTR_OSCILLATING) is None
|
||||
assert state.attributes.get(fan.ATTR_DIRECTION) is None
|
||||
|
||||
await common.async_oscillate(hass, "fan.test", True)
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
assert state.attributes.get(fan.ATTR_OSCILLATING) is True
|
||||
assert state.attributes.get(fan.ATTR_DIRECTION) is None
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "reverse")
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
assert state.attributes.get(fan.ATTR_OSCILLATING) is True
|
||||
assert state.attributes.get(fan.ATTR_DIRECTION) == "reverse"
|
||||
|
||||
await common.async_oscillate(hass, "fan.test", False)
|
||||
state = hass.states.get("fan.test")
|
||||
@@ -1451,6 +1577,13 @@ async def test_attributes(
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
assert state.attributes.get(fan.ATTR_OSCILLATING) is False
|
||||
|
||||
await common.async_set_direction(hass, "fan.test", "forward")
|
||||
state = hass.states.get("fan.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
assert state.attributes.get(fan.ATTR_OSCILLATING) is False
|
||||
assert state.attributes.get(fan.ATTR_DIRECTION) == "forward"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("name", "hass_config", "success", "features"),
|
||||
@@ -1694,6 +1827,20 @@ async def test_attributes(
|
||||
True,
|
||||
fan.FanEntityFeature.PRESET_MODE,
|
||||
),
|
||||
(
|
||||
"test17",
|
||||
{
|
||||
mqtt.DOMAIN: {
|
||||
fan.DOMAIN: {
|
||||
"name": "test17",
|
||||
"command_topic": "command-topic",
|
||||
"direction_command_topic": "direction-command-topic",
|
||||
}
|
||||
}
|
||||
},
|
||||
True,
|
||||
fan.FanEntityFeature.DIRECTION,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_supported_features(
|
||||
@@ -2027,6 +2174,13 @@ async def test_entity_debug_info_message(
|
||||
"oscillate_on",
|
||||
"oscillation_command_template",
|
||||
),
|
||||
(
|
||||
fan.SERVICE_SET_DIRECTION,
|
||||
"direction_command_topic",
|
||||
{fan.ATTR_DIRECTION: "forward"},
|
||||
"forward",
|
||||
"direction_command_template",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_publishing_with_custom_encoding(
|
||||
|
||||
Reference in New Issue
Block a user