mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Update device registry of MQTT light (#20441)
* Update device registry of MQTT light * Move config_entry to constructor
This commit is contained in:
parent
f353d51ab1
commit
3ee3acd550
@ -55,7 +55,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
try:
|
try:
|
||||||
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
|
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
|
||||||
config = PLATFORM_SCHEMA(discovery_payload)
|
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)
|
discovery_hash)
|
||||||
except Exception:
|
except Exception:
|
||||||
if discovery_hash:
|
if discovery_hash:
|
||||||
@ -67,7 +67,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
async_discover)
|
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):
|
discovery_hash=None):
|
||||||
"""Set up a MQTT Light."""
|
"""Set up a MQTT Light."""
|
||||||
setup_entity = {
|
setup_entity = {
|
||||||
@ -76,4 +76,4 @@ async def _async_setup_entity(config, async_add_entities,
|
|||||||
'template': schema_template.async_setup_entity_template,
|
'template': schema_template.async_setup_entity_template,
|
||||||
}
|
}
|
||||||
await setup_entity[config[CONF_SCHEMA]](
|
await setup_entity[config[CONF_SCHEMA]](
|
||||||
config, async_add_entities, discovery_hash)
|
config, async_add_entities, config_entry, discovery_hash)
|
||||||
|
@ -111,13 +111,13 @@ PLATFORM_SCHEMA_BASIC = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
|
|||||||
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
|
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):
|
discovery_hash=None):
|
||||||
"""Set up a MQTT Light."""
|
"""Set up a MQTT Light."""
|
||||||
config.setdefault(
|
config.setdefault(
|
||||||
CONF_STATE_VALUE_TEMPLATE, config.get(CONF_VALUE_TEMPLATE))
|
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
|
# pylint: disable=too-many-ancestors
|
||||||
@ -125,7 +125,7 @@ class MqttLight(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
MqttEntityDeviceInfo, Light, RestoreEntity):
|
MqttEntityDeviceInfo, Light, RestoreEntity):
|
||||||
"""Representation of a MQTT light."""
|
"""Representation of a MQTT light."""
|
||||||
|
|
||||||
def __init__(self, config, discovery_hash):
|
def __init__(self, config, config_entry, discovery_hash):
|
||||||
"""Initialize MQTT light."""
|
"""Initialize MQTT light."""
|
||||||
self._state = False
|
self._state = False
|
||||||
self._sub_state = None
|
self._sub_state = None
|
||||||
@ -157,7 +157,7 @@ class MqttLight(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
MqttAvailability.__init__(self, config)
|
MqttAvailability.__init__(self, config)
|
||||||
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
||||||
self.discovery_update)
|
self.discovery_update)
|
||||||
MqttEntityDeviceInfo.__init__(self, device_config)
|
MqttEntityDeviceInfo.__init__(self, device_config, config_entry)
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to MQTT events."""
|
"""Subscribe to MQTT events."""
|
||||||
@ -170,6 +170,7 @@ class MqttLight(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
self._setup_from_config(config)
|
self._setup_from_config(config)
|
||||||
await self.attributes_discovery_update(config)
|
await self.attributes_discovery_update(config)
|
||||||
await self.availability_discovery_update(config)
|
await self.availability_discovery_update(config)
|
||||||
|
await self.device_info_discovery_update(config)
|
||||||
await self._subscribe_topics()
|
await self._subscribe_topics()
|
||||||
self.async_schedule_update_ha_state()
|
self.async_schedule_update_ha_state()
|
||||||
|
|
||||||
|
@ -84,10 +84,10 @@ PLATFORM_SCHEMA_JSON = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
|
|||||||
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
|
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entity_json(config: ConfigType,
|
async def async_setup_entity_json(config: ConfigType, async_add_entities,
|
||||||
async_add_entities, discovery_hash):
|
config_entry, discovery_hash):
|
||||||
"""Set up a MQTT JSON Light."""
|
"""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
|
# pylint: disable=too-many-ancestors
|
||||||
@ -95,7 +95,7 @@ class MqttLightJson(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
MqttEntityDeviceInfo, Light, RestoreEntity):
|
MqttEntityDeviceInfo, Light, RestoreEntity):
|
||||||
"""Representation of a MQTT JSON light."""
|
"""Representation of a MQTT JSON light."""
|
||||||
|
|
||||||
def __init__(self, config, discovery_hash):
|
def __init__(self, config, config_entry, discovery_hash):
|
||||||
"""Initialize MQTT JSON light."""
|
"""Initialize MQTT JSON light."""
|
||||||
self._state = False
|
self._state = False
|
||||||
self._sub_state = None
|
self._sub_state = None
|
||||||
@ -120,7 +120,7 @@ class MqttLightJson(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
MqttAvailability.__init__(self, config)
|
MqttAvailability.__init__(self, config)
|
||||||
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
||||||
self.discovery_update)
|
self.discovery_update)
|
||||||
MqttEntityDeviceInfo.__init__(self, device_config)
|
MqttEntityDeviceInfo.__init__(self, device_config, config_entry)
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to MQTT events."""
|
"""Subscribe to MQTT events."""
|
||||||
@ -133,6 +133,7 @@ class MqttLightJson(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
self._setup_from_config(config)
|
self._setup_from_config(config)
|
||||||
await self.attributes_discovery_update(config)
|
await self.attributes_discovery_update(config)
|
||||||
await self.availability_discovery_update(config)
|
await self.availability_discovery_update(config)
|
||||||
|
await self.device_info_discovery_update(config)
|
||||||
await self._subscribe_topics()
|
await self._subscribe_topics()
|
||||||
self.async_schedule_update_ha_state()
|
self.async_schedule_update_ha_state()
|
||||||
|
|
||||||
|
@ -70,10 +70,10 @@ PLATFORM_SCHEMA_TEMPLATE = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
|
|||||||
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
|
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):
|
discovery_hash):
|
||||||
"""Set up a MQTT Template light."""
|
"""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
|
# pylint: disable=too-many-ancestors
|
||||||
@ -81,7 +81,7 @@ class MqttTemplate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
MqttEntityDeviceInfo, Light, RestoreEntity):
|
MqttEntityDeviceInfo, Light, RestoreEntity):
|
||||||
"""Representation of a MQTT Template light."""
|
"""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."""
|
"""Initialize a MQTT Template light."""
|
||||||
self._state = False
|
self._state = False
|
||||||
self._sub_state = None
|
self._sub_state = None
|
||||||
@ -107,7 +107,7 @@ class MqttTemplate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
MqttAvailability.__init__(self, config)
|
MqttAvailability.__init__(self, config)
|
||||||
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
||||||
self.discovery_update)
|
self.discovery_update)
|
||||||
MqttEntityDeviceInfo.__init__(self, device_config)
|
MqttEntityDeviceInfo.__init__(self, device_config, config_entry)
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to MQTT events."""
|
"""Subscribe to MQTT events."""
|
||||||
@ -120,6 +120,7 @@ class MqttTemplate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
|||||||
self._setup_from_config(config)
|
self._setup_from_config(config)
|
||||||
await self.attributes_discovery_update(config)
|
await self.attributes_discovery_update(config)
|
||||||
await self.availability_discovery_update(config)
|
await self.availability_discovery_update(config)
|
||||||
|
await self.device_info_discovery_update(config)
|
||||||
await self._subscribe_topics()
|
await self._subscribe_topics()
|
||||||
self.async_schedule_update_ha_state()
|
self.async_schedule_update_ha_state()
|
||||||
|
|
||||||
|
@ -1346,6 +1346,53 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
|||||||
assert device.sw_version == '0.1-beta'
|
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):
|
async def test_entity_id_update(hass, mqtt_mock):
|
||||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||||
registry = mock_registry(hass, {})
|
registry = mock_registry(hass, {})
|
||||||
|
@ -818,6 +818,54 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
|||||||
assert device.sw_version == '0.1-beta'
|
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):
|
async def test_entity_id_update(hass, mqtt_mock):
|
||||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||||
registry = mock_registry(hass, {})
|
registry = mock_registry(hass, {})
|
||||||
|
@ -791,6 +791,56 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
|||||||
assert device.sw_version == '0.1-beta'
|
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):
|
async def test_entity_id_update(hass, mqtt_mock):
|
||||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||||
registry = mock_registry(hass, {})
|
registry = mock_registry(hass, {})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user