From 9f4c2f6260ad73c01f058b35503e39ea10c630b6 Mon Sep 17 00:00:00 2001 From: RadekHvizdos <10856567+RadekHvizdos@users.noreply.github.com> Date: Mon, 15 Mar 2021 20:02:02 +0100 Subject: [PATCH] Add suggested_area to MQTT discovery (#47903) * Add suggested_area to MQTT Discovery This adds suggested_area to MQTT discovery, so that the discovered devices could be automatically added to the proper area. * Add abbreviation for MQTT suggested_area * Remove extra whitespace * Remove extra whitespace #2 * Added tests for MQTT Dicovery of suggested_area * Fix test for MQTT suggested_area * Better tests of MQTT suggested_area Changes made as per feedback from @emontnemery --- homeassistant/components/mqtt/abbreviations.py | 1 + homeassistant/components/mqtt/mixins.py | 5 +++++ tests/components/mqtt/test_common.py | 4 ++++ tests/components/mqtt/test_discovery.py | 3 ++- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mqtt/abbreviations.py b/homeassistant/components/mqtt/abbreviations.py index 8868d487f93..868e2fdd791 100644 --- a/homeassistant/components/mqtt/abbreviations.py +++ b/homeassistant/components/mqtt/abbreviations.py @@ -203,4 +203,5 @@ DEVICE_ABBREVIATIONS = { "mf": "manufacturer", "mdl": "model", "sw": "sw_version", + "sa": "suggested_area", } diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index 898072de5f9..5737dad255c 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -63,6 +63,7 @@ CONF_MODEL = "model" CONF_SW_VERSION = "sw_version" CONF_VIA_DEVICE = "via_device" CONF_DEPRECATED_VIA_HUB = "via_hub" +CONF_SUGGESTED_AREA = "suggested_area" MQTT_AVAILABILITY_SINGLE_SCHEMA = vol.Schema( { @@ -129,6 +130,7 @@ MQTT_ENTITY_DEVICE_INFO_SCHEMA = vol.All( vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_SW_VERSION): cv.string, vol.Optional(CONF_VIA_DEVICE): cv.string, + vol.Optional(CONF_SUGGESTED_AREA): cv.string, } ), validate_device_has_at_least_one_identifier, @@ -491,6 +493,9 @@ def device_info_from_config(config): if CONF_VIA_DEVICE in config: info["via_device"] = (DOMAIN, config[CONF_VIA_DEVICE]) + if CONF_SUGGESTED_AREA in config: + info["suggested_area"] = config[CONF_SUGGESTED_AREA] + return info diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index f1243918e4e..43f27373a3e 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -20,6 +20,7 @@ DEFAULT_CONFIG_DEVICE_INFO_ID = { "name": "Beer", "model": "Glass", "sw_version": "0.1-beta", + "suggested_area": "default_area", } DEFAULT_CONFIG_DEVICE_INFO_MAC = { @@ -28,6 +29,7 @@ DEFAULT_CONFIG_DEVICE_INFO_MAC = { "name": "Beer", "model": "Glass", "sw_version": "0.1-beta", + "suggested_area": "default_area", } @@ -739,6 +741,7 @@ async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, assert device.name == "Beer" assert device.model == "Glass" assert device.sw_version == "0.1-beta" + assert device.suggested_area == "default_area" async def help_test_entity_device_info_with_connection(hass, mqtt_mock, domain, config): @@ -764,6 +767,7 @@ async def help_test_entity_device_info_with_connection(hass, mqtt_mock, domain, assert device.name == "Beer" assert device.model == "Glass" assert device.sw_version == "0.1-beta" + assert device.suggested_area == "default_area" async def help_test_entity_device_info_remove(hass, mqtt_mock, domain, config): diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index fed0dfa54d6..d55c8e0eccc 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -442,7 +442,8 @@ async def test_discovery_expansion(hass, mqtt_mock, caplog): ' "name":"DiscoveryExpansionTest1 Device",' ' "mdl":"Generic",' ' "sw":"1.2.3.4",' - ' "mf":"None"' + ' "mf":"None",' + ' "sa":"default_area"' " }" "}" )