async def test_status_invalid_json(opp, mqtt_mock): """Test to make sure nothing breaks if the vacuum sends bad JSON.""" config = deepcopy(DEFAULT_CONFIG) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( ALL_SERVICES, SERVICE_TO_STRING ) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await opp.async_block_till_done() async_fire_mqtt_message(opp, "vacuum/state", '{"asdfasas false}') state = opp.states.get("vacuum.mqtttest") assert state.state == STATE_OFF assert state.attributes.get(ATTR_STATUS) == "Stopped"
async def test_status_fan_speed(opp, mqtt_mock): """Test status updates from the vacuum.""" config = deepcopy(DEFAULT_CONFIG) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( ALL_SERVICES, SERVICE_TO_STRING) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) message = """{ "fan_speed": "max" }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") assert state.attributes.get(ATTR_FAN_SPEED) == "max"
async def test_status_charging(opp, mqtt_mock): """Test status updates from the vacuum.""" config = deepcopy(DEFAULT_CONFIG) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( ALL_SERVICES, SERVICE_TO_STRING) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) message = """{ "charging": true }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-outline"
async def test_no_fan_vacuum(opp, mqtt_mock): """Test status updates from the vacuum when fan is not supported.""" config = deepcopy(DEFAULT_CONFIG) del config[mqttvacuum.CONF_FAN_SPEED_LIST] config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( mqttvacuum.DEFAULT_SERVICES, SERVICE_TO_STRING) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await opp.async_block_till_done() message = """{ "battery_level": 54, "state": "cleaning" }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") 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 assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-50" message = """{ "battery_level": 54, "state": "cleaning", "fan_speed": "max" }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") 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 assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-50" message = """{ "battery_level": 61, "state": "docked" }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") assert state.state == STATE_DOCKED assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-charging-60" assert state.attributes.get(ATTR_BATTERY_LEVEL) == 61
async def test_status_no_fan_speed_list(opp, mqtt_mock): """Test status updates from the vacuum. If the vacuum doesn't support fan speed, fan speed list should be None. """ config = deepcopy(DEFAULT_CONFIG) services = ALL_SERVICES - mqttvacuum.SUPPORT_FAN_SPEED config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( services, SERVICE_TO_STRING ) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await opp.async_block_till_done() state = opp.states.get("vacuum.mqtttest") assert state.attributes.get(ATTR_FAN_SPEED_LIST) is None
async def test_status_docked(opp, mqtt_mock): """Test status updates from the vacuum.""" config = deepcopy(DEFAULT_CONFIG) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( ALL_SERVICES, SERVICE_TO_STRING ) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await opp.async_block_till_done() message = """{ "docked": true }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") assert state.state == STATE_OFF
async def test_commands_without_supported_features(opp, mqtt_mock): """Test commands which are not supported by the vacuum.""" config = deepcopy(DEFAULT_CONFIG) services = mqttvacuum.STRING_TO_SERVICE["status"] config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( services, SERVICE_TO_STRING) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await common.async_turn_on(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_turn_off(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_stop(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_clean_spot(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_locate(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_start_pause(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_return_to_base(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_set_fan_speed(opp, "high", "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_send_command(opp, "44 FE 93", entity_id="vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock()
async def test_default_supported_features(opp, mqtt_mock): """Test that the correct supported features.""" assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: DEFAULT_CONFIG}) entity = opp.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([ "turn_on", "turn_off", "stop", "return_home", "battery", "status", "clean_spot", ])
async def test_battery_template(opp, mqtt_mock): """Test that you can use non-default templates for battery_level.""" config = deepcopy(DEFAULT_CONFIG) config.update({ mqttvacuum.CONF_SUPPORTED_FEATURES: services_to_strings(ALL_SERVICES, SERVICE_TO_STRING), mqttvacuum.CONF_BATTERY_LEVEL_TOPIC: "retroroomba/battery_level", mqttvacuum.CONF_BATTERY_LEVEL_TEMPLATE: "{{ value }}", }) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) async_fire_mqtt_message(opp, "retroroomba/battery_level", "54") state = opp.states.get("vacuum.mqtttest") assert state.attributes.get(ATTR_BATTERY_LEVEL) == 54 assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-50"
async def test_status(opp, mqtt_mock): """Test status updates from the vacuum.""" config = deepcopy(DEFAULT_CONFIG) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( ALL_SERVICES, SERVICE_TO_STRING ) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await opp.async_block_till_done() message = """{ "battery_level": 54, "cleaning": true, "docked": false, "charging": false, "fan_speed": "max" }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") assert state.state == STATE_ON assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-50" assert state.attributes.get(ATTR_BATTERY_LEVEL) == 54 assert state.attributes.get(ATTR_FAN_SPEED) == "max" message = """{ "battery_level": 61, "docked": true, "cleaning": false, "charging": true, "fan_speed": "min" }""" async_fire_mqtt_message(opp, "vacuum/state", message) state = opp.states.get("vacuum.mqtttest") assert state.state == STATE_OFF assert state.attributes.get(ATTR_BATTERY_ICON) == "mdi:battery-charging-60" assert state.attributes.get(ATTR_BATTERY_LEVEL) == 61 assert state.attributes.get(ATTR_FAN_SPEED) == "min"
async def test_all_commands(opp, mqtt_mock): """Test simple commands to the vacuum.""" config = deepcopy(DEFAULT_CONFIG) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( ALL_SERVICES, SERVICE_TO_STRING) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await common.async_turn_on(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/command", "turn_on", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_turn_off(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/command", "turn_off", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_stop(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/command", "stop", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_clean_spot(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/command", "clean_spot", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_locate(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/command", "locate", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_start_pause(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/command", "start_pause", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_return_to_base(opp, "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/command", "return_to_base", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_set_fan_speed(opp, "high", "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/set_fan_speed", "high", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_send_command(opp, "44 FE 93", entity_id="vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with("vacuum/send_command", "44 FE 93", 0, False) mqtt_mock.async_publish.reset_mock() await common.async_send_command(opp, "44 FE 93", {"key": "value"}, entity_id="vacuum.mqtttest") assert json.loads(mqtt_mock.async_publish.mock_calls[-1][1][1]) == { "command": "44 FE 93", "key": "value", } await common.async_send_command(opp, "44 FE 93", {"key": "value"}, entity_id="vacuum.mqtttest") assert json.loads(mqtt_mock.async_publish.mock_calls[-1][1][1]) == { "command": "44 FE 93", "key": "value", }
async def test_all_commands(opp, mqtt_mock): """Test simple commands send to the vacuum.""" config = deepcopy(DEFAULT_CONFIG) config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING ) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await opp.services.async_call( DOMAIN, SERVICE_START, {"entity_id": ENTITY_MATCH_ALL}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "start", 0, False) mqtt_mock.async_publish.reset_mock() await opp.services.async_call( DOMAIN, SERVICE_STOP, {"entity_id": ENTITY_MATCH_ALL}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "stop", 0, False) mqtt_mock.async_publish.reset_mock() await opp.services.async_call( DOMAIN, SERVICE_PAUSE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "pause", 0, False) mqtt_mock.async_publish.reset_mock() await opp.services.async_call( DOMAIN, SERVICE_LOCATE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with(COMMAND_TOPIC, "locate", 0, False) mqtt_mock.async_publish.reset_mock() await opp.services.async_call( DOMAIN, SERVICE_CLEAN_SPOT, {"entity_id": ENTITY_MATCH_ALL}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with( COMMAND_TOPIC, "clean_spot", 0, False ) mqtt_mock.async_publish.reset_mock() await opp.services.async_call( DOMAIN, SERVICE_RETURN_TO_BASE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True ) mqtt_mock.async_publish.assert_called_once_with( COMMAND_TOPIC, "return_to_base", 0, False ) mqtt_mock.async_publish.reset_mock() await common.async_set_fan_speed(opp, "medium", "vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with( "vacuum/set_fan_speed", "medium", 0, False ) mqtt_mock.async_publish.reset_mock() await common.async_send_command(opp, "44 FE 93", entity_id="vacuum.mqtttest") mqtt_mock.async_publish.assert_called_once_with( "vacuum/send_command", "44 FE 93", 0, False ) mqtt_mock.async_publish.reset_mock() await common.async_send_command( opp, "44 FE 93", {"key": "value"}, entity_id="vacuum.mqtttest" ) assert json.loads(mqtt_mock.async_publish.mock_calls[-1][1][1]) == { "command": "44 FE 93", "key": "value", }
async def test_commands_without_supported_features(opp, mqtt_mock): """Test commands which are not supported by the vacuum.""" config = deepcopy(DEFAULT_CONFIG) services = mqttvacuum.STRING_TO_SERVICE["status"] config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( services, SERVICE_TO_STRING) assert await async_setup_component(opp, vacuum.DOMAIN, {vacuum.DOMAIN: config}) await opp.async_block_till_done() await opp.services.async_call(DOMAIN, SERVICE_START, {"entity_id": ENTITY_MATCH_ALL}, blocking=True) mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await opp.services.async_call(DOMAIN, SERVICE_PAUSE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True) mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await opp.services.async_call(DOMAIN, SERVICE_STOP, {"entity_id": ENTITY_MATCH_ALL}, blocking=True) mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await opp.services.async_call(DOMAIN, SERVICE_RETURN_TO_BASE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True) mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await opp.services.async_call(DOMAIN, SERVICE_LOCATE, {"entity_id": ENTITY_MATCH_ALL}, blocking=True) mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await opp.services.async_call(DOMAIN, SERVICE_CLEAN_SPOT, {"entity_id": ENTITY_MATCH_ALL}, blocking=True) mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_set_fan_speed(opp, "medium", "vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.reset_mock() await common.async_send_command(opp, "44 FE 93", {"key": "value"}, entity_id="vacuum.mqtttest") mqtt_mock.async_publish.assert_not_called()