From bba7a243abebbf7881b708c15854f3805ebc9582 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 28 Sep 2018 17:51:17 +0200 Subject: [PATCH 1/8] Implement base for MQTT device registry integration --- homeassistant/components/mqtt/__init__.py | 64 ++++++++++++++++++++++- homeassistant/components/sensor/mqtt.py | 12 +++-- homeassistant/helpers/device_registry.py | 1 + tests/components/mqtt/test_init.py | 49 +++++++++++++++++ tests/components/sensor/test_mqtt.py | 41 +++++++++++++++ 5 files changed, 161 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 335b4d31acb767..9f3d4e4e6bfc86 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -21,10 +21,11 @@ from homeassistant import config_entries from homeassistant.const import ( CONF_PASSWORD, CONF_PAYLOAD, CONF_PORT, CONF_PROTOCOL, CONF_USERNAME, - CONF_VALUE_TEMPLATE, EVENT_HOMEASSISTANT_STOP) + CONF_VALUE_TEMPLATE, EVENT_HOMEASSISTANT_STOP, CONF_TYPE, CONF_NAME, + CONF_DEVICE) from homeassistant.core import Event, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import config_validation as cv +from homeassistant.helpers import config_validation as cv, device_registry from homeassistant.helpers import template from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import ( @@ -73,6 +74,13 @@ CONF_QOS = 'qos' CONF_RETAIN = 'retain' +CONF_IDENTIFIERS = 'identifiers' +CONF_CONNECTIONS = 'connections' +CONF_IDENTIFIER = 'identifier' +CONF_MANUFACTURER = 'manufacturer' +CONF_MODEL = 'model' +CONF_SW_VERSION = 'sw_version' + PROTOCOL_31 = '3.1' PROTOCOL_311 = '3.1.1' @@ -144,6 +152,15 @@ def valid_publish_topic(value: Any) -> str: return value +def validate_device_has_at_least_one_identifier(value: ConfigType) -> \ + ConfigType: + """Validate that a device info entry has at least one identifying value.""" + if not value.get(CONF_IDENTIFIERS) and not value.get(CONF_CONNECTIONS): + raise vol.Invalid("Device must has at least one identifying value in" + "'identifiers' and/or 'connections'") + return value + + _VALID_QOS_SCHEMA = vol.All(vol.Coerce(int), vol.In([0, 1, 2])) CLIENT_KEY_AUTH_MSG = 'client_key and client_cert must both be present in ' \ @@ -198,6 +215,20 @@ def valid_publish_topic(value: Any) -> str: default=DEFAULT_PAYLOAD_NOT_AVAILABLE): cv.string, }) +MQTT_ENTITY_DEVICE_INFO_SCHEMA = vol.All(vol.Schema({ + vol.Optional(CONF_IDENTIFIERS, default=list): + vol.All(cv.ensure_list, [cv.string]), + vol.Optional(CONF_CONNECTIONS, default=list): + vol.All(cv.ensure_list, [vol.Schema({ + vol.Required(CONF_TYPE): vol.Any(*device_registry.CONNECTION_KEYS), + vol.Required(CONF_IDENTIFIER): cv.string, + })]), + vol.Optional(CONF_MANUFACTURER): cv.string, + vol.Optional(CONF_MODEL): cv.string, + vol.Optional(CONF_NAME): cv.string, + vol.Optional(CONF_SW_VERSION): cv.string, +}), validate_device_has_at_least_one_identifier) + MQTT_BASE_PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(SCHEMA_BASE) # Sensor type platforms subscribe to MQTT events @@ -868,3 +899,32 @@ def discovery_callback(payload): self.hass, MQTT_DISCOVERY_UPDATED.format(self._discovery_hash), discovery_callback) + + +class MqttEntityDeviceInfo(Entity): + """Mixin used for mqtt platforms that support the device registry.""" + + def __init__(self, device_config: Optional[ConfigType]) -> None: + """Initialize the device mixin.""" + self._device_config = device_config + + @property + def device_info(self): + """Return a device description for device registry.""" + if not self._device_config: + return None + + return { + 'identifiers': { + (DOMAIN, id_) + for id_ in self._device_config[CONF_IDENTIFIERS] + }, + 'connections': { + (connection[CONF_TYPE], connection[CONF_IDENTIFIER]) + for connection in self._device_config[CONF_CONNECTIONS] + }, + 'manufacturer': self._device_config.get(CONF_MANUFACTURER), + 'model': self._device_config.get(CONF_MODEL), + 'name': self._device_config.get(CONF_NAME), + 'sw_version': self._device_config.get(CONF_SW_VERSION), + } diff --git a/homeassistant/components/sensor/mqtt.py b/homeassistant/components/sensor/mqtt.py index fe0b77b2024b96..225ed07a6227a7 100644 --- a/homeassistant/components/sensor/mqtt.py +++ b/homeassistant/components/sensor/mqtt.py @@ -16,12 +16,12 @@ from homeassistant.components.mqtt import ( ATTR_DISCOVERY_HASH, CONF_AVAILABILITY_TOPIC, CONF_STATE_TOPIC, CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, - MqttAvailability, MqttDiscoveryUpdate) + MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo) from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW from homeassistant.components.sensor import DEVICE_CLASSES_SCHEMA from homeassistant.const import ( CONF_FORCE_UPDATE, CONF_NAME, CONF_VALUE_TEMPLATE, STATE_UNKNOWN, - CONF_UNIT_OF_MEASUREMENT, CONF_ICON, CONF_DEVICE_CLASS) + CONF_UNIT_OF_MEASUREMENT, CONF_ICON, CONF_DEVICE_CLASS, CONF_DEVICE) from homeassistant.helpers.entity import Entity from homeassistant.components import mqtt import homeassistant.helpers.config_validation as cv @@ -51,6 +51,7 @@ # Integrations shouldn't never expose unique_id through configuration # this here is an exception because MQTT is a msg transport, not a protocol vol.Optional(CONF_UNIQUE_ID): cv.string, + vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA, }).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema) @@ -95,22 +96,25 @@ async def _async_setup_entity(hass: HomeAssistantType, config: ConfigType, config.get(CONF_AVAILABILITY_TOPIC), config.get(CONF_PAYLOAD_AVAILABLE), config.get(CONF_PAYLOAD_NOT_AVAILABLE), + config.get(CONF_DEVICE), discovery_hash, )]) -class MqttSensor(MqttAvailability, MqttDiscoveryUpdate, Entity): +class MqttSensor(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + Entity): """Representation of a sensor that can be updated using MQTT.""" def __init__(self, name, state_topic, qos, unit_of_measurement, force_update, expire_after, icon, device_class: Optional[str], value_template, json_attributes, unique_id: Optional[str], availability_topic, payload_available, payload_not_available, - discovery_hash): + device_config: Optional[ConfigType], discovery_hash): """Initialize the sensor.""" MqttAvailability.__init__(self, availability_topic, qos, payload_available, payload_not_available) MqttDiscoveryUpdate.__init__(self, discovery_hash) + MqttEntityDeviceInfo.__init__(self, device_config) self._state = STATE_UNKNOWN self._name = name self._state_topic = state_topic diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 478b29c75b2d81..a6b6b41c295a80 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -20,6 +20,7 @@ CONNECTION_NETWORK_MAC = 'mac' CONNECTION_ZIGBEE = 'zigbee' +CONNECTION_KEYS = [CONNECTION_NETWORK_MAC, CONNECTION_ZIGBEE] @attr.s(slots=True, frozen=True) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 831bcaa1d24f1f..a8696a4d16fbd7 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -190,6 +190,55 @@ def test_validate_publish_topic(self): # 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'] + }) + # just connection + mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({ + 'connections': [{ + 'type': 'mac', + 'identifier': '02:5b:26:a8:dc:12' + }] + }) + # full device info + mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({ + 'identifiers': ['helloworld', 'hello'], + 'connections': [{ + "type": "mac", + "identifier": "02:5b:26:a8:dc:12", + }, { + "type": "zigbee", + "identifier": "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', + }) + # invalid connection type + self.assertRaises(vol.Invalid, mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA, { + 'connections': [{ + 'type': 'invalid', + 'identifier': 'empty' + }] + }) + # pylint: disable=invalid-name class TestMQTTCallbacks(unittest.TestCase): diff --git a/tests/components/sensor/test_mqtt.py b/tests/components/sensor/test_mqtt.py index 4f70c37e04f6fb..0d1489a759e902 100644 --- a/tests/components/sensor/test_mqtt.py +++ b/tests/components/sensor/test_mqtt.py @@ -1,4 +1,5 @@ """The tests for the MQTT sensor platform.""" +import json import unittest from datetime import timedelta, datetime @@ -411,3 +412,43 @@ 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': [{ + "type": "mac", + "identifier": "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' + From 0c61e189cee8940ec0d31b114aed6e9ee6faca9c Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 28 Sep 2018 18:08:22 +0200 Subject: [PATCH 2/8] Lint --- tests/components/sensor/test_mqtt.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/components/sensor/test_mqtt.py b/tests/components/sensor/test_mqtt.py index 0d1489a759e902..06f376cb184435 100644 --- a/tests/components/sensor/test_mqtt.py +++ b/tests/components/sensor/test_mqtt.py @@ -451,4 +451,3 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): assert device.name == 'Beer' assert device.model == 'Glass' assert device.sw_version == '0.1-beta' - From 74f7df117a8cf2336c3e15688805985c3f2703b6 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 28 Sep 2018 20:32:30 +0200 Subject: [PATCH 3/8] Lint --- homeassistant/components/mqtt/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 9f3d4e4e6bfc86..2cc5ceff04324a 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -21,8 +21,7 @@ from homeassistant import config_entries from homeassistant.const import ( CONF_PASSWORD, CONF_PAYLOAD, CONF_PORT, CONF_PROTOCOL, CONF_USERNAME, - CONF_VALUE_TEMPLATE, EVENT_HOMEASSISTANT_STOP, CONF_TYPE, CONF_NAME, - CONF_DEVICE) + CONF_VALUE_TEMPLATE, EVENT_HOMEASSISTANT_STOP, CONF_TYPE, CONF_NAME) from homeassistant.core import Event, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_validation as cv, device_registry From 1489a9263a2b60169f73abceb17c6fc63c7f341e Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 4 Oct 2018 17:52:10 +0200 Subject: [PATCH 4/8] Address comments --- homeassistant/components/mqtt/__init__.py | 12 ++++++------ homeassistant/helpers/device_registry.py | 1 - tests/components/mqtt/test_init.py | 20 ++++++-------------- tests/components/sensor/test_mqtt.py | 3 +-- 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 2cc5ceff04324a..e9b3632e70840c 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -219,8 +219,7 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> \ vol.All(cv.ensure_list, [cv.string]), vol.Optional(CONF_CONNECTIONS, default=list): vol.All(cv.ensure_list, [vol.Schema({ - vol.Required(CONF_TYPE): vol.Any(*device_registry.CONNECTION_KEYS), - vol.Required(CONF_IDENTIFIER): cv.string, + cv.string: cv.string, })]), vol.Optional(CONF_MANUFACTURER): cv.string, vol.Optional(CONF_MODEL): cv.string, @@ -913,15 +912,16 @@ def device_info(self): if not self._device_config: return None + connections = set() + for conn in self._device_config[CONF_CONNECTIONS]: + connections |= set(conn.items()) + return { 'identifiers': { (DOMAIN, id_) for id_ in self._device_config[CONF_IDENTIFIERS] }, - 'connections': { - (connection[CONF_TYPE], connection[CONF_IDENTIFIER]) - for connection in self._device_config[CONF_CONNECTIONS] - }, + 'connections': connections, 'manufacturer': self._device_config.get(CONF_MANUFACTURER), 'model': self._device_config.get(CONF_MODEL), 'name': self._device_config.get(CONF_NAME), diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index a6b6b41c295a80..478b29c75b2d81 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -20,7 +20,6 @@ CONNECTION_NETWORK_MAC = 'mac' CONNECTION_ZIGBEE = 'zigbee' -CONNECTION_KEYS = [CONNECTION_NETWORK_MAC, CONNECTION_ZIGBEE] @attr.s(slots=True, frozen=True) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index a8696a4d16fbd7..f04533ef8c262f 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -196,22 +196,21 @@ def test_entity_device_info_schema(self): 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': [{ - 'type': 'mac', - 'identifier': '02:5b:26:a8:dc:12' + 'mac': '02:5b:26:a8:dc:12', }] }) # full device info mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({ 'identifiers': ['helloworld', 'hello'], 'connections': [{ - "type": "mac", - "identifier": "02:5b:26:a8:dc:12", - }, { - "type": "zigbee", - "identifier": "zigbee_id" + "mac": "02:5b:26:a8:dc:12", + "zigbee": "zigbee_id", }], 'manufacturer': 'Whatever', 'name': 'Beer', @@ -231,13 +230,6 @@ def test_entity_device_info_schema(self): 'connections': [], 'name': 'Beer', }) - # invalid connection type - self.assertRaises(vol.Invalid, mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA, { - 'connections': [{ - 'type': 'invalid', - 'identifier': 'empty' - }] - }) # pylint: disable=invalid-name diff --git a/tests/components/sensor/test_mqtt.py b/tests/components/sensor/test_mqtt.py index 06f376cb184435..7bbacd3ae3e7b5 100644 --- a/tests/components/sensor/test_mqtt.py +++ b/tests/components/sensor/test_mqtt.py @@ -428,8 +428,7 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): 'device': { 'identifiers': ['helloworld'], 'connections': [{ - "type": "mac", - "identifier": "02:5b:26:a8:dc:12", + "mac": "02:5b:26:a8:dc:12", }], 'manufacturer': 'Whatever', 'name': 'Beer', From 2f717eda0bf9f997b764ea709c88e2a7bf340af3 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 4 Oct 2018 17:54:28 +0200 Subject: [PATCH 5/8] Lint --- homeassistant/components/mqtt/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index e9b3632e70840c..8c8af1d4c57ad3 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -21,10 +21,10 @@ from homeassistant import config_entries from homeassistant.const import ( CONF_PASSWORD, CONF_PAYLOAD, CONF_PORT, CONF_PROTOCOL, CONF_USERNAME, - CONF_VALUE_TEMPLATE, EVENT_HOMEASSISTANT_STOP, CONF_TYPE, CONF_NAME) + CONF_VALUE_TEMPLATE, EVENT_HOMEASSISTANT_STOP, CONF_NAME) from homeassistant.core import Event, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import config_validation as cv, device_registry +from homeassistant.helpers import config_validation as cv from homeassistant.helpers import template from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import ( From 1644b293b9eeb74a43ff2f6a3639477fb98db156 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 4 Oct 2018 17:56:08 +0200 Subject: [PATCH 6/8] Lint --- homeassistant/components/mqtt/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 8c8af1d4c57ad3..c5e9427f203caa 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -75,7 +75,6 @@ CONF_IDENTIFIERS = 'identifiers' CONF_CONNECTIONS = 'connections' -CONF_IDENTIFIER = 'identifier' CONF_MANUFACTURER = 'manufacturer' CONF_MODEL = 'model' CONF_SW_VERSION = 'sw_version' From 2ad629111a03530a245ce737f91d6b032520511a Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 5 Oct 2018 12:17:23 +0200 Subject: [PATCH 7/8] Address comments --- homeassistant/components/mqtt/__init__.py | 14 +++++--------- tests/components/mqtt/test_init.py | 14 +++++++------- tests/components/sensor/test_mqtt.py | 6 +++--- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index c5e9427f203caa..5c8e4fb0fca7ff 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -154,7 +154,7 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> \ ConfigType: """Validate that a device info entry has at least one identifying value.""" if not value.get(CONF_IDENTIFIERS) and not value.get(CONF_CONNECTIONS): - raise vol.Invalid("Device must has at least one identifying value in" + raise vol.Invalid("Device must have at least one identifying value in " "'identifiers' and/or 'connections'") return value @@ -217,9 +217,7 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> \ vol.Optional(CONF_IDENTIFIERS, default=list): vol.All(cv.ensure_list, [cv.string]), vol.Optional(CONF_CONNECTIONS, default=list): - vol.All(cv.ensure_list, [vol.Schema({ - cv.string: cv.string, - })]), + vol.All(cv.ensure_list, [vol.All(vol.Length(2), [cv.string])]), vol.Optional(CONF_MANUFACTURER): cv.string, vol.Optional(CONF_MODEL): cv.string, vol.Optional(CONF_NAME): cv.string, @@ -911,16 +909,14 @@ def device_info(self): if not self._device_config: return None - connections = set() - for conn in self._device_config[CONF_CONNECTIONS]: - connections |= set(conn.items()) - return { 'identifiers': { (DOMAIN, id_) for id_ in self._device_config[CONF_IDENTIFIERS] }, - 'connections': connections, + 'connections': { + tuple(x) for x in self._device_config[CONF_CONNECTIONS] + }, 'manufacturer': self._device_config.get(CONF_MANUFACTURER), 'model': self._device_config.get(CONF_MODEL), 'name': self._device_config.get(CONF_NAME), diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index f04533ef8c262f..045a411a271c67 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -201,17 +201,17 @@ def test_entity_device_info_schema(self): }) # just connection mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA({ - 'connections': [{ - 'mac': '02:5b:26:a8:dc:12', - }] + '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", - }], + 'connections': [ + ["mac", "02:5b:26:a8:dc:12"], + ["zigbee", "zigbee_id"], + ], 'manufacturer': 'Whatever', 'name': 'Beer', 'model': 'Glass', diff --git a/tests/components/sensor/test_mqtt.py b/tests/components/sensor/test_mqtt.py index 7bbacd3ae3e7b5..873de5a9bd68b1 100644 --- a/tests/components/sensor/test_mqtt.py +++ b/tests/components/sensor/test_mqtt.py @@ -427,9 +427,9 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): 'state_topic': 'test-topic', 'device': { 'identifiers': ['helloworld'], - 'connections': [{ - "mac": "02:5b:26:a8:dc:12", - }], + 'connections': [ + ["mac", "02:5b:26:a8:dc:12"], + ], 'manufacturer': 'Whatever', 'name': 'Beer', 'model': 'Glass', From c1fea4990b62c05715fdc7fe889b1087c15efa55 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 8 Oct 2018 10:58:59 +0200 Subject: [PATCH 8/8] Only add keys if specified See https://github.com/home-assistant/home-assistant/pull/17136#discussion_r223267185 --- homeassistant/components/mqtt/__init__.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 5c8e4fb0fca7ff..b2ee0a68336233 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -909,16 +909,26 @@ def device_info(self): if not self._device_config: return None - return { + info = { 'identifiers': { (DOMAIN, id_) for id_ in self._device_config[CONF_IDENTIFIERS] }, 'connections': { tuple(x) for x in self._device_config[CONF_CONNECTIONS] - }, - 'manufacturer': self._device_config.get(CONF_MANUFACTURER), - 'model': self._device_config.get(CONF_MODEL), - 'name': self._device_config.get(CONF_NAME), - 'sw_version': self._device_config.get(CONF_SW_VERSION), + } } + + if CONF_MANUFACTURER in self._device_config: + info['manufacturer'] = self._device_config[CONF_MANUFACTURER] + + if CONF_MODEL in self._device_config: + info['model'] = self._device_config[CONF_MODEL] + + if CONF_NAME in self._device_config: + info['name'] = self._device_config[CONF_NAME] + + if CONF_SW_VERSION in self._device_config: + info['sw_version'] = self._device_config[CONF_SW_VERSION] + + return info