mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Merge pull request #19744 from emontnemery/mqtt_discovery_cleanup_binary_sensor
Cleanup if discovered mqtt binary_sensor can't be added
This commit is contained in:
commit
a62e514d8f
@ -20,7 +20,8 @@ from homeassistant.components.mqtt import (
|
|||||||
CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS,
|
CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS,
|
||||||
MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
MqttAttributes, 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
|
||||||
import homeassistant.helpers.event as evt
|
import homeassistant.helpers.event as evt
|
||||||
@ -63,9 +64,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
"""Set up MQTT binary sensor dynamically through MQTT discovery."""
|
"""Set up MQTT binary sensor dynamically through MQTT discovery."""
|
||||||
async def async_discover(discovery_payload):
|
async def async_discover(discovery_payload):
|
||||||
"""Discover and add a MQTT binary sensor."""
|
"""Discover and add a MQTT binary sensor."""
|
||||||
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(binary_sensor.DOMAIN, 'mqtt'),
|
hass, MQTT_DISCOVERY_NEW.format(binary_sensor.DOMAIN, 'mqtt'),
|
||||||
|
@ -390,7 +390,7 @@ async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog):
|
|||||||
|
|
||||||
|
|
||||||
async def test_discovery_update_binary_sensor(hass, mqtt_mock, caplog):
|
async def test_discovery_update_binary_sensor(hass, mqtt_mock, caplog):
|
||||||
"""Test removal of discovered binary_sensor."""
|
"""Test update of discovered binary_sensor."""
|
||||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||||
await async_start(hass, 'homeassistant', {}, entry)
|
await async_start(hass, 'homeassistant', {}, entry)
|
||||||
data1 = (
|
data1 = (
|
||||||
@ -421,6 +421,39 @@ async def test_discovery_update_binary_sensor(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",'
|
||||||
|
' "off_delay": -1 }'
|
||||||
|
)
|
||||||
|
data2 = (
|
||||||
|
'{ "name": "Milk",'
|
||||||
|
' "state_topic": "test_topic" }'
|
||||||
|
)
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
|
||||||
|
data1)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('binary_sensor.beer')
|
||||||
|
assert state is None
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
|
||||||
|
data2)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('binary_sensor.milk')
|
||||||
|
assert state is not None
|
||||||
|
assert state.name == 'Milk'
|
||||||
|
state = hass.states.get('binary_sensor.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 binary sensor device registry integration."""
|
"""Test MQTT binary sensor device registry integration."""
|
||||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user