diff --git a/tests/components/mqtt/common.py b/tests/components/mqtt/common.py index f8c57101445..f8de0faf82f 100644 --- a/tests/components/mqtt/common.py +++ b/tests/components/mqtt/common.py @@ -17,7 +17,10 @@ from tests.common import ( async def help_test_setting_attribute_via_mqtt_json_message( hass, mqtt_mock, domain, config ): - """Test the setting of attribute via MQTT with JSON payload.""" + """Test the setting of attribute via MQTT with JSON payload. + + This is a test helper for the MqttAttributes mixin. + """ assert await async_setup_component(hass, domain, config,) async_fire_mqtt_message(hass, "attr-topic", '{ "val": "100" }') @@ -29,7 +32,10 @@ async def help_test_setting_attribute_via_mqtt_json_message( async def help_test_update_with_json_attrs_not_dict( hass, mqtt_mock, caplog, domain, config ): - """Test attributes get extracted from a JSON result.""" + """Test attributes get extracted from a JSON result. + + This is a test helper for the MqttAttributes mixin. + """ assert await async_setup_component(hass, domain, config,) async_fire_mqtt_message(hass, "attr-topic", '[ "list", "of", "things"]') @@ -42,7 +48,10 @@ async def help_test_update_with_json_attrs_not_dict( async def help_test_update_with_json_attrs_bad_JSON( hass, mqtt_mock, caplog, domain, config ): - """Test attributes get extracted from a JSON result.""" + """Test JSON validation of attributes. + + This is a test helper for the MqttAttributes mixin. + """ assert await async_setup_component(hass, domain, config,) async_fire_mqtt_message(hass, "attr-topic", "This is not JSON") @@ -55,13 +64,16 @@ async def help_test_update_with_json_attrs_bad_JSON( async def help_test_discovery_update_attr( hass, mqtt_mock, caplog, domain, data1, data2 ): - """Test update of discovered MQTTAttributes.""" + """Test update of discovered MQTTAttributes. + + This is a test helper for the MqttAttributes mixin. + """ entry = MockConfigEntry(domain=mqtt.DOMAIN) await async_start(hass, "homeassistant", {}, entry) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) await hass.async_block_till_done() async_fire_mqtt_message(hass, "attr-topic1", '{ "val": "100" }') - state = hass.states.get(f"{domain}.beer") + state = hass.states.get(f"{domain}.test") assert state.attributes.get("val") == "100" # Change json_attributes_topic @@ -70,17 +82,17 @@ async def help_test_discovery_update_attr( # Verify we are no longer subscribing to the old topic async_fire_mqtt_message(hass, "attr-topic1", '{ "val": "50" }') - state = hass.states.get(f"{domain}.beer") + state = hass.states.get(f"{domain}.test") assert state.attributes.get("val") == "100" # Verify we are subscribing to the new topic async_fire_mqtt_message(hass, "attr-topic2", '{ "val": "75" }') - state = hass.states.get(f"{domain}.beer") + state = hass.states.get(f"{domain}.test") assert state.attributes.get("val") == "75" async def help_test_unique_id(hass, domain, config): - """Test unique id option only creates one alarm per unique_id.""" + """Test unique id option only creates one entity per unique_id.""" await async_mock_mqtt_component(hass) assert await async_setup_component(hass, domain, config,) async_fire_mqtt_message(hass, "test-topic", "payload") @@ -88,26 +100,32 @@ async def help_test_unique_id(hass, domain, config): async def help_test_discovery_removal(hass, mqtt_mock, caplog, domain, data): - """Test removal of discovered component.""" + """Test removal of discovered component. + + This is a test helper for the MqttDiscoveryUpdate mixin. + """ entry = MockConfigEntry(domain=mqtt.DOMAIN) await async_start(hass, "homeassistant", {}, entry) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) await hass.async_block_till_done() - state = hass.states.get(f"{domain}.beer") + state = hass.states.get(f"{domain}.test") assert state is not None - assert state.name == "Beer" + assert state.name == "test" async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", "") await hass.async_block_till_done() - state = hass.states.get(f"{domain}.beer") + state = hass.states.get(f"{domain}.test") assert state is None async def help_test_discovery_update(hass, mqtt_mock, caplog, domain, data1, data2): - """Test update of discovered component.""" + """Test update of discovered component. + + This is a test helper for the MqttDiscoveryUpdate mixin. + """ entry = MockConfigEntry(domain=mqtt.DOMAIN) await async_start(hass, "homeassistant", {}, entry) @@ -150,13 +168,17 @@ async def help_test_discovery_broken(hass, mqtt_mock, caplog, domain, data1, dat assert state is None -async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, data): - """Test MQTT alarm control panel device registry integration.""" +async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, config): + """Test device registry integration. + + This is a test helper for the MqttDiscoveryUpdate mixin. + """ entry = MockConfigEntry(domain=mqtt.DOMAIN) entry.add_to_hass(hass) await async_start(hass, "homeassistant", {}, entry) registry = await hass.helpers.device_registry.async_get_registry() + data = json.dumps(config) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) await hass.async_block_till_done() @@ -171,7 +193,10 @@ async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, async def help_test_entity_device_info_update(hass, mqtt_mock, domain, config): - """Test device registry update.""" + """Test device registry update. + + This is a test helper for the MqttDiscoveryUpdate mixin. + """ entry = MockConfigEntry(domain=mqtt.DOMAIN) entry.add_to_hass(hass) await async_start(hass, "homeassistant", {}, entry) diff --git a/tests/components/mqtt/test_alarm_control_panel.py b/tests/components/mqtt/test_alarm_control_panel.py index 6a14f2ebbda..9a0df0bcd8d 100644 --- a/tests/components/mqtt/test_alarm_control_panel.py +++ b/tests/components/mqtt/test_alarm_control_panel.py @@ -1,4 +1,5 @@ """The tests the MQTT alarm control panel component.""" +import copy import json from homeassistant.components import alarm_control_panel @@ -36,6 +37,52 @@ from tests.components.alarm_control_panel import common CODE = "HELLO_CODE" +DEFAULT_CONFIG = { + alarm_control_panel.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "alarm/state", + "command_topic": "alarm/command", + } +} + +DEFAULT_CONFIG_ATTR = { + alarm_control_panel.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "alarm/state", + "command_topic": "alarm/command", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_CODE = { + alarm_control_panel.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "alarm/state", + "command_topic": "alarm/command", + "code": "1234", + "code_arm_required": True, + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "platform": "mqtt", + "name": "test", + "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", +} + async def test_fail_setup_without_state_topic(hass, mqtt_mock): """Test for failing with no state topic.""" @@ -71,16 +118,7 @@ async def test_fail_setup_without_command_topic(hass, mqtt_mock): async def test_update_state_via_state_topic(hass, mqtt_mock): """Test updating with via state topic.""" assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG, ) entity_id = "alarm_control_panel.test" @@ -102,16 +140,7 @@ async def test_update_state_via_state_topic(hass, mqtt_mock): async def test_ignore_update_state_if_unknown_via_state_topic(hass, mqtt_mock): """Test ignoring updates via state topic.""" assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG, ) entity_id = "alarm_control_panel.test" @@ -125,16 +154,7 @@ async def test_ignore_update_state_if_unknown_via_state_topic(hass, mqtt_mock): async def test_arm_home_publishes_mqtt(hass, mqtt_mock): """Test publishing of MQTT messages while armed.""" assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG, ) await common.async_alarm_arm_home(hass) @@ -149,18 +169,7 @@ async def test_arm_home_not_publishes_mqtt_with_invalid_code_when_req(hass, mqtt When code_arm_required = True """ assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_arm_required": True, - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE, ) call_count = mqtt_mock.async_publish.call_count @@ -173,20 +182,9 @@ async def test_arm_home_publishes_mqtt_when_code_not_req(hass, mqtt_mock): When code_arm_required = False """ - assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_arm_required": False, - } - }, - ) + config = copy.deepcopy(DEFAULT_CONFIG_CODE) + config[alarm_control_panel.DOMAIN]["code_arm_required"] = False + assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,) await common.async_alarm_arm_home(hass) mqtt_mock.async_publish.assert_called_once_with( @@ -197,16 +195,7 @@ async def test_arm_home_publishes_mqtt_when_code_not_req(hass, mqtt_mock): async def test_arm_away_publishes_mqtt(hass, mqtt_mock): """Test publishing of MQTT messages while armed.""" assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG, ) await common.async_alarm_arm_away(hass) @@ -221,18 +210,7 @@ async def test_arm_away_not_publishes_mqtt_with_invalid_code_when_req(hass, mqtt When code_arm_required = True """ assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_arm_required": True, - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE, ) call_count = mqtt_mock.async_publish.call_count @@ -245,20 +223,9 @@ async def test_arm_away_publishes_mqtt_when_code_not_req(hass, mqtt_mock): When code_arm_required = False """ - assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_arm_required": False, - } - }, - ) + config = copy.deepcopy(DEFAULT_CONFIG_CODE) + config[alarm_control_panel.DOMAIN]["code_arm_required"] = False + assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,) await common.async_alarm_arm_away(hass) mqtt_mock.async_publish.assert_called_once_with( @@ -269,16 +236,7 @@ async def test_arm_away_publishes_mqtt_when_code_not_req(hass, mqtt_mock): async def test_arm_night_publishes_mqtt(hass, mqtt_mock): """Test publishing of MQTT messages while armed.""" assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG, ) await common.async_alarm_arm_night(hass) @@ -293,18 +251,7 @@ async def test_arm_night_not_publishes_mqtt_with_invalid_code_when_req(hass, mqt When code_arm_required = True """ assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_arm_required": True, - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE, ) call_count = mqtt_mock.async_publish.call_count @@ -317,20 +264,9 @@ async def test_arm_night_publishes_mqtt_when_code_not_req(hass, mqtt_mock): When code_arm_required = False """ - assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_arm_required": False, - } - }, - ) + config = copy.deepcopy(DEFAULT_CONFIG_CODE) + config[alarm_control_panel.DOMAIN]["code_arm_required"] = False + assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,) await common.async_alarm_arm_night(hass) mqtt_mock.async_publish.assert_called_once_with( @@ -341,16 +277,7 @@ async def test_arm_night_publishes_mqtt_when_code_not_req(hass, mqtt_mock): async def test_disarm_publishes_mqtt(hass, mqtt_mock): """Test publishing of MQTT messages while disarmed.""" assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG, ) await common.async_alarm_disarm(hass) @@ -362,20 +289,12 @@ async def test_disarm_publishes_mqtt_with_template(hass, mqtt_mock): When command_template set to output json """ - assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "command_template": '{"action":"{{ action }}",' '"code":"{{ code }}"}', - } - }, + config = copy.deepcopy(DEFAULT_CONFIG_CODE) + config[alarm_control_panel.DOMAIN]["code"] = "1234" + config[alarm_control_panel.DOMAIN]["command_template"] = ( + '{"action":"{{ action }}",' '"code":"{{ code }}"}' ) + assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,) await common.async_alarm_disarm(hass, 1234) mqtt_mock.async_publish.assert_called_once_with( @@ -388,20 +307,10 @@ async def test_disarm_publishes_mqtt_when_code_not_req(hass, mqtt_mock): When code_disarm_required = False """ - assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_disarm_required": False, - } - }, - ) + config = copy.deepcopy(DEFAULT_CONFIG_CODE) + config[alarm_control_panel.DOMAIN]["code"] = "1234" + config[alarm_control_panel.DOMAIN]["code_disarm_required"] = False + assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,) await common.async_alarm_disarm(hass) mqtt_mock.async_publish.assert_called_once_with("alarm/command", "DISARM", 0, False) @@ -413,18 +322,7 @@ async def test_disarm_not_publishes_mqtt_with_invalid_code_when_req(hass, mqtt_m When code_disarm_required = True """ assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "code_disarm_required": True, - } - }, + hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE, ) call_count = mqtt_mock.async_publish.call_count @@ -434,20 +332,9 @@ async def test_disarm_not_publishes_mqtt_with_invalid_code_when_req(hass, mqtt_m async def test_default_availability_payload(hass, mqtt_mock): """Test availability by default payload with defined topic.""" - assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "availability_topic": "availability-topic", - } - }, - ) + config = copy.deepcopy(DEFAULT_CONFIG_CODE) + config[alarm_control_panel.DOMAIN]["availability_topic"] = "availability-topic" + assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,) state = hass.states.get("alarm_control_panel.test") assert state.state == STATE_UNAVAILABLE @@ -465,22 +352,11 @@ async def test_default_availability_payload(hass, mqtt_mock): async def test_custom_availability_payload(hass, mqtt_mock): """Test availability by custom payload with defined topic.""" - assert await async_setup_component( - hass, - alarm_control_panel.DOMAIN, - { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "alarm/state", - "command_topic": "alarm/command", - "code": "1234", - "availability_topic": "availability-topic", - "payload_available": "good", - "payload_not_available": "nogood", - } - }, - ) + config = copy.deepcopy(DEFAULT_CONFIG) + config[alarm_control_panel.DOMAIN]["availability_topic"] = "availability-topic" + config[alarm_control_panel.DOMAIN]["payload_available"] = "good" + config[alarm_control_panel.DOMAIN]["payload_not_available"] = "nogood" + assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,) state = hass.states.get("alarm_control_panel.test") assert state.state == STATE_UNAVAILABLE @@ -496,22 +372,6 @@ async def test_custom_availability_payload(hass, mqtt_mock): assert state.state == STATE_UNAVAILABLE -async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): - """Test the setting of attribute via MQTT with JSON payload.""" - config = { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } - await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, alarm_control_panel.DOMAIN, config - ) - - async def test_update_state_via_state_topic_template(hass, mqtt_mock): """Test updating with template_value via state topic.""" assert await async_setup_component( @@ -542,50 +402,36 @@ async def test_update_state_via_state_topic_template(hass, mqtt_mock): assert state.state == STATE_ALARM_ARMED_AWAY +async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): + """Test the setting of attribute via MQTT with JSON payload.""" + await help_test_setting_attribute_via_mqtt_json_message( + hass, mqtt_mock, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_ATTR + ) + + async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, config + hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - alarm_control_panel.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, config + hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" - data1 = ( - '{ "name": "Beer",' - ' "command_topic": "test_topic",' - ' "json_attributes_topic": "attr-topic1" }' - ) - data2 = ( - '{ "name": "Beer",' - ' "command_topic": "test_topic",' - ' "json_attributes_topic": "attr-topic2" }' - ) + config1 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN]) + config1["json_attributes_topic"] = "attr-topic1" + config2["json_attributes_topic"] = "attr-topic2" + data1 = json.dumps(config1) + data2 = json.dumps(config2) + await help_test_discovery_update_attr( hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, data1, data2 ) @@ -616,11 +462,7 @@ async def test_unique_id(hass): async def test_discovery_removal_alarm(hass, mqtt_mock, caplog): """Test removal of discovered alarm_control_panel.""" - data = ( - '{ "name": "Beer",' - ' "state_topic": "test_topic",' - ' "command_topic": "test_topic" }' - ) + data = json.dumps(DEFAULT_CONFIG[alarm_control_panel.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, data ) @@ -628,16 +470,13 @@ async def test_discovery_removal_alarm(hass, mqtt_mock, caplog): async def test_discovery_update_alarm(hass, mqtt_mock, caplog): """Test update of discovered alarm_control_panel.""" - data1 = ( - '{ "name": "Beer",' - ' "state_topic": "test_topic",' - ' "command_topic": "test_topic" }' - ) - data2 = ( - '{ "name": "Milk",' - ' "state_topic": "test_topic",' - ' "command_topic": "test_topic" }' - ) + config1 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN]) + config1["name"] = "Beer" + config2["name"] = "Milk" + + data1 = json.dumps(config1) + data2 = json.dumps(config2) await help_test_discovery_update( hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, data1, data2 ) @@ -658,47 +497,15 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT alarm control panel device registry integration.""" - data = json.dumps( - { - "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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, alarm_control_panel.DOMAIN, data + hass, mqtt_mock, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "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", - } await help_test_entity_device_info_update( - hass, mqtt_mock, alarm_control_panel.DOMAIN, config + hass, mqtt_mock, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index e77cddda76d..2cc917c527b 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -1,4 +1,5 @@ """The tests for the MQTT binary sensor platform.""" +import copy from datetime import datetime, timedelta import json from unittest.mock import patch @@ -30,6 +31,38 @@ from .common import ( from tests.common import async_fire_mqtt_message, async_fire_time_changed +DEFAULT_CONFIG = { + binary_sensor.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "test-topic", + } +} + +DEFAULT_CONFIG_ATTR = { + binary_sensor.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "test-topic", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "platform": "mqtt", + "name": "test", + "state_topic": "test-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 def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock, caplog): """Test the expiration of the value.""" @@ -424,61 +457,34 @@ async def test_off_delay(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - binary_sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, binary_sensor.DOMAIN, config + hass, mqtt_mock, binary_sensor.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - binary_sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, binary_sensor.DOMAIN, config + hass, mqtt_mock, caplog, binary_sensor.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - binary_sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, binary_sensor.DOMAIN, config + hass, mqtt_mock, caplog, binary_sensor.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" - data1 = ( - '{ "name": "Beer",' - ' "state_topic": "test_topic",' - ' "json_attributes_topic": "attr-topic1" }' - ) - data2 = ( - '{ "name": "Beer",' - ' "state_topic": "test_topic",' - ' "json_attributes_topic": "attr-topic2" }' - ) + config1 = copy.deepcopy(DEFAULT_CONFIG_ATTR[binary_sensor.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG_ATTR[binary_sensor.DOMAIN]) + config1["json_attributes_topic"] = "attr-topic1" + config2["json_attributes_topic"] = "attr-topic2" + data1 = json.dumps(config1) + data2 = json.dumps(config2) + await help_test_discovery_update_attr( hass, mqtt_mock, caplog, binary_sensor.DOMAIN, data1, data2 ) @@ -507,11 +513,7 @@ async def test_unique_id(hass): async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog): """Test removal of discovered binary_sensor.""" - data = ( - '{ "name": "Beer",' - ' "state_topic": "test_topic",' - ' "availability_topic": "availability_topic" }' - ) + data = json.dumps(DEFAULT_CONFIG[binary_sensor.DOMAIN]) await help_test_discovery_removal( hass, mqtt_mock, caplog, binary_sensor.DOMAIN, data ) @@ -519,16 +521,13 @@ async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog): async def test_discovery_update_binary_sensor(hass, mqtt_mock, caplog): """Test update of discovered binary_sensor.""" - data1 = ( - '{ "name": "Beer",' - ' "state_topic": "test_topic",' - ' "availability_topic": "availability_topic1" }' - ) - data2 = ( - '{ "name": "Milk",' - ' "state_topic": "test_topic2",' - ' "availability_topic": "availability_topic2" }' - ) + config1 = copy.deepcopy(DEFAULT_CONFIG[binary_sensor.DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[binary_sensor.DOMAIN]) + config1["name"] = "Beer" + config2["name"] = "Milk" + + data1 = json.dumps(config1) + data2 = json.dumps(config2) await help_test_discovery_update( hass, mqtt_mock, caplog, binary_sensor.DOMAIN, data1, data2 ) @@ -545,45 +544,15 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT binary sensor device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, binary_sensor.DOMAIN, data + hass, mqtt_mock, binary_sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-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", - } await help_test_entity_device_info_update( - hass, mqtt_mock, binary_sensor.DOMAIN, config + hass, mqtt_mock, binary_sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 677fef06f22..a6fb5f2cc66 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -60,6 +60,32 @@ DEFAULT_CONFIG = { } } +DEFAULT_CONFIG_ATTR = { + CLIMATE_DOMAIN: { + "platform": "mqtt", + "name": "test", + "power_state_topic": "test-topic", + "power_command_topic": "test_topic", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "platform": "mqtt", + "name": "Test 1", + "power_state_topic": "test-topic", + "power_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 def test_setup_params(hass, mqtt_mock): """Test the initial parameters.""" @@ -773,66 +799,34 @@ async def test_temp_step_custom(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - CLIMATE_DOMAIN: { - "platform": "mqtt", - "name": "test", - "power_state_topic": "test-topic", - "power_command_topic": "test_topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, CLIMATE_DOMAIN, config + hass, mqtt_mock, CLIMATE_DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - CLIMATE_DOMAIN: { - "platform": "mqtt", - "name": "test", - "power_state_topic": "test-topic", - "power_command_topic": "test_topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, CLIMATE_DOMAIN, config + hass, mqtt_mock, caplog, CLIMATE_DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - CLIMATE_DOMAIN: { - "platform": "mqtt", - "name": "test", - "power_state_topic": "test-topic", - "power_command_topic": "test_topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, CLIMATE_DOMAIN, config + hass, mqtt_mock, caplog, CLIMATE_DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" - data1 = ( - '{ "name": "Beer",' - ' "power_state_topic": "test-topic",' - ' "power_command_topic": "test_topic",' - ' "json_attributes_topic": "attr-topic1" }' - ) - data2 = ( - '{ "name": "Beer",' - ' "power_state_topic": "test-topic",' - ' "power_command_topic": "test_topic",' - ' "json_attributes_topic": "attr-topic2" }' - ) + config1 = copy.deepcopy(DEFAULT_CONFIG[CLIMATE_DOMAIN]) + config2 = copy.deepcopy(DEFAULT_CONFIG[CLIMATE_DOMAIN]) + config1["json_attributes_topic"] = "attr-topic1" + config2["json_attributes_topic"] = "attr-topic2" + data1 = json.dumps(config1) + data2 = json.dumps(config2) + await help_test_discovery_update_attr( hass, mqtt_mock, caplog, CLIMATE_DOMAIN, data1, data2 ) @@ -863,7 +857,7 @@ async def test_unique_id(hass): async def test_discovery_removal_climate(hass, mqtt_mock, caplog): """Test removal of discovered climate.""" - data = '{ "name": "Beer" }' + data = json.dumps(DEFAULT_CONFIG[CLIMATE_DOMAIN]) await help_test_discovery_removal(hass, mqtt_mock, caplog, CLIMATE_DOMAIN, data) @@ -887,44 +881,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT climate device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "name": "Test 1", - "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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, CLIMATE_DOMAIN, data + hass, mqtt_mock, CLIMATE_DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "platform": "mqtt", - "name": "Test 1", - "power_state_topic": "test-topic", - "power_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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, CLIMATE_DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, CLIMATE_DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index 0bf8aa92150..78f7dc72a24 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -1,6 +1,4 @@ """The tests for the MQTT cover platform.""" -import json - from homeassistant.components import cover from homeassistant.components.cover import ATTR_POSITION, ATTR_TILT_POSITION from homeassistant.components.mqtt.cover import MqttCover @@ -41,6 +39,31 @@ from .common import ( from tests.common import async_fire_mqtt_message +DEFAULT_CONFIG_ATTR = { + cover.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_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", +} + async def test_state_via_state_topic(hass, mqtt_mock): """Test the controlling state via topic.""" @@ -1700,61 +1723,38 @@ async def test_invalid_device_class(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - cover.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, cover.DOMAIN, config + hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - cover.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, cover.DOMAIN, config + hass, mqtt_mock, caplog, cover.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - cover.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, cover.DOMAIN, config + hass, mqtt_mock, caplog, cover.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' ) + await help_test_discovery_update_attr( hass, mqtt_mock, caplog, cover.DOMAIN, data1, data2 ) @@ -1783,7 +1783,7 @@ async def test_unique_id(hass): async def test_discovery_removal_cover(hass, mqtt_mock, caplog): """Test removal of discovered cover.""" - data = '{ "name": "Beer",' ' "command_topic": "test_topic" }' + data = '{ "name": "test",' ' "command_topic": "test_topic" }' await help_test_discovery_removal(hass, mqtt_mock, caplog, cover.DOMAIN, data) @@ -1807,46 +1807,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT cover device registry integration.""" - data = json.dumps( - { - "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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, cover.DOMAIN, data + hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, cover.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_fan.py b/tests/components/mqtt/test_fan.py index 556dbbe5528..512dddd4fc6 100644 --- a/tests/components/mqtt/test_fan.py +++ b/tests/components/mqtt/test_fan.py @@ -1,6 +1,4 @@ """Test MQTT fans.""" -import json - from homeassistant.components import fan from homeassistant.const import ( ATTR_ASSUMED_STATE, @@ -27,6 +25,31 @@ from .common import ( from tests.common import async_fire_mqtt_message from tests.components.fan import common +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", +} + async def test_fail_setup_if_no_command_topic(hass, mqtt_mock): """Test if command fails with command topic.""" @@ -447,58 +470,34 @@ async def test_custom_availability_payload(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - fan.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, fan.DOMAIN, config + hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - fan.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, fan.DOMAIN, config + hass, mqtt_mock, caplog, fan.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - fan.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, fan.DOMAIN, config + hass, mqtt_mock, caplog, fan.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' ) @@ -532,7 +531,7 @@ async def test_unique_id(hass): async def test_discovery_removal_fan(hass, mqtt_mock, caplog): """Test removal of discovered fan.""" - data = '{ "name": "Beer",' ' "command_topic": "test_topic" }' + data = '{ "name": "test",' ' "command_topic": "test_topic" }' await help_test_discovery_removal(hass, mqtt_mock, caplog, fan.DOMAIN, data) @@ -552,46 +551,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT fan device registry integration.""" - data = json.dumps( - { - "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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, fan.DOMAIN, data + hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, fan.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_legacy_vacuum.py b/tests/components/mqtt/test_legacy_vacuum.py index 558a185dbb0..c3500e6ac6a 100644 --- a/tests/components/mqtt/test_legacy_vacuum.py +++ b/tests/components/mqtt/test_legacy_vacuum.py @@ -63,6 +63,29 @@ DEFAULT_CONFIG = { mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"], } +DEFAULT_CONFIG_ATTR = { + vacuum.DOMAIN: { + "platform": "mqtt", + "name": "test", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "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 def test_default_supported_features(hass, mqtt_mock): """Test that the correct supported features.""" @@ -525,55 +548,34 @@ async def test_custom_availability_payload(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "name": "test", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, vacuum.DOMAIN, config + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "name": "test", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, vacuum.DOMAIN, config + hass, mqtt_mock, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "name": "test", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, vacuum.DOMAIN, config + hass, mqtt_mock, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' ) @@ -605,7 +607,7 @@ async def test_unique_id(hass, mqtt_mock): async def test_discovery_removal_vacuum(hass, mqtt_mock, caplog): """Test removal of discovered vacuum.""" - data = '{ "name": "Beer",' ' "command_topic": "test_topic" }' + data = json.dumps(DEFAULT_CONFIG_ATTR[vacuum.DOMAIN]) await help_test_discovery_removal(hass, mqtt_mock, caplog, vacuum.DOMAIN, data) @@ -629,44 +631,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT vacuum device registry integration.""" - 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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, vacuum.DOMAIN, data + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, vacuum.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 389806a2b20..f2bde3d3b43 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -153,7 +153,6 @@ light: payload_off: "off" """ -import json from unittest import mock from unittest.mock import patch @@ -190,6 +189,31 @@ from tests.common import ( ) from tests.components.light import common +DEFAULT_CONFIG_ATTR = { + light.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", +} + async def test_fail_setup_if_no_command_topic(hass, mqtt_mock): """Test if command fails with command topic.""" @@ -1075,58 +1099,34 @@ async def test_custom_availability_payload(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, light.DOMAIN, config + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, light.DOMAIN, config + hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, light.DOMAIN, config + hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' ) @@ -1161,7 +1161,7 @@ async def test_unique_id(hass): async def test_discovery_removal_light(hass, mqtt_mock, caplog): """Test removal of discovered light.""" data = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "state_topic": "test_topic",' ' "command_topic": "test_topic" }' ) @@ -1214,46 +1214,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT light device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test-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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, light.DOMAIN, data + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, light.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 8783f16c9af..71ced8f1db2 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -120,6 +120,33 @@ from .common import ( from tests.common import MockConfigEntry, async_fire_mqtt_message, mock_coro from tests.components.light import common +DEFAULT_CONFIG_ATTR = { + light.DOMAIN: { + "platform": "mqtt", + "schema": "json", + "name": "test", + "command_topic": "test-topic", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "platform": "mqtt", + "name": "Test 1", + "schema": "json", + "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", +} + class JsonValidator(object): """Helper to compare JSON.""" @@ -921,62 +948,35 @@ async def test_custom_availability_payload(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "json", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, light.DOMAIN, config + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "json", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, light.DOMAIN, config + hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "json", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, light.DOMAIN, config + hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "schema": "json",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "schema": "json",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' @@ -1013,7 +1013,7 @@ async def test_unique_id(hass): async def test_discovery_removal(hass, mqtt_mock, caplog): """Test removal of discovered mqtt_json lights.""" - data = '{ "name": "Beer",' ' "schema": "json",' ' "command_topic": "test_topic" }' + data = '{ "name": "test",' ' "schema": "json",' ' "command_topic": "test_topic" }' await help_test_discovery_removal(hass, mqtt_mock, caplog, light.DOMAIN, data) @@ -1068,48 +1068,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT light device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "name": "Test 1", - "schema": "json", - "state_topic": "test-topic", - "command_topic": "test-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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, light.DOMAIN, data + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "platform": "mqtt", - "name": "Test 1", - "schema": "json", - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, light.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index 26c58b7522c..9d4d3fcba25 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -26,7 +26,6 @@ If your light doesn't support white value feature, omit `white_value_template`. If your light doesn't support RGB feature, omit `(red|green|blue)_template`. """ -import json from unittest.mock import patch from homeassistant.components import light, mqtt @@ -61,6 +60,37 @@ from tests.common import ( mock_coro, ) +DEFAULT_CONFIG_ATTR = { + light.DOMAIN: { + "platform": "mqtt", + "schema": "template", + "name": "test", + "command_topic": "test-topic", + "command_on_template": "on,{{ transition }}", + "command_off_template": "off,{{ transition|d }}", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "platform": "mqtt", + "name": "Test 1", + "schema": "template", + "state_topic": "test-topic", + "command_topic": "test-command-topic", + "command_on_template": "on,{{ transition }}", + "command_off_template": "off,{{ transition|d }}", + "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 def test_setup_fails(hass, mqtt_mock): """Test that setup fails with missing required configuration items.""" @@ -522,62 +552,29 @@ async def test_custom_availability_payload(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "template", - "name": "test", - "command_topic": "test-topic", - "command_on_template": "on,{{ transition }}", - "command_off_template": "off,{{ transition|d }}", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, light.DOMAIN, config + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "template", - "name": "test", - "command_topic": "test-topic", - "command_on_template": "on,{{ transition }}", - "command_off_template": "off,{{ transition|d }}", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, light.DOMAIN, config + hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - light.DOMAIN: { - "platform": "mqtt", - "schema": "template", - "name": "test", - "command_topic": "test-topic", - "command_on_template": "on,{{ transition }}", - "command_off_template": "off,{{ transition|d }}", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, light.DOMAIN, config + hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "schema": "template",' ' "command_topic": "test_topic",' ' "command_on_template": "on",' @@ -585,7 +582,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "schema": "template",' ' "command_topic": "test_topic",' ' "command_on_template": "on",' @@ -627,7 +624,7 @@ async def test_unique_id(hass): async def test_discovery_removal(hass, mqtt_mock, caplog): """Test removal of discovered mqtt_json lights.""" data = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "schema": "template",' ' "command_topic": "test_topic",' ' "command_on_template": "on",' @@ -695,52 +692,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT light device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "name": "Test 1", - "schema": "template", - "state_topic": "test-topic", - "command_topic": "test-topic", - "command_on_template": "on,{{ transition }}", - "command_off_template": "off,{{ transition|d }}", - "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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, light.DOMAIN, data + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "platform": "mqtt", - "name": "Test 1", - "schema": "template", - "state_topic": "test-topic", - "command_topic": "test-command-topic", - "command_on_template": "on,{{ transition }}", - "command_off_template": "off,{{ transition|d }}", - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, light.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_lock.py b/tests/components/mqtt/test_lock.py index a9c1fe73952..d636eb1534d 100644 --- a/tests/components/mqtt/test_lock.py +++ b/tests/components/mqtt/test_lock.py @@ -1,6 +1,4 @@ """The tests for the MQTT lock platform.""" -import json - from homeassistant.components import lock from homeassistant.const import ( ATTR_ASSUMED_STATE, @@ -27,6 +25,31 @@ from .common import ( from tests.common import async_fire_mqtt_message from tests.components.lock import common +DEFAULT_CONFIG_ATTR = { + lock.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", +} + async def test_controlling_state_via_topic(hass, mqtt_mock): """Test the controlling state via topic.""" @@ -316,58 +339,34 @@ async def test_custom_availability_payload(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - lock.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, lock.DOMAIN, config + hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - lock.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, lock.DOMAIN, config + hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - lock.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, lock.DOMAIN, config + hass, mqtt_mock, caplog, lock.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' ) @@ -401,7 +400,7 @@ async def test_unique_id(hass): async def test_discovery_removal_lock(hass, mqtt_mock, caplog): """Test removal of discovered lock.""" - data = '{ "name": "Beer",' ' "command_topic": "test_topic" }' + data = '{ "name": "test",' ' "command_topic": "test_topic" }' await help_test_discovery_removal(hass, mqtt_mock, caplog, lock.DOMAIN, data) @@ -431,46 +430,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT lock device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-topic", - "command_topic": "test-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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, lock.DOMAIN, data + hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, lock.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index be29a297d3d..0cf24894bcb 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -31,6 +31,30 @@ from tests.common import ( async_fire_time_changed, ) +DEFAULT_CONFIG_ATTR = { + sensor.DOMAIN: { + "platform": "mqtt", + "name": "test", + "state_topic": "test-topic", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "platform": "mqtt", + "name": "Test 1", + "state_topic": "test-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 def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock): """Test the setting of the value via MQTT.""" @@ -447,43 +471,27 @@ async def test_setting_attribute_with_template(hass, mqtt_mock): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, sensor.DOMAIN, config + hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - sensor.DOMAIN: { - "platform": "mqtt", - "name": "test", - "state_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, sensor.DOMAIN, config + hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "state_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "state_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' ) @@ -515,7 +523,7 @@ async def test_unique_id(hass): async def test_discovery_removal_sensor(hass, mqtt_mock, caplog): """Test removal of discovered sensor.""" - data = '{ "name": "Beer",' ' "state_topic": "test_topic" }' + data = '{ "name": "test",' ' "state_topic": "test_topic" }' await help_test_discovery_removal(hass, mqtt_mock, caplog, sensor.DOMAIN, data) @@ -539,44 +547,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT sensor device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, sensor.DOMAIN, data + hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "platform": "mqtt", - "name": "Test 1", - "state_topic": "test-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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, sensor.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_state_vacuum.py b/tests/components/mqtt/test_state_vacuum.py index 146f69b30d7..52c101d138c 100644 --- a/tests/components/mqtt/test_state_vacuum.py +++ b/tests/components/mqtt/test_state_vacuum.py @@ -63,6 +63,31 @@ DEFAULT_CONFIG = { mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"], } +DEFAULT_CONFIG_ATTR = { + vacuum.DOMAIN: { + "platform": "mqtt", + "schema": "state", + "name": "test", + "json_attributes_topic": "attr-topic", + } +} + +DEFAULT_CONFIG_DEVICE_INFO = { + "platform": "mqtt", + "schema": "state", + "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 def test_default_supported_features(hass, mqtt_mock): """Test that the correct supported features.""" @@ -351,59 +376,35 @@ async def test_custom_availability_payload(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "schema": "state", - "name": "test", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, vacuum.DOMAIN, config + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "schema": "state", - "name": "test", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, vacuum.DOMAIN, config + hass, mqtt_mock, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_json(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - vacuum.DOMAIN: { - "platform": "mqtt", - "schema": "state", - "name": "test", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, vacuum.DOMAIN, config + hass, mqtt_mock, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "schema": "state",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "schema": "state",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' @@ -438,7 +439,7 @@ async def test_unique_id(hass, mqtt_mock): async def test_discovery_removal_vacuum(hass, mqtt_mock, caplog): """Test removal of discovered vacuum.""" - data = '{ "schema": "state", "name": "Beer",' ' "command_topic": "test_topic"}' + data = '{ "schema": "state", "name": "test",' ' "command_topic": "test_topic"}' await help_test_discovery_removal(hass, mqtt_mock, caplog, vacuum.DOMAIN, data) @@ -462,46 +463,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT vacuum device registry integration.""" - data = json.dumps( - { - "platform": "mqtt", - "schema": "state", - "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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, vacuum.DOMAIN, data + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "platform": "mqtt", - "schema": "state", - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, vacuum.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index cfb5c3598b2..983d91f08a2 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -1,6 +1,4 @@ """The tests for the MQTT switch platform.""" -import json - from asynctest import patch import pytest @@ -31,6 +29,31 @@ from .common import ( from tests.common import async_fire_mqtt_message, async_mock_mqtt_component, mock_coro from tests.components.switch import common +DEFAULT_CONFIG_ATTR = { + switch.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", +} + @pytest.fixture def mock_publish(hass): @@ -271,58 +294,34 @@ async def test_custom_state_payload(hass, mock_publish): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): """Test the setting of attribute via MQTT with JSON payload.""" - config = { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_setting_attribute_via_mqtt_json_message( - hass, mqtt_mock, switch.DOMAIN, config + hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_not_dict( - hass, mqtt_mock, caplog, switch.DOMAIN, config + hass, mqtt_mock, caplog, switch.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog): """Test attributes get extracted from a JSON result.""" - config = { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "test-topic", - "json_attributes_topic": "attr-topic", - } - } await help_test_update_with_json_attrs_bad_JSON( - hass, mqtt_mock, caplog, switch.DOMAIN, config + hass, mqtt_mock, caplog, switch.DOMAIN, DEFAULT_CONFIG_ATTR ) async def test_discovery_update_attr(hass, mqtt_mock, caplog): """Test update of discovered MQTTAttributes.""" data1 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic1" }' ) data2 = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "command_topic": "test_topic",' ' "json_attributes_topic": "attr-topic2" }' ) @@ -357,7 +356,7 @@ async def test_unique_id(hass): async def test_discovery_removal_switch(hass, mqtt_mock, caplog): """Test removal of discovered switch.""" data = ( - '{ "name": "Beer",' + '{ "name": "test",' ' "state_topic": "test_topic",' ' "command_topic": "test_topic" }' ) @@ -396,46 +395,16 @@ async def test_discovery_broken(hass, mqtt_mock, caplog): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT switch device registry integration.""" - data = json.dumps( - { - "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", - } - ) await help_test_entity_device_info_with_identifier( - hass, mqtt_mock, switch.DOMAIN, data + hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO ) async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - config = { - "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", - } - await help_test_entity_device_info_update(hass, mqtt_mock, switch.DOMAIN, config) + await help_test_entity_device_info_update( + hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO + ) async def test_entity_id_update(hass, mqtt_mock):