mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Cleanup if discovered mqtt lock can't be added (#19746)
* Cleanup if discovered mqtt lock can't be added
This commit is contained in:
parent
406b45c6e7
commit
bb37cf906c
@ -18,7 +18,8 @@ from homeassistant.components.mqtt import (
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICE, CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE)
|
||||
from homeassistant.components import mqtt, lock
|
||||
from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW
|
||||
from homeassistant.components.mqtt.discovery import (
|
||||
MQTT_DISCOVERY_NEW, clear_discovery_hash)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.typing import HomeAssistantType, ConfigType
|
||||
@ -57,9 +58,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up MQTT lock dynamically through MQTT discovery."""
|
||||
async def async_discover(discovery_payload):
|
||||
"""Discover and add an MQTT lock."""
|
||||
config = PLATFORM_SCHEMA(discovery_payload)
|
||||
await _async_setup_entity(hass, config, async_add_entities,
|
||||
discovery_payload[ATTR_DISCOVERY_HASH])
|
||||
try:
|
||||
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
|
||||
config = PLATFORM_SCHEMA(discovery_payload)
|
||||
await _async_setup_entity(hass, config, async_add_entities,
|
||||
discovery_hash)
|
||||
except Exception:
|
||||
if discovery_hash:
|
||||
clear_discovery_hash(hass, discovery_hash)
|
||||
raise
|
||||
|
||||
async_dispatcher_connect(
|
||||
hass, MQTT_DISCOVERY_NEW.format(lock.DOMAIN, 'mqtt'),
|
||||
|
@ -182,6 +182,38 @@ async def test_discovery_removal_lock(hass, mqtt_mock, caplog):
|
||||
assert state is None
|
||||
|
||||
|
||||
async def test_discovery_broken(hass, mqtt_mock, caplog):
|
||||
"""Test handling of bad discovery message."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
await async_start(hass, 'homeassistant', {}, entry)
|
||||
|
||||
data1 = (
|
||||
'{ "name": "Beer" }'
|
||||
)
|
||||
data2 = (
|
||||
'{ "name": "Milk",'
|
||||
' "command_topic": "test_topic" }'
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/lock/bla/config',
|
||||
data1)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('lock.beer')
|
||||
assert state is None
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/lock/bla/config',
|
||||
data2)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('lock.milk')
|
||||
assert state is not None
|
||||
assert state.name == 'Milk'
|
||||
state = hass.states.get('lock.beer')
|
||||
assert state is None
|
||||
|
||||
|
||||
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
||||
"""Test MQTT lock device registry integration."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
|
Loading…
x
Reference in New Issue
Block a user