Update device registry of MQTT light (#20441)
* Update device registry of MQTT light * Move config_entry to constructor
This commit is contained in:
1 parent
f353d51ab1
commit
3ee3acd550
7 files changed
+164
-16
No files matched your search
@@ -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)
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in new issue
Block a user