Add support for 'via_hub' for device_info (#19454)

* Add support for 'via_hub'

* Update config schema

* add domain to via_hub

* add tests for via_hub
This commit is contained in:
Fredrik Erlandsson 2019-01-11 22:11:13 +01:00 committed by Paulus Schoutsen
parent 199db7219e
commit b9a488912a
3 changed files with 51 additions and 0 deletions

View file

@ -80,6 +80,7 @@ CONF_CONNECTIONS = 'connections'
CONF_MANUFACTURER = 'manufacturer' CONF_MANUFACTURER = 'manufacturer'
CONF_MODEL = 'model' CONF_MODEL = 'model'
CONF_SW_VERSION = 'sw_version' CONF_SW_VERSION = 'sw_version'
CONF_VIA_HUB = 'via_hub'
PROTOCOL_31 = '3.1' PROTOCOL_31 = '3.1'
PROTOCOL_311 = '3.1.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_MODEL): cv.string,
vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_SW_VERSION): cv.string, vol.Optional(CONF_SW_VERSION): cv.string,
vol.Optional(CONF_VIA_HUB): cv.string,
}), validate_device_has_at_least_one_identifier) }), validate_device_has_at_least_one_identifier)
MQTT_JSON_ATTRS_SCHEMA = vol.Schema({ MQTT_JSON_ATTRS_SCHEMA = vol.Schema({
@ -1032,4 +1034,7 @@ class MqttEntityDeviceInfo(Entity):
if CONF_SW_VERSION in self._device_config: if CONF_SW_VERSION in self._device_config:
info['sw_version'] = self._device_config[CONF_SW_VERSION] 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 return info

View file

@ -231,6 +231,19 @@ class TestMQTTComponent(unittest.TestCase):
'model': 'Glass', 'model': 'Glass',
'sw_version': '0.1-beta', '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 # no identifiers
with pytest.raises(vol.Invalid): with pytest.raises(vol.Invalid):
mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({ mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({

View file

@ -610,3 +610,36 @@ async def test_entity_id_update(hass, mqtt_mock):
assert mock_mqtt.async_subscribe.call_count == 2 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('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('avty-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