Remove duplicated config from MQTT tests (#32689)

This commit is contained in:
Erik Montnemery 2020-03-11 17:34:19 +01:00 committed by GitHub
parent 5216dc0ae1
commit f7ddbc7e1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 599 additions and 1131 deletions

View file

@ -17,7 +17,10 @@ from tests.common import (
async def help_test_setting_attribute_via_mqtt_json_message( async def help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock, domain, config 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,) assert await async_setup_component(hass, domain, config,)
async_fire_mqtt_message(hass, "attr-topic", '{ "val": "100" }') 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( async def help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock, caplog, domain, config 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,) assert await async_setup_component(hass, domain, config,)
async_fire_mqtt_message(hass, "attr-topic", '[ "list", "of", "things"]') 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( async def help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock, caplog, domain, config 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,) assert await async_setup_component(hass, domain, config,)
async_fire_mqtt_message(hass, "attr-topic", "This is not JSON") 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( async def help_test_discovery_update_attr(
hass, mqtt_mock, caplog, domain, data1, data2 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) entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, "homeassistant", {}, entry) await async_start(hass, "homeassistant", {}, entry)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
async_fire_mqtt_message(hass, "attr-topic1", '{ "val": "100" }') 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" assert state.attributes.get("val") == "100"
# Change json_attributes_topic # 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 # Verify we are no longer subscribing to the old topic
async_fire_mqtt_message(hass, "attr-topic1", '{ "val": "50" }') 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" assert state.attributes.get("val") == "100"
# Verify we are subscribing to the new topic # Verify we are subscribing to the new topic
async_fire_mqtt_message(hass, "attr-topic2", '{ "val": "75" }') 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" assert state.attributes.get("val") == "75"
async def help_test_unique_id(hass, domain, config): 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) await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, domain, config,) assert await async_setup_component(hass, domain, config,)
async_fire_mqtt_message(hass, "test-topic", "payload") 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): 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) entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, "homeassistant", {}, entry) await async_start(hass, "homeassistant", {}, entry)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
await hass.async_block_till_done() 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 is not None
assert state.name == "Beer" assert state.name == "test"
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", "") async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", "")
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(f"{domain}.beer") state = hass.states.get(f"{domain}.test")
assert state is None assert state is None
async def help_test_discovery_update(hass, mqtt_mock, caplog, domain, data1, data2): 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) entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, "homeassistant", {}, entry) 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 assert state is None
async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, data): async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, config):
"""Test MQTT alarm control panel device registry integration.""" """Test device registry integration.
This is a test helper for the MqttDiscoveryUpdate mixin.
"""
entry = MockConfigEntry(domain=mqtt.DOMAIN) entry = MockConfigEntry(domain=mqtt.DOMAIN)
entry.add_to_hass(hass) entry.add_to_hass(hass)
await async_start(hass, "homeassistant", {}, entry) await async_start(hass, "homeassistant", {}, entry)
registry = await hass.helpers.device_registry.async_get_registry() registry = await hass.helpers.device_registry.async_get_registry()
data = json.dumps(config)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
await hass.async_block_till_done() 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): 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 = MockConfigEntry(domain=mqtt.DOMAIN)
entry.add_to_hass(hass) entry.add_to_hass(hass)
await async_start(hass, "homeassistant", {}, entry) await async_start(hass, "homeassistant", {}, entry)

View file

