From 8804f55fcc10d7cc878376ef1b4281e284e23daa Mon Sep 17 00:00:00 2001 From: emontnemery Date: Sun, 27 Jan 2019 17:43:16 +0100 Subject: [PATCH] Update device registry of MQTT Vacuum (#20500) --- homeassistant/components/mqtt/vacuum.py | 11 +++--- tests/components/mqtt/test_vacuum.py | 47 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/mqtt/vacuum.py b/homeassistant/components/mqtt/vacuum.py index baa43411411..fedf33c5a2a 100644 --- a/homeassistant/components/mqtt/vacuum.py +++ b/homeassistant/components/mqtt/vacuum.py @@ -163,17 +163,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async def async_discover(discovery_payload): """Discover and add a MQTT vacuum.""" config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity(config, async_add_entities, + await _async_setup_entity(config, async_add_entities, config_entry, discovery_payload[ATTR_DISCOVERY_HASH]) async_dispatcher_connect( hass, MQTT_DISCOVERY_NEW.format(DOMAIN, 'mqtt'), async_discover) -async def _async_setup_entity(config, async_add_entities, +async def _async_setup_entity(config, async_add_entities, config_entry, discovery_hash=None): """Set up the MQTT vacuum.""" - async_add_entities([MqttVacuum(config, discovery_hash)]) + async_add_entities([MqttVacuum(config, config_entry, discovery_hash)]) # pylint: disable=too-many-ancestors @@ -181,7 +181,7 @@ class MqttVacuum(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, VacuumDevice): """Representation of a MQTT-controlled vacuum.""" - def __init__(self, config, discovery_info): + def __init__(self, config, config_entry, discovery_info): """Initialize the vacuum.""" self._cleaning = False self._charging = False @@ -203,7 +203,7 @@ class MqttVacuum(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate, MqttAvailability.__init__(self, config) MqttDiscoveryUpdate.__init__(self, discovery_info, self.discovery_update) - MqttEntityDeviceInfo.__init__(self, device_config) + MqttEntityDeviceInfo.__init__(self, device_config, config_entry) def _setup_from_config(self, config): self._name = config.get(CONF_NAME) @@ -257,6 +257,7 @@ class MqttVacuum(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_vacuum.py b/tests/components/mqtt/test_vacuum.py index 99a7b3579f3..04a82bb20e1 100644 --- a/tests/components/mqtt/test_vacuum.py +++ b/tests/components/mqtt/test_vacuum.py @@ -494,3 +494,50 @@ async def test_entity_device_info_with_identifier(hass, mock_publish): assert device.name == 'Beer' assert device.model == 'Glass' 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/vacuum/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/vacuum/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'