Implement base for MQTT device registry integration (#16943)

* Implement base for MQTT device registry integration

* Lint

* Lint

* Address comments

* Lint

* Lint

* Address comments

* Only add keys if specified

See https://github.com/home-assistant/home-assistant/pull/17136#discussion_r223267185
This commit is contained in:
Otto Winter
2018-10-08 12:53:30 +02:00
committed by Paulus Schoutsen
parent 71a0274b12
commit af2402ea59
4 changed files with 153 additions and 5 deletions
+41
View File
@@ -190,6 +190,47 @@ class TestMQTTComponent(unittest.TestCase):
# Topic names beginning with $ SHOULD NOT be used, but can
mqtt.valid_publish_topic('$SYS/')
def test_entity_device_info_schema(self):
"""Test MQTT entity device info validation."""
# just identifier
mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({
'identifiers': ['abcd']
})
mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({
'identifiers': 'abcd'
})
# just connection
mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({
'connections': [
['mac', '02:5b:26:a8:dc:12'],
]
})
# full device info
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',
})
# no identifiers
self.assertRaises(vol.Invalid, mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA, {
'manufacturer': 'Whatever',
'name': 'Beer',
'model': 'Glass',
'sw_version': '0.1-beta',
})
# empty identifiers
self.assertRaises(vol.Invalid, mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA, {
'identifiers': [],
'connections': [],
'name': 'Beer',
})
# pylint: disable=invalid-name
class TestMQTTCallbacks(unittest.TestCase):
+39
View File
@@ -1,4 +1,5 @@
"""The tests for the MQTT sensor platform."""
import json
import unittest
from datetime import timedelta, datetime
@@ -411,3 +412,41 @@ async def test_discovery_removal_sensor(hass, mqtt_mock, caplog):
await hass.async_block_till_done()
state = hass.states.get('sensor.beer')
assert state is None
async def test_entity_device_info_with_identifier(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()
data = json.dumps({
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'device': {
'identifiers': ['helloworld'],
'connections': [
["mac", "02:5b:26:a8:dc:12"],
],
'manufacturer': 'Whatever',
'name': 'Beer',
'model': 'Glass',
'sw_version': '0.1-beta',
},
'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.identifiers == {('mqtt', 'helloworld')}
assert device.connections == {('mac', "02:5b:26:a8:dc:12")}
assert device.manufacturer == 'Whatever'
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'