Remove discovered mqtt_json light entity when discovery is cleared (#16906)

* Remove discovered mqtt_json entity device when discovery topic is cleared

* Keep imports ordered
This commit is contained in:
Otto Winter 2018-09-28 00:25:51 +02:00 committed by Fabian Affolter
parent 9abdbf3db6
commit c9b6567265
2 changed files with 54 additions and 20 deletions

View file

@ -97,11 +97,12 @@ from homeassistant.const import (
STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE,
ATTR_SUPPORTED_FEATURES)
import homeassistant.components.light as light
from homeassistant.components.mqtt.discovery import async_start
import homeassistant.core as ha
from tests.common import (
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message,
assert_setup_component, mock_coro)
assert_setup_component, mock_coro, async_fire_mqtt_message)
from tests.components.light import common
@ -669,3 +670,25 @@ class TestLightMQTTJSON(unittest.TestCase):
state = self.hass.states.get('light.test')
self.assertEqual(STATE_UNAVAILABLE, state.state)
async def test_discovery_removal(hass, mqtt_mock, caplog):
"""Test removal of discovered mqtt_json lights."""
await async_start(hass, 'homeassistant', {})
data = (
'{ "name": "Beer",'
' "platform": "mqtt_json",'
' "command_topic": "test_topic" }'
)
async_fire_mqtt_message(hass, 'homeassistant/light/bla/config',
data)
await hass.async_block_till_done()
state = hass.states.get('light.beer')
assert state is not None
assert state.name == 'Beer'
async_fire_mqtt_message(hass, 'homeassistant/light/bla/config',
'')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('light.beer')
assert state is None