From eed9f674022cc61f876bcda134fbd881c31f862b Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 15 Oct 2021 03:27:40 +0200 Subject: [PATCH] Add service configuration URL to MQTT (#57731) --- homeassistant/components/mqtt/abbreviations.py | 1 + homeassistant/components/mqtt/mixins.py | 5 +++++ tests/components/mqtt/test_common.py | 4 ++++ tests/components/mqtt/test_init.py | 14 ++++++++++++++ 4 files changed, 24 insertions(+) diff --git a/homeassistant/components/mqtt/abbreviations.py b/homeassistant/components/mqtt/abbreviations.py index f3dcca9cfd5..fa7344f82c5 100644 --- a/homeassistant/components/mqtt/abbreviations.py +++ b/homeassistant/components/mqtt/abbreviations.py @@ -22,6 +22,7 @@ ABBREVIATIONS = { "clr_temp_cmd_tpl": "color_temp_command_template", "bat_lev_t": "battery_level_topic", "bat_lev_tpl": "battery_level_template", + "cu": "configuration_url", "chrg_t": "charging_topic", "chrg_tpl": "charging_template", "clrm": "color_mode", diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index 6c29938c75d..0ba699c6229 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -68,6 +68,7 @@ CONF_SW_VERSION = "sw_version" CONF_VIA_DEVICE = "via_device" CONF_DEPRECATED_VIA_HUB = "via_hub" CONF_SUGGESTED_AREA = "suggested_area" +CONF_CONFIGURATION_URL = "configuration_url" MQTT_ATTRIBUTES_BLOCKED = { "assumed_state", @@ -154,6 +155,7 @@ MQTT_ENTITY_DEVICE_INFO_SCHEMA = vol.All( vol.Optional(CONF_SW_VERSION): cv.string, vol.Optional(CONF_VIA_DEVICE): cv.string, vol.Optional(CONF_SUGGESTED_AREA): cv.string, + vol.Optional(CONF_CONFIGURATION_URL): cv.url, } ), validate_device_has_at_least_one_identifier, @@ -531,6 +533,9 @@ def device_info_from_config(config): if CONF_SUGGESTED_AREA in config: info["suggested_area"] = config[CONF_SUGGESTED_AREA] + if CONF_CONFIGURATION_URL in config: + info["configuration_url"] = config[CONF_CONFIGURATION_URL] + return info diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index c3a4022fdd8..0458ad14d51 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -22,6 +22,7 @@ DEFAULT_CONFIG_DEVICE_INFO_ID = { "model": "Glass", "sw_version": "0.1-beta", "suggested_area": "default_area", + "configuration_url": "http://example.com", } DEFAULT_CONFIG_DEVICE_INFO_MAC = { @@ -31,6 +32,7 @@ DEFAULT_CONFIG_DEVICE_INFO_MAC = { "model": "Glass", "sw_version": "0.1-beta", "suggested_area": "default_area", + "configuration_url": "http://example.com", } @@ -771,6 +773,7 @@ async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, assert device.model == "Glass" assert device.sw_version == "0.1-beta" assert device.suggested_area == "default_area" + assert device.configuration_url == "http://example.com" async def help_test_entity_device_info_with_connection(hass, mqtt_mock, domain, config): @@ -799,6 +802,7 @@ async def help_test_entity_device_info_with_connection(hass, mqtt_mock, domain, assert device.model == "Glass" assert device.sw_version == "0.1-beta" assert device.suggested_area == "default_area" + assert device.configuration_url == "http://example.com" async def help_test_entity_device_info_remove(hass, mqtt_mock, domain, config): diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index dfdd316cda9..7c6d482c7ec 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -301,6 +301,7 @@ def test_entity_device_info_schema(): "name": "Beer", "model": "Glass", "sw_version": "0.1-beta", + "configuration_url": "http://example.com", } ) # full device info with via_device @@ -316,6 +317,7 @@ def test_entity_device_info_schema(): "model": "Glass", "sw_version": "0.1-beta", "via_device": "test-hub", + "configuration_url": "http://example.com", } ) # no identifiers @@ -334,6 +336,18 @@ def test_entity_device_info_schema(): {"identifiers": [], "connections": [], "name": "Beer"} ) + # not an valid URL + with pytest.raises(vol.Invalid): + MQTT_ENTITY_DEVICE_INFO_SCHEMA( + { + "manufacturer": "Whatever", + "name": "Beer", + "model": "Glass", + "sw_version": "0.1-beta", + "configuration_url": "fake://link", + } + ) + async def test_receiving_non_utf8_message_gets_logged( hass, mqtt_mock, calls, record_calls, caplog