Skip to content

Commit

Permalink
Add suggested_area to MQTT discovery (home-assistant#47903)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RadekHvizdos authored Mar 15, 2021
1 parent 07c1976 commit 9f4c2f6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions homeassistant/components/mqtt/abbreviations.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,5 @@
"mf": "manufacturer",
"mdl": "model",
"sw": "sw_version",
"sa": "suggested_area",
}
5 changes: 5 additions & 0 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
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(
{
Expand Down Expand Up @@ -129,6 +130,7 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType
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,
Expand Down Expand Up @@ -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


Expand Down
4 changes: 4 additions & 0 deletions tests/components/mqtt/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"name": "Beer",
"model": "Glass",
"sw_version": "0.1-beta",
"suggested_area": "default_area",
}

DEFAULT_CONFIG_DEVICE_INFO_MAC = {
Expand All @@ -28,6 +29,7 @@
"name": "Beer",
"model": "Glass",
"sw_version": "0.1-beta",
"suggested_area": "default_area",
}


Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion tests/components/mqtt/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
" }"
"}"
)
Expand Down

0 comments on commit 9f4c2f6

Please sign in to comment.