Fix error handling on subscribe when mqtt is not initialized (#101832)
This commit is contained in:
parent
20a58d2314
commit
485c52568d
2 changed files with 18 additions and 1 deletions
|
@ -176,7 +176,13 @@ async def async_subscribe(
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(
|
||||||
f"Cannot subscribe to topic '{topic}', MQTT is not enabled"
|
f"Cannot subscribe to topic '{topic}', MQTT is not enabled"
|
||||||
)
|
)
|
||||||
mqtt_data = get_mqtt_data(hass)
|
try:
|
||||||
|
mqtt_data = get_mqtt_data(hass)
|
||||||
|
except KeyError as ex:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
f"Cannot subscribe to topic '{topic}', "
|
||||||
|
"make sure MQTT is set up correctly"
|
||||||
|
) from ex
|
||||||
async_remove = await mqtt_data.client.async_subscribe(
|
async_remove = await mqtt_data.client.async_subscribe(
|
||||||
topic,
|
topic,
|
||||||
catch_log_exception(
|
catch_log_exception(
|
||||||
|
|
|
@ -959,6 +959,17 @@ async def test_subscribe_topic(
|
||||||
unsub()
|
unsub()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_subscribe_topic_not_initialize(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||||
|
) -> None:
|
||||||
|
"""Test the subscription of a topic when MQTT was not initialized."""
|
||||||
|
with pytest.raises(
|
||||||
|
HomeAssistantError, match=r".*make sure MQTT is set up correctly"
|
||||||
|
):
|
||||||
|
await mqtt.async_subscribe(hass, "test-topic", record_calls)
|
||||||
|
|
||||||
|
|
||||||
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
|
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
|
||||||
@patch("homeassistant.components.mqtt.client.UNSUBSCRIBE_COOLDOWN", 0.2)
|
@patch("homeassistant.components.mqtt.client.UNSUBSCRIBE_COOLDOWN", 0.2)
|
||||||
async def test_subscribe_and_resubscribe(
|
async def test_subscribe_and_resubscribe(
|
||||||
|
|
Loading…
Add table
Reference in a new issue