@ -1,4 +1,5 @@
"""The tests the MQTT alarm control panel component.""" """The tests the MQTT alarm control panel component."""
import copy
import json import json
from homeassistant.components import alarm_control_panel from homeassistant.components import alarm_control_panel
@ -36,6 +37,52 @@ from tests.components.alarm_control_panel import common
CODE = "HELLO_CODE" 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): async def test_fail_setup_without_state_topic(hass, mqtt_mock):
"""Test for failing with no state topic.""" """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): async def test_update_state_via_state_topic(hass, mqtt_mock):
"""Test updating with via state topic.""" """Test updating with via state topic."""
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG,
alarm_control_panel.DOMAIN,
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
}
},
) )
entity_id = "alarm_control_panel.test" 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): async def test_ignore_update_state_if_unknown_via_state_topic(hass, mqtt_mock):
"""Test ignoring updates via state topic.""" """Test ignoring updates via state topic."""
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG,
alarm_control_panel.DOMAIN,
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
}
},
) )
entity_id = "alarm_control_panel.test" 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): async def test_arm_home_publishes_mqtt(hass, mqtt_mock):
"""Test publishing of MQTT messages while armed.""" """Test publishing of MQTT messages while armed."""
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG,
alarm_control_panel.DOMAIN,
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
}
},
) )
await common.async_alarm_arm_home(hass) 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 When code_arm_required = True
""" """
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE,
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,
}
},
) )
call_count = mqtt_mock.async_publish.call_count 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 When code_arm_required = False
""" """
assert await async_setup_component( config = copy.deepcopy(DEFAULT_CONFIG_CODE)
hass, config[alarm_control_panel.DOMAIN]["code_arm_required"] = False
alarm_control_panel.DOMAIN, assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,)
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
"code": "1234",
"code_arm_required": False,
}
},
)
await common.async_alarm_arm_home(hass) await common.async_alarm_arm_home(hass)
mqtt_mock.async_publish.assert_called_once_with( 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): async def test_arm_away_publishes_mqtt(hass, mqtt_mock):
"""Test publishing of MQTT messages while armed.""" """Test publishing of MQTT messages while armed."""
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG,
alarm_control_panel.DOMAIN,
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
}
},
) )
await common.async_alarm_arm_away(hass) 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 When code_arm_required = True
""" """
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE,
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,
}
},
) )
call_count = mqtt_mock.async_publish.call_count 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 When code_arm_required = False
""" """
assert await async_setup_component( config = copy.deepcopy(DEFAULT_CONFIG_CODE)
hass, config[alarm_control_panel.DOMAIN]["code_arm_required"] = False
alarm_control_panel.DOMAIN, assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,)
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
"code": "1234",
"code_arm_required": False,
}
},
)
await common.async_alarm_arm_away(hass) await common.async_alarm_arm_away(hass)
mqtt_mock.async_publish.assert_called_once_with( 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): async def test_arm_night_publishes_mqtt(hass, mqtt_mock):
"""Test publishing of MQTT messages while armed.""" """Test publishing of MQTT messages while armed."""
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG,
alarm_control_panel.DOMAIN,
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
}
},
) )
await common.async_alarm_arm_night(hass) 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 When code_arm_required = True
""" """
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE,
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,
}
},
) )
call_count = mqtt_mock.async_publish.call_count 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 When code_arm_required = False
""" """
assert await async_setup_component( config = copy.deepcopy(DEFAULT_CONFIG_CODE)
hass, config[alarm_control_panel.DOMAIN]["code_arm_required"] = False
alarm_control_panel.DOMAIN, assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,)
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
"code": "1234",
"code_arm_required": False,
}
},
)
await common.async_alarm_arm_night(hass) await common.async_alarm_arm_night(hass)
mqtt_mock.async_publish.assert_called_once_with( 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): async def test_disarm_publishes_mqtt(hass, mqtt_mock):
"""Test publishing of MQTT messages while disarmed.""" """Test publishing of MQTT messages while disarmed."""
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG,
alarm_control_panel.DOMAIN,
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
}
},
) )
await common.async_alarm_disarm(hass) 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 When command_template set to output json
""" """
assert await async_setup_component( config = copy.deepcopy(DEFAULT_CONFIG_CODE)
hass, config[alarm_control_panel.DOMAIN]["code"] = "1234"
alarm_control_panel.DOMAIN, config[alarm_control_panel.DOMAIN]["command_template"] = (
{ '{"action":"{{ action }}",' '"code":"{{ code }}"}'
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
"code": "1234",
"command_template": '{"action":"{{ action }}",' '"code":"{{ code }}"}',
}
},
) )
assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,)
await common.async_alarm_disarm(hass, 1234) await common.async_alarm_disarm(hass, 1234)
mqtt_mock.async_publish.assert_called_once_with( 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 When code_disarm_required = False
""" """
assert await async_setup_component( config = copy.deepcopy(DEFAULT_CONFIG_CODE)
hass, config[alarm_control_panel.DOMAIN]["code"] = "1234"
alarm_control_panel.DOMAIN, config[alarm_control_panel.DOMAIN]["code_disarm_required"] = False
{ assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,)
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
"code": "1234",
"code_disarm_required": False,
}
},
)
await common.async_alarm_disarm(hass) await common.async_alarm_disarm(hass)
mqtt_mock.async_publish.assert_called_once_with("alarm/command", "DISARM", 0, False) 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 When code_disarm_required = True
""" """
assert await async_setup_component( assert await async_setup_component(
hass, hass, alarm_control_panel.DOMAIN, DEFAULT_CONFIG_CODE,
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,
}
},
) )
call_count = mqtt_mock.async_publish.call_count 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): async def test_default_availability_payload(hass, mqtt_mock):
"""Test availability by default payload with defined topic.""" """Test availability by default payload with defined topic."""
assert await async_setup_component( config = copy.deepcopy(DEFAULT_CONFIG_CODE)
hass, config[alarm_control_panel.DOMAIN]["availability_topic"] = "availability-topic"
alarm_control_panel.DOMAIN, assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,)
{
alarm_control_panel.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
"code": "1234",
"availability_topic": "availability-topic",
}
},
)
state = hass.states.get("alarm_control_panel.test") state = hass.states.get("alarm_control_panel.test")
assert state.state == STATE_UNAVAILABLE 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): async def test_custom_availability_payload(hass, mqtt_mock):
"""Test availability by custom payload with defined topic.""" """Test availability by custom payload with defined topic."""
assert await async_setup_component( config = copy.deepcopy(DEFAULT_CONFIG)
hass, config[alarm_control_panel.DOMAIN]["availability_topic"] = "availability-topic"
alarm_control_panel.DOMAIN, config[alarm_control_panel.DOMAIN]["payload_available"] = "good"
{ config[alarm_control_panel.DOMAIN]["payload_not_available"] = "nogood"
alarm_control_panel.DOMAIN: { assert await async_setup_component(hass, alarm_control_panel.DOMAIN, config,)
"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",
}
},
)
state = hass.states.get("alarm_control_panel.test") state = hass.states.get("alarm_control_panel.test")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
@ -496,22 +372,6 @@ async def test_custom_availability_payload(hass, mqtt_mock):
assert state.state == STATE_UNAVAILABLE 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): async def test_update_state_via_state_topic_template(hass, mqtt_mock):
"""Test updating with template_value via state topic.""" """Test updating with template_value via state topic."""
assert await async_setup_component( 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 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( config1 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN])
'{ "name": "Beer",' config2 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN])
' "command_topic": "test_topic",' config1["json_attributes_topic"] = "attr-topic1"
' "json_attributes_topic": "attr-topic1" }' config2["json_attributes_topic"] = "attr-topic2"
) data1 = json.dumps(config1)
data2 = ( data2 = json.dumps(config2)
'{ "name": "Beer",'
' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }'
)
await help_test_discovery_update_attr( await help_test_discovery_update_attr(
hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, data1, data2 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): async def test_discovery_removal_alarm(hass, mqtt_mock, caplog):
"""Test removal of discovered alarm_control_panel.""" """Test removal of discovered alarm_control_panel."""
data = ( data = json.dumps(DEFAULT_CONFIG[alarm_control_panel.DOMAIN])
'{ "name": "Beer",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)
await help_test_discovery_removal( await help_test_discovery_removal(
hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, data 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): async def test_discovery_update_alarm(hass, mqtt_mock, caplog):
"""Test update of discovered alarm_control_panel.""" """Test update of discovered alarm_control_panel."""
data1 = ( config1 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN])
'{ "name": "Beer",' config2 = copy.deepcopy(DEFAULT_CONFIG[alarm_control_panel.DOMAIN])
' "state_topic": "test_topic",' config1["name"] = "Beer"
' "command_topic": "test_topic" }' config2["name"] = "Milk"
)
data2 = ( data1 = json.dumps(config1)
'{ "name": "Milk",' data2 = json.dumps(config2)
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)
await help_test_discovery_update( await help_test_discovery_update(
hass, mqtt_mock, caplog, alarm_control_panel.DOMAIN, data1, data2 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT alarm control panel device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """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( 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
) )

