Add device registry to MQTT light (#19013)

This commit is contained in:
emontnemery authored and Otto Winter committed 2018-12-13 15:51:50 +01:00
1 parent 6766d25e62
commit 7436c0fe42
6 files changed
+239 -16

No files matched your search

+69 -1
View File
@@ -87,6 +87,7 @@ light:
brightness: true
brightness_scale: 99
"""
import json
from unittest.mock import patch
from homeassistant.setup import async_setup_component
@@ -97,7 +98,9 @@ from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.discovery import async_start
import homeassistant.core as ha
from tests.common import mock_coro, async_fire_mqtt_message, MockConfigEntry
from tests.common import (
mock_coro, async_fire_mqtt_message, async_mock_mqtt_component,
MockConfigEntry)
async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
@@ -533,6 +536,31 @@ async def test_custom_availability_payload(hass, mqtt_mock):
assert STATE_UNAVAILABLE == state.state
async def test_unique_id(hass):
"""Test unique id option only creates one light per unique_id."""
await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, light.DOMAIN, {
light.DOMAIN: [{
'platform': 'mqtt',
'name': 'Test 1',
'schema': 'json',
'status_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'schema': 'json',
'status_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
async_fire_mqtt_message(hass, 'test-topic', 'payload')
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(light.DOMAIN)) == 1
async def test_discovery_removal(hass, mqtt_mock, caplog):
"""Test removal of discovered mqtt_json lights."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
@@ -609,3 +637,43 @@ async def test_discovery_update_light(hass, mqtt_mock, caplog):
assert state.name == 'Milk'
state = hass.states.get('light.milk')
assert state is None
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT light 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',
'schema': 'json',
'state_topic': 'test-topic',
'command_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/light/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'