Skip to content

Commit 9f4c2f6

Browse files
authored
Add suggested_area to MQTT discovery (home-assistant#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
1 parent 07c1976 commit 9f4c2f6

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

homeassistant/components/mqtt/abbreviations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,5 @@
203203
"mf": "manufacturer",
204204
"mdl": "model",
205205
"sw": "sw_version",
206+
"sa": "suggested_area",
206207
}

homeassistant/components/mqtt/mixins.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
CONF_SW_VERSION = "sw_version"
6464
CONF_VIA_DEVICE = "via_device"
6565
CONF_DEPRECATED_VIA_HUB = "via_hub"
66+
CONF_SUGGESTED_AREA = "suggested_area"
6667

6768
MQTT_AVAILABILITY_SINGLE_SCHEMA = vol.Schema(
6869
{
@@ -129,6 +130,7 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType
129130
vol.Optional(CONF_NAME): cv.string,
130131
vol.Optional(CONF_SW_VERSION): cv.string,
131132
vol.Optional(CONF_VIA_DEVICE): cv.string,
133+
vol.Optional(CONF_SUGGESTED_AREA): cv.string,
132134
}
133135
),
134136
validate_device_has_at_least_one_identifier,
@@ -491,6 +493,9 @@ def device_info_from_config(config):
491493
if CONF_VIA_DEVICE in config:
492494
info["via_device"] = (DOMAIN, config[CONF_VIA_DEVICE])
493495

496+
if CONF_SUGGESTED_AREA in config:
497+
info["suggested_area"] = config[CONF_SUGGESTED_AREA]
498+
494499
return info
495500

496501

tests/components/mqtt/test_common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"name": "Beer",
2121
"model": "Glass",
2222
"sw_version": "0.1-beta",
23+
"suggested_area": "default_area",
2324
}
2425

2526
DEFAULT_CONFIG_DEVICE_INFO_MAC = {
@@ -28,6 +29,7 @@
2829
"name": "Beer",
2930
"model": "Glass",
3031
"sw_version": "0.1-beta",
32+
"suggested_area": "default_area",
3133
}
3234

3335

@@ -739,6 +741,7 @@ async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain,
739741
assert device.name == "Beer"
740742
assert device.model == "Glass"
741743
assert device.sw_version == "0.1-beta"
744+
assert device.suggested_area == "default_area"
742745

743746

744747
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,
764767
assert device.name == "Beer"
765768
assert device.model == "Glass"
766769
assert device.sw_version == "0.1-beta"
770+
assert device.suggested_area == "default_area"
767771

768772

769773
async def help_test_entity_device_info_remove(hass, mqtt_mock, domain, config):

tests/components/mqtt/test_discovery.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ async def test_discovery_expansion(hass, mqtt_mock, caplog):
442442
' "name":"DiscoveryExpansionTest1 Device",'
443443
' "mdl":"Generic",'
444444
' "sw":"1.2.3.4",'
445-
' "mf":"None"'
445+
' "mf":"None",'
446+
' "sa":"default_area"'
446447
" }"
447448
"}"
448449
)

0 commit comments

Comments
 (0)