diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index c740086ba2f..801cb2997d4 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -80,6 +80,7 @@ CONF_CONNECTIONS = 'connections' CONF_MANUFACTURER = 'manufacturer' CONF_MODEL = 'model' CONF_SW_VERSION = 'sw_version' +CONF_VIA_HUB = 'via_hub' PROTOCOL_31 = '3.1' PROTOCOL_311 = '3.1.1' @@ -224,6 +225,7 @@ MQTT_ENTITY_DEVICE_INFO_SCHEMA = vol.All(vol.Schema({ vol.Optional(CONF_MODEL): cv.string, vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_SW_VERSION): cv.string, + vol.Optional(CONF_VIA_HUB): cv.string, }), validate_device_has_at_least_one_identifier) MQTT_JSON_ATTRS_SCHEMA = vol.Schema({ @@ -1032,4 +1034,7 @@ class MqttEntityDeviceInfo(Entity): if CONF_SW_VERSION in self._device_config: info['sw_version'] = self._device_config[CONF_SW_VERSION] + if CONF_VIA_HUB in self._device_config: + info['via_hub'] = (DOMAIN, self._device_config[CONF_VIA_HUB]) + return info diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 81e6a7b298d..6652eddd20b 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -231,6 +231,19 @@ class TestMQTTComponent(unittest.TestCase): 'model': 'Glass', 'sw_version': '0.1-beta', }) + # full device info with via_hub + mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({ + 'identifiers': ['helloworld', 'hello'], + 'connections': [ + ["mac", "02:5b:26:a8:dc:12"], + ["zigbee", "zigbee_id"], + ], + 'manufacturer': 'Whatever', + 'name': 'Beer', + 'model': 'Glass', + 'sw_version': '0.1-beta', + 'via_hub': 'test-hub', + }) # no identifiers with pytest.raises(vol.Invalid): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({ diff --git a/tests/components/sensor/test_mqtt.py b/tests/components/sensor/test_mqtt.py index 739e81258c2..f5eb8b23cf1 100644 --- a/tests/components/sensor/test_mqtt.py +++ b/tests/components/sensor/test_mqtt.py @@ -610,3 +610,36 @@ async def test_entity_id_update(hass, mqtt_mock): assert mock_mqtt.async_subscribe.call_count == 2 mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8') mock_mqtt.async_subscribe.assert_any_call('avty-topic', ANY, 0, 'utf-8') + + +async def test_entity_device_info_with_hub(hass, mqtt_mock): + """Test MQTT sensor device registry integration.""" + entry = MockConfigEntry(domain=mqtt.DOMAIN) + entry.add_to_hass(hass) + await async_start(hass, 'homeassistant', {}, entry) + + registry = await hass.helpers.device_registry.async_get_registry() + hub = registry.async_get_or_create( + config_entry_id='123', + connections=set(), + identifiers={('mqtt', 'hub-id')}, + manufacturer='manufacturer', model='hub' + ) + + data = json.dumps({ + 'platform': 'mqtt', + 'name': 'Test 1', + 'state_topic': 'test-topic', + 'device': { + 'identifiers': ['helloworld'], + 'via_hub': 'hub-id', + }, + 'unique_id': 'veryunique' + }) + async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', data) + await hass.async_block_till_done() + await hass.async_block_till_done() + + device = registry.async_get_device({('mqtt', 'helloworld')}, set()) + assert device is not None + assert device.hub_device_id == hub.id