Cleanup if discovered mqtt alarm can't be added (#19742)
* Cleanup if discovered mqtt alarm can't be addedpull/19875/head
parent
1a5fe3d880
commit
44f6151548
|
@ -21,7 +21,8 @@ from homeassistant.components.mqtt import (
|
||||||
CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_RETAIN,
|
CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_RETAIN,
|
||||||
CONF_STATE_TOPIC, MqttAvailability, MqttDiscoveryUpdate,
|
CONF_STATE_TOPIC, MqttAvailability, MqttDiscoveryUpdate,
|
||||||
MqttEntityDeviceInfo, subscription)
|
MqttEntityDeviceInfo, subscription)
|
||||||
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
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.typing import HomeAssistantType, ConfigType
|
from homeassistant.helpers.typing import HomeAssistantType, ConfigType
|
||||||
|
@ -62,9 +63,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up MQTT alarm control panel dynamically through MQTT discovery."""
|
"""Set up MQTT alarm control panel dynamically through MQTT discovery."""
|
||||||
async def async_discover(discovery_payload):
|
async def async_discover(discovery_payload):
|
||||||
"""Discover and add an MQTT alarm control panel."""
|
"""Discover and add an MQTT alarm control panel."""
|
||||||
config = PLATFORM_SCHEMA(discovery_payload)
|
try:
|
||||||
await _async_setup_entity(config, async_add_entities,
|
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
|
||||||
discovery_payload[ATTR_DISCOVERY_HASH])
|
config = PLATFORM_SCHEMA(discovery_payload)
|
||||||
|
await _async_setup_entity(config, async_add_entities,
|
||||||
|
discovery_hash)
|
||||||
|
except Exception:
|
||||||
|
if discovery_hash:
|
||||||
|
clear_discovery_hash(hass, discovery_hash)
|
||||||
|
raise
|
||||||
|
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
hass, MQTT_DISCOVERY_NEW.format(alarm.DOMAIN, 'mqtt'),
|
hass, MQTT_DISCOVERY_NEW.format(alarm.DOMAIN, 'mqtt'),
|
||||||
|
|
|
@ -300,7 +300,7 @@ async def test_discovery_removal_alarm(hass, mqtt_mock, caplog):
|
||||||
|
|
||||||
|
|
||||||
async def test_discovery_update_alarm(hass, mqtt_mock, caplog):
|
async def test_discovery_update_alarm(hass, mqtt_mock, caplog):
|
||||||
"""Test removal of discovered alarm_control_panel."""
|
"""Test update of discovered alarm_control_panel."""
|
||||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||||
await async_start(hass, 'homeassistant', {}, entry)
|
await async_start(hass, 'homeassistant', {}, entry)
|
||||||
|
|
||||||
|
@ -338,6 +338,41 @@ async def test_discovery_update_alarm(hass, mqtt_mock, caplog):
|
||||||
assert state is None
|
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",'
|
||||||
|
' "status_topic": "test_topic",'
|
||||||
|
' "command_topic": "test_topic" }'
|
||||||
|
)
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass,
|
||||||
|
'homeassistant/alarm_control_panel/bla/config',
|
||||||
|
data1)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('alarm_control_panel.beer')
|
||||||
|
assert state is None
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass,
|
||||||
|
'homeassistant/alarm_control_panel/bla/config',
|
||||||
|
data2)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('alarm_control_panel.milk')
|
||||||
|
assert state is not None
|
||||||
|
assert state.name == 'Milk'
|
||||||
|
state = hass.states.get('alarm_control_panel.beer')
|
||||||
|
assert state is None
|
||||||
|
|
||||||
|
|
||||||
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
||||||
"""Test MQTT alarm control panel device registry integration."""
|
"""Test MQTT alarm control panel device registry integration."""
|
||||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||||
|
|
Loading…
Reference in New Issue