2018-01-02 02:32:29 +00:00
|
|
|
"""Test MQTT fans."""
|
2020-03-09 16:40:00 +00:00
|
|
|
from homeassistant.components import fan
|
2019-04-19 03:59:41 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ASSUMED_STATE,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
)
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2020-03-09 16:40:00 +00:00
|
|
|
from .common import (
|
|
|
|
help_test_discovery_broken,
|
|
|
|
help_test_discovery_removal,
|
|
|
|
help_test_discovery_update,
|
|
|
|
help_test_discovery_update_attr,
|
|
|
|
help_test_entity_device_info_update,
|
|
|
|
help_test_entity_device_info_with_identifier,
|
|
|
|
help_test_entity_id_update,
|
|
|
|
help_test_setting_attribute_via_mqtt_json_message,
|
|
|
|
help_test_unique_id,
|
|
|
|
help_test_update_with_json_attrs_bad_JSON,
|
|
|
|
help_test_update_with_json_attrs_not_dict,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-03-09 16:40:00 +00:00
|
|
|
|
|
|
|
from tests.common import async_fire_mqtt_message
|
2019-04-19 03:59:41 +00:00
|
|
|
from tests.components.fan import common
|
2018-10-28 22:49:55 +00:00
|
|
|
|
2020-03-11 16:34:19 +00:00
|
|
|
DEFAULT_CONFIG_ATTR = {
|
|
|
|
fan.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test-topic",
|
|
|
|
"json_attributes_topic": "attr-topic",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_CONFIG_DEVICE_INFO = {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "test-command-topic",
|
|
|
|
"device": {
|
|
|
|
"identifiers": ["helloworld"],
|
|
|
|
"connections": [["mac", "02:5b:26:a8:dc:12"]],
|
|
|
|
"manufacturer": "Whatever",
|
|
|
|
"name": "Beer",
|
|
|
|
"model": "Glass",
|
|
|
|
"sw_version": "0.1-beta",
|
|
|
|
},
|
|
|
|
"unique_id": "veryunique",
|
|
|
|
}
|
|
|
|
|
2018-10-28 22:49:55 +00:00
|
|
|
|
|
|
|
async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
|
|
|
|
"""Test if command fails with command topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, fan.DOMAIN, {fan.DOMAIN: {"platform": "mqtt", "name": "test"}}
|
|
|
|
)
|
|
|
|
assert hass.states.get("fan.test") is None
|
2018-10-28 22:49:55 +00:00
|
|
|
|
|
|
|
|
2019-04-19 03:59:41 +00:00
|
|
|
async def test_controlling_state_via_topic(hass, mqtt_mock):
|
|
|
|
"""Test the controlling state via topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
fan.DOMAIN,
|
|
|
|
{
|
|
|
|
fan.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "state-topic",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"payload_off": "StAtE_OfF",
|
|
|
|
"payload_on": "StAtE_On",
|
|
|
|
"oscillation_state_topic": "oscillation-state-topic",
|
|
|
|
"oscillation_command_topic": "oscillation-command-topic",
|
|
|
|
"payload_oscillation_off": "OsC_OfF",
|
|
|
|
"payload_oscillation_on": "OsC_On",
|
|
|
|
"speed_state_topic": "speed-state-topic",
|
|
|
|
"speed_command_topic": "speed-command-topic",
|
|
|
|
"payload_off_speed": "speed_OfF",
|
|
|
|
"payload_low_speed": "speed_lOw",
|
|
|
|
"payload_medium_speed": "speed_mEdium",
|
|
|
|
"payload_high_speed": "speed_High",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "state-topic", "StAtE_On")
|
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_ON
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "state-topic", "StAtE_OfF")
|
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("oscillating") is False
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "oscillation-state-topic", "OsC_On")
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("oscillating") is True
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "oscillation-state-topic", "OsC_OfF")
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("oscillating") is False
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("speed") == fan.SPEED_OFF
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", "speed_lOw")
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_LOW
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", "speed_mEdium")
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_MEDIUM
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", "speed_High")
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_HIGH
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", "speed_OfF")
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_OFF
|
2019-04-19 03:59:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
|
|
|
|
"""Test the controlling state via topic and JSON message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
fan.DOMAIN,
|
|
|
|
{
|
|
|
|
fan.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "state-topic",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"oscillation_state_topic": "oscillation-state-topic",
|
|
|
|
"oscillation_command_topic": "oscillation-command-topic",
|
|
|
|
"speed_state_topic": "speed-state-topic",
|
|
|
|
"speed_command_topic": "speed-command-topic",
|
|
|
|
"state_value_template": "{{ value_json.val }}",
|
|
|
|
"oscillation_value_template": "{{ value_json.val }}",
|
|
|
|
"speed_value_template": "{{ value_json.val }}",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "state-topic", '{"val":"ON"}')
|
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_ON
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "state-topic", '{"val":"OFF"}')
|
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("oscillating") is False
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "oscillation-state-topic", '{"val":"oscillate_on"}')
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("oscillating") is True
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "oscillation-state-topic", '{"val":"oscillate_off"}')
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("oscillating") is False
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("speed") == fan.SPEED_OFF
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", '{"val":"low"}')
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_LOW
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", '{"val":"medium"}')
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_MEDIUM
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", '{"val":"high"}')
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_HIGH
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "speed-state-topic", '{"val":"off"}')
|
|
|
|
state = hass.states.get("fan.test")
|
|
|
|
assert state.attributes.get("speed") == fan.SPEED_OFF
|
2019-04-19 03:59:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
|
|
|
|
"""Test optimistic mode without state topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
fan.DOMAIN,
|
|
|
|
{
|
|
|
|
fan.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"payload_off": "StAtE_OfF",
|
|
|
|
"payload_on": "StAtE_On",
|
|
|
|
"oscillation_command_topic": "oscillation-command-topic",
|
2020-01-06 16:10:51 +00:00
|
|
|
"oscillation_state_topic": "oscillation-state-topic",
|
2019-07-31 19:25:30 +00:00
|
|
|
"payload_oscillation_off": "OsC_OfF",
|
|
|
|
"payload_oscillation_on": "OsC_On",
|
|
|
|
"speed_command_topic": "speed-command-topic",
|
2020-01-06 16:10:51 +00:00
|
|
|
"speed_state_topic": "speed-state-topic",
|
2019-07-31 19:25:30 +00:00
|
|
|
"payload_off_speed": "speed_OfF",
|
|
|
|
"payload_low_speed": "speed_lOw",
|
|
|
|
"payload_medium_speed": "speed_mEdium",
|
|
|
|
"payload_high_speed": "speed_High",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_turn_on(hass, "fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"command-topic", "StAtE_On", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_ON
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_turn_off(hass, "fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"command-topic", "StAtE_OfF", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_oscillate(hass, "fan.test", True)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"oscillation-command-topic", "OsC_On", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_oscillate(hass, "fan.test", False)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"oscillation-command-topic", "OsC_OfF", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_LOW)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "speed_lOw", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_MEDIUM)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "speed_mEdium", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_HIGH)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "speed_High", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_OFF)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "speed_OfF", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_sending_mqtt_commands_and_explicit_optimistic(hass, mqtt_mock):
|
|
|
|
"""Test optimistic mode with state topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
fan.DOMAIN,
|
|
|
|
{
|
|
|
|
fan.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "state-topic",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"oscillation_state_topic": "oscillation-state-topic",
|
|
|
|
"oscillation_command_topic": "oscillation-command-topic",
|
|
|
|
"speed_state_topic": "speed-state-topic",
|
|
|
|
"speed_command_topic": "speed-command-topic",
|
|
|
|
"optimistic": True,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_turn_on(hass, "fan.test")
|
|
|
|
mqtt_mock.async_publish.assert_called_once_with("command-topic", "ON", 0, False)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_ON
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_turn_off(hass, "fan.test")
|
|
|
|
mqtt_mock.async_publish.assert_called_once_with("command-topic", "OFF", 0, False)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_oscillate(hass, "fan.test", True)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"oscillation-command-topic", "oscillate_on", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_oscillate(hass, "fan.test", False)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"oscillation-command-topic", "oscillate_off", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_LOW)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "low", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_MEDIUM)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "medium", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_HIGH)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "high", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_speed(hass, "fan.test", fan.SPEED_OFF)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"speed-command-topic", "off", 0, False
|
|
|
|
)
|
2019-04-19 03:59:41 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2019-04-19 03:59:41 +00:00
|
|
|
assert state.state is STATE_OFF
|
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
|
|
|
|
2018-10-28 22:49:55 +00:00
|
|
|
async def test_default_availability_payload(hass, mqtt_mock):
|
|
|
|
"""Test the availability payload."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
fan.DOMAIN,
|
|
|
|
{
|
|
|
|
fan.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "state-topic",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"availability_topic": "availability_topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-28 22:49:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is STATE_UNAVAILABLE
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability_topic", "online")
|
2018-01-20 16:11:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is not STATE_UNAVAILABLE
|
|
|
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
2018-01-20 16:11:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability_topic", "offline")
|
2018-01-20 16:11:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is STATE_UNAVAILABLE
|
2018-01-20 16:11:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "state-topic", "1")
|
2018-01-20 16:11:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is STATE_UNAVAILABLE
|
2018-01-20 16:11:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability_topic", "online")
|
2018-01-20 16:11:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is not STATE_UNAVAILABLE
|
2018-01-20 16:11:04 +00:00
|
|
|
|
|
|
|
|
2018-10-28 22:49:55 +00:00
|
|
|
async def test_custom_availability_payload(hass, mqtt_mock):
|
|
|
|
"""Test the availability payload."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
fan.DOMAIN,
|
|
|
|
{
|
|
|
|
fan.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "state-topic",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"availability_topic": "availability_topic",
|
|
|
|
"payload_available": "good",
|
|
|
|
"payload_not_available": "nogood",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is STATE_UNAVAILABLE
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability_topic", "good")
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is not STATE_UNAVAILABLE
|
|
|
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability_topic", "nogood")
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is STATE_UNAVAILABLE
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "state-topic", "1")
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is STATE_UNAVAILABLE
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability_topic", "good")
|
2018-01-02 02:32:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("fan.test")
|
2018-10-28 22:49:55 +00:00
|
|
|
assert state.state is not STATE_UNAVAILABLE
|
2018-09-25 17:32:31 +00:00
|
|
|
|
|
|
|
|
2019-01-21 00:42:17 +00:00
|
|
|
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_setting_attribute_via_mqtt_json_message(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG_ATTR
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-21 00:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_update_with_json_attrs_not_dict(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, caplog, fan.DOMAIN, DEFAULT_CONFIG_ATTR
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-21 00:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_update_with_json_attrs_bad_JSON(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, caplog, fan.DOMAIN, DEFAULT_CONFIG_ATTR
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-21 00:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered MQTTAttributes."""
|
|
|
|
data1 = (
|
2020-03-11 16:34:19 +00:00
|
|
|
'{ "name": "test",'
|
2019-01-21 00:42:17 +00:00
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "json_attributes_topic": "attr-topic1" }'
|
|
|
|
)
|
|
|
|
data2 = (
|
2020-03-11 16:34:19 +00:00
|
|
|
'{ "name": "test",'
|
2019-01-21 00:42:17 +00:00
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "json_attributes_topic": "attr-topic2" }'
|
|
|
|
)
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_update_attr(
|
|
|
|
hass, mqtt_mock, caplog, fan.DOMAIN, data1, data2
|
|
|
|
)
|
2019-01-21 00:42:17 +00:00
|
|
|
|
|
|
|
|
2020-03-09 16:40:00 +00:00
|
|
|
async def test_unique_id(hass):
|
|
|
|
"""Test unique_id option only creates one fan per id."""
|
|
|
|
config = {
|
|
|
|
fan.DOMAIN: [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "test_topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 2",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "test_topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
await help_test_unique_id(hass, fan.DOMAIN, config)
|
2019-01-21 00:42:17 +00:00
|
|
|
|
|
|
|
|
2020-03-09 16:40:00 +00:00
|
|
|
async def test_discovery_removal_fan(hass, mqtt_mock, caplog):
|
|
|
|
"""Test removal of discovered fan."""
|
2020-03-11 16:34:19 +00:00
|
|
|
data = '{ "name": "test",' ' "command_topic": "test_topic" }'
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_removal(hass, mqtt_mock, caplog, fan.DOMAIN, data)
|
2019-01-21 00:42:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-03-09 16:40:00 +00:00
|
|
|
async def test_discovery_update_fan(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered fan."""
|
|
|
|
data1 = '{ "name": "Beer",' ' "command_topic": "test_topic" }'
|
|
|
|
data2 = '{ "name": "Milk",' ' "command_topic": "test_topic" }'
|
|
|
|
await help_test_discovery_update(hass, mqtt_mock, caplog, fan.DOMAIN, data1, data2)
|
|
|
|
|
2018-09-29 19:22:57 +00:00
|
|
|
|
2020-03-09 16:40:00 +00:00
|
|
|
async def test_discovery_broken(hass, mqtt_mock, caplog):
|
|
|
|
"""Test handling of bad discovery message."""
|
|
|
|
data1 = '{ "name": "Beer" }'
|
|
|
|
data2 = '{ "name": "Milk",' ' "command_topic": "test_topic" }'
|
|
|
|
await help_test_discovery_broken(hass, mqtt_mock, caplog, fan.DOMAIN, data1, data2)
|
2018-10-09 08:13:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
|
|
|
"""Test MQTT fan device registry integration."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_entity_device_info_with_identifier(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
|
2020-03-09 16:40:00 +00:00
|
|
|
)
|
2018-12-19 13:05:24 +00:00
|
|
|
|
|
|
|
|
2019-01-26 18:46:41 +00:00
|
|
|
async def test_entity_device_info_update(hass, mqtt_mock):
|
|
|
|
"""Test device registry update."""
|
2020-03-11 16:34:19 +00:00
|
|
|
await help_test_entity_device_info_update(
|
|
|
|
hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
|
|
|
|
)
|
2019-01-26 18:46:41 +00:00
|
|
|
|
|
|
|
|
2018-12-19 13:05:24 +00:00
|
|
|
async def test_entity_id_update(hass, mqtt_mock):
|
|
|
|
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
2020-03-09 16:40:00 +00:00
|
|
|
config = {
|
|
|
|
fan.DOMAIN: [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "beer",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"availability_topic": "avty-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
await help_test_entity_id_update(hass, mqtt_mock, fan.DOMAIN, config)
|