View file

@ -1,4 +1,5 @@
"""The tests for the MQTT binary sensor platform.""" """The tests for the MQTT binary sensor platform."""
import copy
from datetime import datetime, timedelta from datetime import datetime, timedelta
import json import json
from unittest.mock import patch from unittest.mock import patch
@ -30,6 +31,38 @@ from .common import (
from tests.common import async_fire_mqtt_message, async_fire_time_changed 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): async def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock, caplog):
"""Test the expiration of the value.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( config1 = copy.deepcopy(DEFAULT_CONFIG_ATTR[binary_sensor.DOMAIN])
'{ "name": "Beer",' config2 = copy.deepcopy(DEFAULT_CONFIG_ATTR[binary_sensor.DOMAIN])
' "state_topic": "test_topic",' config1["json_attributes_topic"] = "attr-topic1"
' "json_attributes_topic": "attr-topic1" }' config2["json_attributes_topic"] = "attr-topic2"
) data1 = json.dumps(config1)
data2 = ( data2 = json.dumps(config2)
'{ "name": "Beer",'
' "state_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }'
)
await help_test_discovery_update_attr( await help_test_discovery_update_attr(
hass, mqtt_mock, caplog, binary_sensor.DOMAIN, data1, data2 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): async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog):
"""Test removal of discovered binary_sensor.""" """Test removal of discovered binary_sensor."""
data = ( data = json.dumps(DEFAULT_CONFIG[binary_sensor.DOMAIN])
'{ "name": "Beer",'
' "state_topic": "test_topic",'
' "availability_topic": "availability_topic" }'
)
await help_test_discovery_removal( await help_test_discovery_removal(
hass, mqtt_mock, caplog, binary_sensor.DOMAIN, data 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): async def test_discovery_update_binary_sensor(hass, mqtt_mock, caplog):
"""Test update of discovered binary_sensor.""" """Test update of discovered binary_sensor."""
data1 = ( config1 = copy.deepcopy(DEFAULT_CONFIG[binary_sensor.DOMAIN])
'{ "name": "Beer",' config2 = copy.deepcopy(DEFAULT_CONFIG[binary_sensor.DOMAIN])
' "state_topic": "test_topic",' config1["name"] = "Beer"
' "availability_topic": "availability_topic1" }' config2["name"] = "Milk"
)
data2 = ( data1 = json.dumps(config1)
'{ "name": "Milk",' data2 = json.dumps(config2)
' "state_topic": "test_topic2",'
' "availability_topic": "availability_topic2" }'
)
await help_test_discovery_update( await help_test_discovery_update(
hass, mqtt_mock, caplog, binary_sensor.DOMAIN, data1, data2 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT binary sensor device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """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( await help_test_entity_device_info_update(
hass, mqtt_mock, binary_sensor.DOMAIN, config hass, mqtt_mock, binary_sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
) )

