Add device registry to MQTT light (#19013)
This commit is contained in:
1 parent
6766d25e62
commit
7436c0fe42
6 files changed
+239
-16
No files matched your search
@@ -153,6 +153,7 @@ light:
|
||||
payload_off: "off"
|
||||
|
||||
"""
|
||||
import json
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -165,7 +166,7 @@ import homeassistant.core as ha
|
||||
|
||||
from tests.common import (
|
||||
assert_setup_component, async_fire_mqtt_message,
|
||||
mock_coro, MockConfigEntry)
|
||||
async_mock_mqtt_component, mock_coro, MockConfigEntry)
|
||||
from tests.components.light import common
|
||||
|
||||
|
||||
@@ -1038,6 +1039,29 @@ 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',
|
||||
'status_topic': 'test-topic',
|
||||
'command_topic': 'test_topic',
|
||||
'unique_id': 'TOTALLY_UNIQUE'
|
||||
}, {
|
||||
'platform': 'mqtt',
|
||||
'name': 'Test 2',
|
||||
'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_light(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered light."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
@@ -1117,3 +1141,42 @@ 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',
|
||||
'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'
|
||||
@@ -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'
|
||||
@@ -26,6 +26,7 @@ If your light doesn't support white value feature, omit `white_value_template`.
|
||||
|
||||
If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
|
||||
"""
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -37,7 +38,7 @@ import homeassistant.core as ha
|
||||
|
||||
from tests.common import (
|
||||
async_fire_mqtt_message, assert_setup_component, mock_coro,
|
||||
MockConfigEntry)
|
||||
async_mock_mqtt_component, MockConfigEntry)
|
||||
|
||||
|
||||
async def test_setup_fails(hass, mqtt_mock):
|
||||
@@ -484,6 +485,33 @@ 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': 'template',
|
||||
'status_topic': 'test-topic',
|
||||
'command_topic': 'test_topic',
|
||||
'command_on_template': 'on,{{ transition }}',
|
||||
'command_off_template': 'off,{{ transition|d }}',
|
||||
'unique_id': 'TOTALLY_UNIQUE'
|
||||
}, {
|
||||
'platform': 'mqtt',
|
||||
'name': 'Test 2',
|
||||
'schema': 'template',
|
||||
'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(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered mqtt_json lights."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
@@ -562,3 +590,45 @@ 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': 'template',
|
||||
'state_topic': 'test-topic',
|
||||
'command_topic': 'test-topic',
|
||||
'command_on_template': 'on,{{ transition }}',
|
||||
'command_off_template': 'off,{{ transition|d }}',
|
||||
'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'
|
||||
Reference in new issue
Block a user