Cleanup if discovered mqtt sensor can't be added
This commit is contained in:
parent
dd75c49796
commit
0b57cfb004
2 changed files with 45 additions and 5 deletions
|
@ -17,7 +17,8 @@ from homeassistant.components.mqtt import (
|
|||
ATTR_DISCOVERY_HASH, CONF_AVAILABILITY_TOPIC, CONF_PAYLOAD_AVAILABLE,
|
||||
CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_STATE_TOPIC, MqttAttributes,
|
||||
MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, subscription)
|
||||
from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW
|
||||
from homeassistant.components.mqtt.discovery import (
|
||||
ALREADY_DISCOVERED, MQTT_DISCOVERY_NEW)
|
||||
from homeassistant.components.sensor import DEVICE_CLASSES_SCHEMA
|
||||
from homeassistant.const import (
|
||||
CONF_FORCE_UPDATE, CONF_NAME, CONF_VALUE_TEMPLATE, STATE_UNKNOWN,
|
||||
|
@ -66,9 +67,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
"""Set up MQTT sensors dynamically through MQTT discovery."""
|
||||
async def async_discover_sensor(discovery_payload):
|
||||
"""Discover and add a discovered MQTT sensor."""
|
||||
try:
|
||||
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
|
||||
config = PLATFORM_SCHEMA(discovery_payload)
|
||||
await _async_setup_entity(config, async_add_entities,
|
||||
discovery_payload[ATTR_DISCOVERY_HASH])
|
||||
discovery_hash)
|
||||
except: # noqa: E722
|
||||
if discovery_hash:
|
||||
del hass.data[ALREADY_DISCOVERED][discovery_hash]
|
||||
raise
|
||||
|
||||
async_dispatcher_connect(hass,
|
||||
MQTT_DISCOVERY_NEW.format(sensor.DOMAIN, 'mqtt'),
|
||||
|
|
|
@ -474,7 +474,7 @@ async def test_discovery_removal_sensor(hass, mqtt_mock, caplog):
|
|||
|
||||
|
||||
async def test_discovery_update_sensor(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered sensor."""
|
||||
"""Test update of discovered sensor."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
await async_start(hass, 'homeassistant', {}, entry)
|
||||
data1 = (
|
||||
|
@ -506,6 +506,39 @@ async def test_discovery_update_sensor(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",'
|
||||
' "state_topic": "test_topic#" }'
|
||||
)
|
||||
data2 = (
|
||||
'{ "name": "Milk",'
|
||||
' "state_topic": "test_topic" }'
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config',
|
||||
data1)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('sensor.beer')
|
||||
assert state is None
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config',
|
||||
data2)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('sensor.milk')
|
||||
assert state is not None
|
||||
assert state.name == 'Milk'
|
||||
state = hass.states.get('sensor.beer')
|
||||
assert state is None
|
||||
|
||||
|
||||
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
||||
"""Test MQTT sensor device registry integration."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue