Add JSON attribute topic to MQTT light (#20191)

This commit is contained in:
emontnemery 2019-01-17 19:54:22 +01:00 committed by Paulus Schoutsen
parent d1c6eb4f3e
commit 234f348ba1
6 changed files with 346 additions and 11 deletions

View file

@ -17,7 +17,8 @@ from homeassistant.components.light import (
SUPPORT_FLASH, SUPPORT_TRANSITION, SUPPORT_WHITE_VALUE, Light)
from homeassistant.components.mqtt import (
CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN, CONF_STATE_TOPIC,
MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, subscription)
MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
MqttEntityDeviceInfo, subscription)
from homeassistant.const import (
CONF_BRIGHTNESS, CONF_COLOR_TEMP, CONF_DEVICE, CONF_EFFECT, CONF_NAME,
CONF_OPTIMISTIC, CONF_RGB, CONF_WHITE_VALUE, CONF_XY, STATE_ON)
@ -80,7 +81,8 @@ PLATFORM_SCHEMA_JSON = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_HS, default=DEFAULT_HS): cv.boolean,
vol.Required(CONF_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
async def async_setup_entity_json(hass: HomeAssistantType, config: ConfigType,
@ -90,7 +92,7 @@ async def async_setup_entity_json(hass: HomeAssistantType, config: ConfigType,
# pylint: disable=too-many-ancestors
class MqttLightJson(MqttAvailability, MqttDiscoveryUpdate,
class MqttLightJson(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
MqttEntityDeviceInfo, Light, RestoreEntity):
"""Representation of a MQTT JSON light."""
@ -115,6 +117,7 @@ class MqttLightJson(MqttAvailability, MqttDiscoveryUpdate,
device_config = config.get(CONF_DEVICE)
MqttAttributes.__init__(self, config)
MqttAvailability.__init__(self, config)
MqttDiscoveryUpdate.__init__(self, discovery_hash,
self.discovery_update)
@ -129,6 +132,7 @@ class MqttLightJson(MqttAvailability, MqttDiscoveryUpdate,
"""Handle updated discovery message."""
config = PLATFORM_SCHEMA_JSON(discovery_payload)
self._setup_from_config(config)
await self.attributes_discovery_update(config)
await self.availability_discovery_update(config)
await self._subscribe_topics()
self.async_schedule_update_ha_state()
@ -297,6 +301,7 @@ class MqttLightJson(MqttAvailability, MqttDiscoveryUpdate,
"""Unsubscribe when removed."""
self._sub_state = await subscription.async_unsubscribe_topics(
self.hass, self._sub_state)
await MqttAttributes.async_will_remove_from_hass(self)
await MqttAvailability.async_will_remove_from_hass(self)
@property