From 3ee3acd5509e52995e3296235d8a92a8fefec7a2 Mon Sep 17 00:00:00 2001 From: emontnemery Date: Tue, 29 Jan 2019 04:45:34 +0100 Subject: [PATCH] Update device registry of MQTT light (#20441) * Update device registry of MQTT light * Move config_entry to constructor --- .../components/mqtt/light/__init__.py | 6 +-- .../components/mqtt/light/schema_basic.py | 9 ++-- .../components/mqtt/light/schema_json.py | 11 ++-- .../components/mqtt/light/schema_template.py | 9 ++-- tests/components/mqtt/test_light.py | 47 +++++++++++++++++ tests/components/mqtt/test_light_json.py | 48 ++++++++++++++++++ tests/components/mqtt/test_light_template.py | 50 +++++++++++++++++++ 7 files changed, 164 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/mqtt/light/__init__.py b/homeassistant/components/mqtt/light/__init__.py index 77a1b1d3c10..908ad1ac989 100644 --- a/homeassistant/components/mqtt/light/__init__.py +++ b/homeassistant/components/mqtt/light/__init__.py @@ -55,7 +55,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): try: discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH] config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity(config, async_add_entities, + await _async_setup_entity(config, async_add_entities, config_entry, discovery_hash) except Exception: if discovery_hash: @@ -67,7 +67,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_discover) -async def _async_setup_entity(config, async_add_entities, +async def _async_setup_entity(config, async_add_entities, config_entry=None, discovery_hash=None): """Set up a MQTT Light.""" setup_entity = { @@ -76,4 +76,4 @@ async def _async_setup_entity(config, async_add_entities, 'template': schema_template.async_setup_entity_template, } await setup_entity[config[CONF_SCHEMA]]( - config, async_add_entities, discovery_hash) + config, async_add_entities, config_entry, discovery_hash) diff --git a/homeassistant/components/mqtt/light/schema_basic.py b/homeassistant/components/mqtt/light/schema_basic.py index c4bf41fa673..0ba1db890d7 100644 --- a/homeassistant/components/mqtt/light/schema_basic.py +++ b/homeassistant/components/mqtt/light/schema_basic.py @@ -111,13 +111,13 @@ PLATFORM_SCHEMA_BASIC = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ mqtt.MQTT_JSON_ATTRS_SCHEMA.schema) -async def async_setup_entity_basic(config, async_add_entities, +async def async_setup_entity_basic(config, async_add_entities, config_entry, discovery_hash=None): """Set up a MQTT Light.""" config.setdefault( CONF_STATE_VALUE_TEMPLATE, config.get(CONF_VALUE_TEMPLATE)) - async_add_entities([MqttLight(config, discovery_hash)]) + async_add_entities([MqttLight(config, config_entry, discovery_hash)]) # pylint: disable=too-many-ancestors @@ -125,7 +125,7 @@ class MqttLight(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, Light, RestoreEntity): """Representation of a MQTT light.""" - def __init__(self, config, discovery_hash): + def __init__(self, config, config_entry, discovery_hash): """Initialize MQTT light.""" self._state = False self._sub_state = None @@ -157,7 +157,7 @@ class MqttLight(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttAvailability.__init__(self, config) MqttDiscoveryUpdate.__init__(self, discovery_hash, self.discovery_update) - MqttEntityDeviceInfo.__init__(self, device_config) + MqttEntityDeviceInfo.__init__(self, device_config, config_entry) async def async_added_to_hass(self): """Subscribe to MQTT events.""" @@ -170,6 +170,7 @@ class MqttLight(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, self._setup_from_config(config) await self.attributes_discovery_update(config) await self.availability_discovery_update(config) + await self.device_info_discovery_update(config) await self._subscribe_topics() self.async_schedule_update_ha_state() diff --git a/homeassistant/components/mqtt/light/schema_json.py b/homeassistant/components/mqtt/light/schema_json.py index e1372b9a80f..4b1a1cff3ff 100644 --- a/homeassistant/components/mqtt/light/schema_json.py +++ b/homeassistant/components/mqtt/light/schema_json.py @@ -84,10 +84,10 @@ PLATFORM_SCHEMA_JSON = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ mqtt.MQTT_JSON_ATTRS_SCHEMA.schema) -async def async_setup_entity_json(config: ConfigType, - async_add_entities, discovery_hash): +async def async_setup_entity_json(config: ConfigType, async_add_entities, + config_entry, discovery_hash): """Set up a MQTT JSON Light.""" - async_add_entities([MqttLightJson(config, discovery_hash)]) + async_add_entities([MqttLightJson(config, config_entry, discovery_hash)]) # pylint: disable=too-many-ancestors @@ -95,7 +95,7 @@ class MqttLightJson(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, Light, RestoreEntity): """Representation of a MQTT JSON light.""" - def __init__(self, config, discovery_hash): + def __init__(self, config, config_entry, discovery_hash): """Initialize MQTT JSON light.""" self._state = False self._sub_state = None @@ -120,7 +120,7 @@ class MqttLightJson(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttAvailability.__init__(self, config) MqttDiscoveryUpdate.__init__(self, discovery_hash, self.discovery_update) - MqttEntityDeviceInfo.__init__(self, device_config) + MqttEntityDeviceInfo.__init__(self, device_config, config_entry) async def async_added_to_hass(self): """Subscribe to MQTT events.""" @@ -133,6 +133,7 @@ class MqttLightJson(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, self._setup_from_config(config) await self.attributes_discovery_update(config) await self.availability_discovery_update(config) + await self.device_info_discovery_update(config) await self._subscribe_topics() self.async_schedule_update_ha_state() diff --git a/homeassistant/components/mqtt/light/schema_template.py b/homeassistant/components/mqtt/light/schema_template.py index 80d773060d7..9b8e57c1d2a 100644 --- a/homeassistant/components/mqtt/light/schema_template.py +++ b/homeassistant/components/mqtt/light/schema_template.py @@ -70,10 +70,10 @@ PLATFORM_SCHEMA_TEMPLATE = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ mqtt.MQTT_JSON_ATTRS_SCHEMA.schema) -async def async_setup_entity_template(config, async_add_entities, +async def async_setup_entity_template(config, async_add_entities, config_entry, discovery_hash): """Set up a MQTT Template light.""" - async_add_entities([MqttTemplate(config, discovery_hash)]) + async_add_entities([MqttTemplate(config, config_entry, discovery_hash)]) # pylint: disable=too-many-ancestors @@ -81,7 +81,7 @@ class MqttTemplate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, Light, RestoreEntity): """Representation of a MQTT Template light.""" - def __init__(self, config, discovery_hash): + def __init__(self, config, config_entry, discovery_hash): """Initialize a MQTT Template light.""" self._state = False self._sub_state = None @@ -107,7 +107,7 @@ class MqttTemplate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttAvailability.__init__(self, config) MqttDiscoveryUpdate.__init__(self, discovery_hash, self.discovery_update) - MqttEntityDeviceInfo.__init__(self, device_config) + MqttEntityDeviceInfo.__init__(self, device_config, config_entry) async def async_added_to_hass(self): """Subscribe to MQTT events.""" @@ -120,6 +120,7 @@ class MqttTemplate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, self._setup_from_config(config) await self.attributes_discovery_update(config) await self.availability_discovery_update(config) + await self.device_info_discovery_update(config) await self._subscribe_topics() self.async_schedule_update_ha_state() diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 9ad3a8d8323..f501f97331e 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -1346,6 +1346,53 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): assert device.sw_version == '0.1-beta' +async def test_entity_device_info_update(hass, mqtt_mock): + """Test device registry update.""" + 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() + + config = { + 'platform': 'mqtt', + 'name': 'Test 1', + 'state_topic': 'test-topic', + 'command_topic': 'test-command-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' + } + + data = json.dumps(config) + 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.name == 'Beer' + + config['device']['name'] = 'Milk' + data = json.dumps(config) + 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.name == 'Milk' + + async def test_entity_id_update(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" registry = mock_registry(hass, {}) diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 86523d955a0..9533ea5f204 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -818,6 +818,54 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): assert device.sw_version == '0.1-beta' +async def test_entity_device_info_update(hass, mqtt_mock): + """Test device registry update.""" + 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() + + config = { + 'platform': 'mqtt', + 'name': 'Test 1', + 'schema': 'json', + 'state_topic': 'test-topic', + 'command_topic': 'test-command-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' + } + + data = json.dumps(config) + 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.name == 'Beer' + + config['device']['name'] = 'Milk' + data = json.dumps(config) + 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.name == 'Milk' + + async def test_entity_id_update(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" registry = mock_registry(hass, {}) diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index 66048b3e3bf..a7147e83b99 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -791,6 +791,56 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): assert device.sw_version == '0.1-beta' +async def test_entity_device_info_update(hass, mqtt_mock): + """Test device registry update.""" + 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() + + config = { + 'platform': 'mqtt', + 'name': 'Test 1', + 'schema': 'template', + 'state_topic': 'test-topic', + 'command_topic': 'test-command-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' + } + + data = json.dumps(config) + 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.name == 'Beer' + + config['device']['name'] = 'Milk' + data = json.dumps(config) + 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.name == 'Milk' + + async def test_entity_id_update(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" registry = mock_registry(hass, {})