2019-04-22 19:49:15 +00:00
|
|
|
"""The tests for the State vacuum Mqtt platform."""
|
|
|
|
from copy import deepcopy
|
2019-01-06 18:23:33 +00:00
|
|
|
import json
|
2017-09-15 13:39:20 +00:00
|
|
|
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.components import mqtt, vacuum
|
2019-04-22 19:49:15 +00:00
|
|
|
from homeassistant.components.mqtt import CONF_COMMAND_TOPIC, CONF_STATE_TOPIC
|
2019-01-06 16:05:04 +00:00
|
|
|
from homeassistant.components.mqtt.discovery import async_start
|
2019-10-18 00:04:27 +00:00
|
|
|
from homeassistant.components.mqtt.vacuum import CONF_SCHEMA, schema_state as mqttvacuum
|
|
|
|
from homeassistant.components.mqtt.vacuum.schema import services_to_strings
|
2019-04-22 19:49:15 +00:00
|
|
|
from homeassistant.components.mqtt.vacuum.schema_state import SERVICE_TO_STRING
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.components.vacuum import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BATTERY_ICON,
|
|
|
|
ATTR_BATTERY_LEVEL,
|
|
|
|
ATTR_FAN_SPEED,
|
|
|
|
ATTR_FAN_SPEED_LIST,
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_CLEAN_SPOT,
|
|
|
|
SERVICE_LOCATE,
|
|
|
|
SERVICE_PAUSE,
|
|
|
|
SERVICE_RETURN_TO_BASE,
|
|
|
|
SERVICE_START,
|
|
|
|
SERVICE_STOP,
|
|
|
|
STATE_CLEANING,
|
|
|
|
STATE_DOCKED,
|
|
|
|
)
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_PLATFORM,
|
2019-12-08 17:15:26 +00:00
|
|
|
ENTITY_MATCH_ALL,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
STATE_UNKNOWN,
|
|
|
|
)
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2017-09-15 13:39:20 +00:00
|
|
|
from tests.common import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry,
|
|
|
|
async_fire_mqtt_message,
|
|
|
|
async_mock_mqtt_component,
|
|
|
|
)
|
2018-09-26 16:02:05 +00:00
|
|
|
from tests.components.vacuum import common
|
2017-09-15 13:39:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
COMMAND_TOPIC = "vacuum/command"
|
|
|
|
SEND_COMMAND_TOPIC = "vacuum/send_command"
|
|
|
|
STATE_TOPIC = "vacuum/state"
|
2019-04-22 19:49:15 +00:00
|
|
|
|
|
|
|
DEFAULT_CONFIG = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PLATFORM: "mqtt",
|
|
|
|
CONF_SCHEMA: "state",
|
|
|
|
CONF_NAME: "mqtttest",
|
2019-04-22 19:49:15 +00:00
|
|
|
CONF_COMMAND_TOPIC: COMMAND_TOPIC,
|
|
|
|
mqttvacuum.CONF_SEND_COMMAND_TOPIC: SEND_COMMAND_TOPIC,
|
|
|
|
CONF_STATE_TOPIC: STATE_TOPIC,
|
2019-07-31 19:25:30 +00:00
|
|
|
mqttvacuum.CONF_SET_FAN_SPEED_TOPIC: "vacuum/set_fan_speed",
|
|
|
|
mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"],
|
2019-01-06 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_default_supported_features(hass, mqtt_mock):
|
2019-01-06 16:05:04 +00:00
|
|
|
"""Test that the correct supported features."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, vacuum.DOMAIN, {vacuum.DOMAIN: DEFAULT_CONFIG}
|
|
|
|
)
|
|
|
|
entity = hass.states.get("vacuum.mqtttest")
|
|
|
|
entity_features = entity.attributes.get(mqttvacuum.CONF_SUPPORTED_FEATURES, 0)
|
|
|
|
assert sorted(services_to_strings(entity_features, SERVICE_TO_STRING)) == sorted(
|
|
|
|
["start", "stop", "return_home", "battery", "status", "clean_spot"]
|
|
|
|
)
|
2019-01-06 16:05:04 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_all_commands(hass, mqtt_mock):
|
|
|
|
"""Test simple commands send to the vacuum."""
|
|
|
|
config = deepcopy(DEFAULT_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
|
|
|
|
mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING
|
|
|
|
)
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_START, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "start", 0, False)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_STOP, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "stop", 0, False)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_PAUSE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "pause", 0, False)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_LOCATE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "locate", 0, False)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_CLEAN_SPOT, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
COMMAND_TOPIC, "clean_spot", 0, False
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_RETURN_TO_BASE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
COMMAND_TOPIC, "return_to_base", 0, False
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_fan_speed(hass, "medium", "vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"vacuum/set_fan_speed", "medium", 0, False
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_send_command(hass, "44 FE 93", entity_id="vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_called_once_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
"vacuum/send_command", "44 FE 93", 0, False
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-04-14 18:09:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_send_command(
|
|
|
|
hass, "44 FE 93", {"key": "value"}, entity_id="vacuum.mqtttest"
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
assert json.loads(mqtt_mock.async_publish.mock_calls[-1][1][1]) == {
|
2019-04-15 02:07:05 +00:00
|
|
|
"command": "44 FE 93",
|
2019-07-31 19:25:30 +00:00
|
|
|
"key": "value",
|
2019-04-15 02:07:05 +00:00
|
|
|
}
|
2019-01-06 16:05:04 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_commands_without_supported_features(hass, mqtt_mock):
|
|
|
|
"""Test commands which are not supported by the vacuum."""
|
|
|
|
config = deepcopy(DEFAULT_CONFIG)
|
|
|
|
services = mqttvacuum.STRING_TO_SERVICE["status"]
|
2019-07-31 19:25:30 +00:00
|
|
|
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
|
|
|
|
services, SERVICE_TO_STRING
|
|
|
|
)
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_START, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_PAUSE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_STOP, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_RETURN_TO_BASE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_LOCATE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-04-14 03:29:01 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_CLEAN_SPOT, {"entity_id": ENTITY_MATCH_ALL}, blocking=True
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
|
|
|
mqtt_mock.async_publish.reset_mock()
|
2019-04-14 03:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_set_fan_speed(hass, "medium", "vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
|
|
|
mqtt_mock.async_publish.reset_mock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await common.async_send_command(
|
|
|
|
hass, "44 FE 93", {"key": "value"}, entity_id="vacuum.mqtttest"
|
|
|
|
)
|
2019-04-22 19:49:15 +00:00
|
|
|
mqtt_mock.async_publish.assert_not_called()
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_status(hass, mqtt_mock):
|
2019-04-14 03:29:01 +00:00
|
|
|
"""Test status updates from the vacuum."""
|
2019-04-22 19:49:15 +00:00
|
|
|
config = deepcopy(DEFAULT_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
|
|
|
|
mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING
|
|
|
|
)
|
2019-04-14 03:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
message = """{
|
2019-04-22 19:49:15 +00:00
|
|
|
"battery_level": 54,
|
|
|
|
"state": "cleaning",
|
|
|
|
"fan_speed": "max"
|
2019-04-14 03:29:01 +00:00
|
|
|
}"""
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "vacuum/state", message)
|
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_CLEANING
|
|
|
|
assert state.attributes.get(ATTR_BATTERY_LEVEL) == 54
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-50"
|
|
|
|
assert state.attributes.get(ATTR_FAN_SPEED) == "max"
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
message = """{
|
2019-04-22 19:49:15 +00:00
|
|
|
"battery_level": 61,
|
|
|
|
"state": "docked",
|
|
|
|
"fan_speed": "min"
|
2019-04-14 03:29:01 +00:00
|
|
|
}"""
|
2019-04-22 19:49:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "vacuum/state", message)
|
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_DOCKED
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-charging-60"
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_LEVEL) == 61
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get(ATTR_FAN_SPEED) == "min"
|
|
|
|
assert state.attributes.get(ATTR_FAN_SPEED_LIST) == ["min", "medium", "high", "max"]
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_no_fan_vacuum(hass, mqtt_mock):
|
|
|
|
"""Test status updates from the vacuum when fan is not supported."""
|
|
|
|
config = deepcopy(DEFAULT_CONFIG)
|
|
|
|
del config[mqttvacuum.CONF_FAN_SPEED_LIST]
|
2019-07-31 19:25:30 +00:00
|
|
|
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
|
|
|
|
mqttvacuum.DEFAULT_SERVICES, SERVICE_TO_STRING
|
|
|
|
)
|
2019-04-14 03:29:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
message = """{
|
2019-04-22 19:49:15 +00:00
|
|
|
"battery_level": 54,
|
|
|
|
"state": "cleaning"
|
2019-04-14 03:29:01 +00:00
|
|
|
}"""
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "vacuum/state", message)
|
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_CLEANING
|
|
|
|
assert state.attributes.get(ATTR_FAN_SPEED) is None
|
|
|
|
assert state.attributes.get(ATTR_FAN_SPEED_LIST) is None
|
|
|
|
assert state.attributes.get(ATTR_BATTERY_LEVEL) == 54
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-50"
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
message = """{
|
2019-04-22 19:49:15 +00:00
|
|
|
"battery_level": 54,
|
|
|
|
"state": "cleaning",
|
2019-04-14 03:29:01 +00:00
|
|
|
"fan_speed": "max"
|
|
|
|
}"""
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "vacuum/state", message)
|
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-14 03:29:01 +00:00
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_CLEANING
|
|
|
|
assert state.attributes.get(ATTR_FAN_SPEED) is None
|
|
|
|
assert state.attributes.get(ATTR_FAN_SPEED_LIST) is None
|
2019-04-14 03:29:01 +00:00
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_LEVEL) == 54
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-50"
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
message = """{
|
2019-04-22 19:49:15 +00:00
|
|
|
"battery_level": 61,
|
|
|
|
"state": "docked"
|
2019-04-14 03:29:01 +00:00
|
|
|
}"""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "vacuum/state", message)
|
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_DOCKED
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-charging-60"
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.attributes.get(ATTR_BATTERY_LEVEL) == 61
|
2019-01-06 16:05:04 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_status_invalid_json(hass, mqtt_mock):
|
2019-01-06 16:05:04 +00:00
|
|
|
"""Test to make sure nothing breaks if the vacuum sends bad JSON."""
|
2019-04-22 19:49:15 +00:00
|
|
|
config = deepcopy(DEFAULT_CONFIG)
|
2019-10-18 00:04:27 +00:00
|
|
|
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
|
2019-07-31 19:25:30 +00:00
|
|
|
mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING
|
|
|
|
)
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "vacuum/state", '{"asdfasas false}')
|
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_UNKNOWN
|
2019-04-14 03:29:01 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_default_availability_payload(hass, mqtt_mock):
|
2019-01-06 16:05:04 +00:00
|
|
|
"""Test availability by default payload with defined topic."""
|
2019-04-22 19:49:15 +00:00
|
|
|
config = deepcopy(DEFAULT_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
config.update({"availability_topic": "availability-topic"})
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "online")
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-01-06 16:05:04 +00:00
|
|
|
assert STATE_UNAVAILABLE != state.state
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "offline")
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2019-01-06 16:05:04 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_custom_availability_payload(hass, mqtt_mock):
|
2019-01-06 16:05:04 +00:00
|
|
|
"""Test availability by custom payload with defined topic."""
|
2019-04-22 19:49:15 +00:00
|
|
|
config = deepcopy(DEFAULT_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
config.update(
|
|
|
|
{
|
|
|
|
"availability_topic": "availability-topic",
|
|
|
|
"payload_available": "good",
|
|
|
|
"payload_not_available": "nogood",
|
|
|
|
}
|
|
|
|
)
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "good")
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state != STATE_UNAVAILABLE
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "nogood")
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.mqtttest")
|
2019-04-22 19:49:15 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2019-01-06 16:05:04 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_discovery_removal_vacuum(hass, mqtt_mock):
|
2019-01-06 16:05:04 +00:00
|
|
|
"""Test removal of discovered vacuum."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = '{ "name": "Beer",' ' "command_topic": "test_topic"}'
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data)
|
2019-01-06 16:05:04 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.beer")
|
2019-01-06 16:05:04 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Beer"
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", "")
|
2019-01-06 16:05:04 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.beer")
|
2019-01-06 16:05:04 +00:00
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
2019-01-28 23:56:47 +00:00
|
|
|
async def test_discovery_broken(hass, mqtt_mock, caplog):
|
|
|
|
"""Test handling of bad discovery message."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-28 23:56:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data1 = '{ "name": "Beer",' ' "command_topic": "test_topic#"}'
|
|
|
|
data2 = '{ "name": "Milk",' ' "command_topic": "test_topic"}'
|
2019-01-28 23:56:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data1)
|
2019-01-28 23:56:47 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.beer")
|
2019-01-28 23:56:47 +00:00
|
|
|
assert state is None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data2)
|
2019-01-28 23:56:47 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.milk")
|
2019-01-28 23:56:47 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Milk"
|
|
|
|
state = hass.states.get("vacuum.beer")
|
2019-01-28 23:56:47 +00:00
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_discovery_update_vacuum(hass, mqtt_mock):
|
2019-01-06 16:05:04 +00:00
|
|
|
"""Test update of discovered vacuum."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data1 = '{ "name": "Beer",' ' "command_topic": "test_topic"}'
|
|
|
|
data2 = '{ "name": "Milk",' ' "command_topic": "test_topic"}'
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data1)
|
2019-01-06 16:05:04 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.beer")
|
2019-01-06 16:05:04 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Beer"
|
2019-01-06 16:05:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data2)
|
2019-01-06 16:05:04 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.beer")
|
2019-01-06 16:05:04 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Milk"
|
|
|
|
state = hass.states.get("vacuum.milk")
|
2019-01-06 16:05:04 +00:00
|
|
|
assert state is None
|
2019-01-06 18:23:33 +00:00
|
|
|
|
|
|
|
|
2019-01-19 10:58:21 +00:00
|
|
|
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
vacuum.DOMAIN,
|
|
|
|
{
|
|
|
|
vacuum.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"json_attributes_topic": "attr-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-01-19 10:58:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "attr-topic", '{ "val": "100" }')
|
|
|
|
state = hass.states.get("vacuum.test")
|
2019-01-19 10:58:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("val") == "100"
|
2019-01-19 10:58:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
vacuum.DOMAIN,
|
|
|
|
{
|
|
|
|
vacuum.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"json_attributes_topic": "attr-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-01-19 10:58:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "attr-topic", '[ "list", "of", "things"]')
|
|
|
|
state = hass.states.get("vacuum.test")
|
2019-01-19 10:58:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("val") is None
|
|
|
|
assert "JSON result was not a dictionary" in caplog.text
|
2019-01-19 10:58:21 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_update_with_json_attrs_bad_json(hass, mqtt_mock, caplog):
|
2019-01-19 10:58:21 +00:00
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
vacuum.DOMAIN,
|
|
|
|
{
|
|
|
|
vacuum.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"json_attributes_topic": "attr-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-01-19 10:58:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "attr-topic", "This is not JSON")
|
2019-01-19 10:58:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("vacuum.test")
|
|
|
|
assert state.attributes.get("val") is None
|
|
|
|
assert "Erroneous JSON: This is not JSON" in caplog.text
|
2019-01-19 10:58:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered MQTTAttributes."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-19 10:58:21 +00:00
|
|
|
data1 = (
|
|
|
|
'{ "name": "Beer",'
|
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "json_attributes_topic": "attr-topic1" }'
|
|
|
|
)
|
|
|
|
data2 = (
|
|
|
|
'{ "name": "Beer",'
|
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "json_attributes_topic": "attr-topic2" }'
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data1)
|
2019-01-19 10:58:21 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "attr-topic1", '{ "val": "100" }')
|
|
|
|
state = hass.states.get("vacuum.beer")
|
|
|
|
assert state.attributes.get("val") == "100"
|
2019-01-19 10:58:21 +00:00
|
|
|
|
|
|
|
# Change json_attributes_topic
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data2)
|
2019-01-19 10:58:21 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Verify we are no longer subscribing to the old topic
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "attr-topic1", '{ "val": "50" }')
|
|
|
|
state = hass.states.get("vacuum.beer")
|
|
|
|
assert state.attributes.get("val") == "100"
|
2019-01-19 10:58:21 +00:00
|
|
|
|
|
|
|
# Verify we are subscribing to the new topic
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "attr-topic2", '{ "val": "75" }')
|
|
|
|
state = hass.states.get("vacuum.beer")
|
|
|
|
assert state.attributes.get("val") == "75"
|
2019-01-19 10:58:21 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_unique_id(hass, mqtt_mock):
|
2019-01-06 18:23:33 +00:00
|
|
|
"""Test unique id option only creates one vacuum per unique_id."""
|
|
|
|
await async_mock_mqtt_component(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
vacuum.DOMAIN,
|
|
|
|
{
|
|
|
|
vacuum.DOMAIN: [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 2",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, "test-topic", "payload")
|
2019-01-06 18:23:33 +00:00
|
|
|
|
2020-01-07 16:30:53 +00:00
|
|
|
assert len(hass.states.async_entity_ids()) == 1
|
2019-01-06 18:23:33 +00:00
|
|
|
|
|
|
|
|
2019-04-22 19:49:15 +00:00
|
|
|
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
2019-01-06 18:23:33 +00:00
|
|
|
"""Test MQTT vacuum device registry integration."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
|
|
|
entry.add_to_hass(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-06 18:23:33 +00:00
|
|
|
registry = await hass.helpers.device_registry.async_get_registry()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = json.dumps(
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"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",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data)
|
2019-01-06 18:23:33 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device = registry.async_get_device({("mqtt", "helloworld")}, set())
|
2019-01-06 18:23:33 +00:00
|
|
|
assert device is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert device.identifiers == {("mqtt", "helloworld")}
|
|
|
|
assert device.connections == {("mac", "02:5b:26:a8:dc:12")}
|
|
|
|
assert device.manufacturer == "Whatever"
|
|
|
|
assert device.name == "Beer"
|
|
|
|
assert device.model == "Glass"
|
|
|
|
assert device.sw_version == "0.1-beta"
|
2019-01-27 16:43:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_device_info_update(hass, mqtt_mock):
|
|
|
|
"""Test device registry update."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
|
|
|
entry.add_to_hass(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-27 16:43:16 +00:00
|
|
|
registry = await hass.helpers.device_registry.async_get_registry()
|
|
|
|
|
|
|
|
config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"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",
|
2019-01-27 16:43:16 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"unique_id": "veryunique",
|
2019-01-27 16:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data = json.dumps(config)
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data)
|
2019-01-27 16:43:16 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device = registry.async_get_device({("mqtt", "helloworld")}, set())
|
2019-01-27 16:43:16 +00:00
|
|
|
assert device is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert device.name == "Beer"
|
2019-01-27 16:43:16 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config["device"]["name"] = "Milk"
|
2019-01-27 16:43:16 +00:00
|
|
|
data = json.dumps(config)
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/vacuum/bla/config", data)
|
2019-01-27 16:43:16 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device = registry.async_get_device({("mqtt", "helloworld")}, set())
|
2019-01-27 16:43:16 +00:00
|
|
|
assert device is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert device.name == "Milk"
|