Remove discovered MQTT Switch device when discovery topic is cleared (#16605)

* Remove discovered device when discovery topic is cleared

* Move entity removal away from mqtt discovery

* Move discovery update to mixin class

* Add testcase

* Review comments
This commit is contained in:
emontnemery authored and Fabian Affolter committed 2018-09-24 10:11:49 +02:00
1 parent a5cb4e6c2b
commit 5ee4718e24
4 files changed
+121 -40

No files matched your search

+21 -10
View File
@@ -11,9 +11,10 @@ import voluptuous as vol
from homeassistant.core import callback
from homeassistant.components.mqtt import (
CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_AVAILABILITY_TOPIC,
CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_RETAIN,
MqttAvailability)
ATTR_DISCOVERY_HASH, CONF_STATE_TOPIC, CONF_COMMAND_TOPIC,
CONF_AVAILABILITY_TOPIC, CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_RETAIN, MqttAvailability,
MqttDiscoveryUpdate)
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (
CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF,
@@ -56,7 +57,11 @@ async def async_setup_platform(hass, config, async_add_entities,
if value_template is not None:
value_template.hass = hass
async_add_entities([MqttSwitch(
discovery_hash = None
if discovery_info is not None and ATTR_DISCOVERY_HASH in discovery_info:
discovery_hash = discovery_info[ATTR_DISCOVERY_HASH]
newswitch = MqttSwitch(
config.get(CONF_NAME),
config.get(CONF_ICON),
config.get(CONF_STATE_TOPIC),
@@ -73,10 +78,13 @@ async def async_setup_platform(hass, config, async_add_entities,
config.get(CONF_PAYLOAD_NOT_AVAILABLE),
config.get(CONF_UNIQUE_ID),
value_template,
)])
discovery_hash,
)
async_add_entities([newswitch])
class MqttSwitch(MqttAvailability, SwitchDevice):
class MqttSwitch(MqttAvailability, MqttDiscoveryUpdate, SwitchDevice):
"""Representation of a switch that can be toggled using MQTT."""
def __init__(self, name, icon,
@@ -84,10 +92,11 @@ class MqttSwitch(MqttAvailability, SwitchDevice):
qos, retain, payload_on, payload_off, state_on,
state_off, optimistic, payload_available,
payload_not_available, unique_id: Optional[str],
value_template):
value_template, discovery_hash):
"""Initialize the MQTT switch."""
super().__init__(availability_topic, qos, payload_available,
payload_not_available)
MqttAvailability.__init__(self, availability_topic, qos,
payload_available, payload_not_available)
MqttDiscoveryUpdate.__init__(self, discovery_hash)
self._state = False
self._name = name
self._icon = icon
@@ -102,10 +111,12 @@ class MqttSwitch(MqttAvailability, SwitchDevice):
self._optimistic = optimistic
self._template = value_template
self._unique_id = unique_id
self._discovery_hash = discovery_hash
async def async_added_to_hass(self):
"""Subscribe to MQTT events."""
await super().async_added_to_hass()
await MqttAvailability.async_added_to_hass(self)
await MqttDiscoveryUpdate.async_added_to_hass(self)
@callback
def state_message_received(topic, payload, qos):