View file

@ -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): async def test_setup_params(hass, mqtt_mock):
"""Test the initial parameters.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( config1 = copy.deepcopy(DEFAULT_CONFIG[CLIMATE_DOMAIN])
'{ "name": "Beer",' config2 = copy.deepcopy(DEFAULT_CONFIG[CLIMATE_DOMAIN])
' "power_state_topic": "test-topic",' config1["json_attributes_topic"] = "attr-topic1"
' "power_command_topic": "test_topic",' config2["json_attributes_topic"] = "attr-topic2"
' "json_attributes_topic": "attr-topic1" }' data1 = json.dumps(config1)
) data2 = json.dumps(config2)
data2 = (
'{ "name": "Beer",'
' "power_state_topic": "test-topic",'
' "power_command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }'
)
await help_test_discovery_update_attr( await help_test_discovery_update_attr(
hass, mqtt_mock, caplog, CLIMATE_DOMAIN, data1, data2 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): async def test_discovery_removal_climate(hass, mqtt_mock, caplog):
"""Test removal of discovered climate.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT climate device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, CLIMATE_DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -1,6 +1,4 @@
"""The tests for the MQTT cover platform.""" """The tests for the MQTT cover platform."""
import json
from homeassistant.components import cover from homeassistant.components import cover
from homeassistant.components.cover import ATTR_POSITION, ATTR_TILT_POSITION from homeassistant.components.cover import ATTR_POSITION, ATTR_TILT_POSITION
from homeassistant.components.mqtt.cover import MqttCover from homeassistant.components.mqtt.cover import MqttCover
@ -41,6 +39,31 @@ from .common import (
from tests.common import async_fire_mqtt_message 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): async def test_state_via_state_topic(hass, mqtt_mock):
"""Test the controlling state via topic.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "json_attributes_topic": "attr-topic2" }'
) )
await help_test_discovery_update_attr( await help_test_discovery_update_attr(
hass, mqtt_mock, caplog, cover.DOMAIN, data1, data2 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): async def test_discovery_removal_cover(hass, mqtt_mock, caplog):
"""Test removal of discovered cover.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT cover device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -1,6 +1,4 @@
"""Test MQTT fans.""" """Test MQTT fans."""
import json
from homeassistant.components import fan from homeassistant.components import fan
from homeassistant.const import ( from homeassistant.const import (
ATTR_ASSUMED_STATE, ATTR_ASSUMED_STATE,
@ -27,6 +25,31 @@ from .common import (
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.components.fan import common 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): async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
"""Test if command fails with command topic.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "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): async def test_discovery_removal_fan(hass, mqtt_mock, caplog):
"""Test removal of discovered fan.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT fan device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -63,6 +63,29 @@ DEFAULT_CONFIG = {
mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"], 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): async def test_default_supported_features(hass, mqtt_mock):
"""Test that the correct supported features.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "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): async def test_discovery_removal_vacuum(hass, mqtt_mock, caplog):
"""Test removal of discovered vacuum.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT vacuum device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -153,7 +153,6 @@ light:
payload_off: "off" payload_off: "off"
""" """
import json
from unittest import mock from unittest import mock
from unittest.mock import patch from unittest.mock import patch
@ -190,6 +189,31 @@ from tests.common import (
) )
from tests.components.light import common 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): async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
"""Test if command fails with command topic.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "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): async def test_discovery_removal_light(hass, mqtt_mock, caplog):
"""Test removal of discovered light.""" """Test removal of discovered light."""
data = ( data = (
'{ "name": "Beer",' '{ "name": "test",'
' "state_topic": "test_topic",' ' "state_topic": "test_topic",'
' "command_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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT light device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -120,6 +120,33 @@ from .common import (
from tests.common import MockConfigEntry, async_fire_mqtt_message, mock_coro from tests.common import MockConfigEntry, async_fire_mqtt_message, mock_coro
from tests.components.light import common 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): class JsonValidator(object):
"""Helper to compare JSON.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "schema": "json",' ' "schema": "json",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "schema": "json",' ' "schema": "json",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "json_attributes_topic": "attr-topic2" }'
@ -1013,7 +1013,7 @@ async def test_unique_id(hass):
async def test_discovery_removal(hass, mqtt_mock, caplog): async def test_discovery_removal(hass, mqtt_mock, caplog):
"""Test removal of discovered mqtt_json lights.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT light device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -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`. If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
""" """
import json
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components import light, mqtt from homeassistant.components import light, mqtt
@ -61,6 +60,37 @@ from tests.common import (
mock_coro, 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): async def test_setup_fails(hass, mqtt_mock):
"""Test that setup fails with missing required configuration items.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "schema": "template",' ' "schema": "template",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "command_on_template": "on",' ' "command_on_template": "on",'
@ -585,7 +582,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "schema": "template",' ' "schema": "template",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "command_on_template": "on",' ' "command_on_template": "on",'
@ -627,7 +624,7 @@ async def test_unique_id(hass):
async def test_discovery_removal(hass, mqtt_mock, caplog): async def test_discovery_removal(hass, mqtt_mock, caplog):
"""Test removal of discovered mqtt_json lights.""" """Test removal of discovered mqtt_json lights."""
data = ( data = (
'{ "name": "Beer",' '{ "name": "test",'
' "schema": "template",' ' "schema": "template",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "command_on_template": "on",' ' "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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT light device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -1,6 +1,4 @@
"""The tests for the MQTT lock platform.""" """The tests for the MQTT lock platform."""
import json
from homeassistant.components import lock from homeassistant.components import lock
from homeassistant.const import ( from homeassistant.const import (
ATTR_ASSUMED_STATE, ATTR_ASSUMED_STATE,
@ -27,6 +25,31 @@ from .common import (
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
from tests.components.lock import common 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): async def test_controlling_state_via_topic(hass, mqtt_mock):
"""Test the controlling state via topic.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "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): async def test_discovery_removal_lock(hass, mqtt_mock, caplog):
"""Test removal of discovered lock.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT lock device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -31,6 +31,30 @@ from tests.common import (
async_fire_time_changed, 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): async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
"""Test the setting of the value via MQTT.""" """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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "state_topic": "test_topic",' ' "state_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "state_topic": "test_topic",' ' "state_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "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): async def test_discovery_removal_sensor(hass, mqtt_mock, caplog):
"""Test removal of discovered sensor.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT sensor device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -63,6 +63,31 @@ DEFAULT_CONFIG = {
mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"], 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): async def test_default_supported_features(hass, mqtt_mock):
"""Test that the correct supported features.""" """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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_json(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "schema": "state",' ' "schema": "state",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "schema": "state",' ' "schema": "state",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "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): async def test_discovery_removal_vacuum(hass, mqtt_mock, caplog):
"""Test removal of discovered vacuum.""" """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) 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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT vacuum device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):

View file

@ -1,6 +1,4 @@
"""The tests for the MQTT switch platform.""" """The tests for the MQTT switch platform."""
import json
from asynctest import patch from asynctest import patch
import pytest 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.common import async_fire_mqtt_message, async_mock_mqtt_component, mock_coro
from tests.components.switch import common 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 @pytest.fixture
def mock_publish(hass): 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): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
"""Test the setting of attribute via MQTT with JSON payload.""" """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( 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): async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
"""Test attributes get extracted from a JSON result.""" """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( 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): async def test_discovery_update_attr(hass, mqtt_mock, caplog):
"""Test update of discovered MQTTAttributes.""" """Test update of discovered MQTTAttributes."""
data1 = ( data1 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }' ' "json_attributes_topic": "attr-topic1" }'
) )
data2 = ( data2 = (
'{ "name": "Beer",' '{ "name": "test",'
' "command_topic": "test_topic",' ' "command_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }' ' "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): async def test_discovery_removal_switch(hass, mqtt_mock, caplog):
"""Test removal of discovered switch.""" """Test removal of discovered switch."""
data = ( data = (
'{ "name": "Beer",' '{ "name": "test",'
' "state_topic": "test_topic",' ' "state_topic": "test_topic",'
' "command_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): async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT switch device registry integration.""" """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( 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): async def test_entity_device_info_update(hass, mqtt_mock):
"""Test device registry update.""" """Test device registry update."""
config = { await help_test_entity_device_info_update(
"platform": "mqtt", hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
"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)
async def test_entity_id_update(hass, mqtt_mock): async def test_entity_id_update(hass, mqtt_mock):