Skip to content

Commit 8dc9fc8

Browse files
committed
Add add entry unload support for notify platform
1 parent 0dc0f51 commit 8dc9fc8

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

homeassistant/components/mqtt/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,3 +1311,11 @@ async def async_remove_config_entry_device(
13111311

13121312
await device_automation.async_removed_from_device(hass, device_entry.id)
13131313
return True
1314+
1315+
1316+
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1317+
"""Unload a config entry."""
1318+
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
1319+
if unload_ok:
1320+
pass
1321+
return unload_ok

homeassistant/components/mqtt/mixins.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ async def async_discovery_update(
571571
async def async_device_removed(event):
572572
"""Handle the removal of a device."""
573573
nonlocal _device_removed
574-
nonlocal _device_removed
575574
if _device_removed or not async_removed_from_device(
576575
hass, event, device_id, config_entry_id
577576
):
@@ -598,11 +597,24 @@ async def _async_tear_down() -> None:
598597
)
599598
del self
600599

600+
@callback
601+
def _entry_unload(*_: Any) -> None:
602+
"""Handle the unload of the config entry."""
603+
nonlocal self
604+
# cleanup discovery
605+
async_dispatcher_send(
606+
hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None
607+
)
608+
clear_discovery_hash(hass, discovery_hash)
609+
_remove_discovery()
610+
hass.async_create_task(self.async_tear_down())
611+
601612
_remove_discovery = async_dispatcher_connect(
602613
hass,
603614
MQTT_DISCOVERY_UPDATED.format(discovery_hash),
604615
async_discovery_update,
605616
)
617+
config_entry.async_on_unload(_entry_unload)
606618
if device_id is not None:
607619
self._remove_device_updated = hass.bus.async_listen(
608620
EVENT_DEVICE_REGISTRY_UPDATED, async_device_removed

tests/components/mqtt/test_notify.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
import yaml
88

9-
from homeassistant import config as hass_config
9+
from homeassistant import config as hass_config, config_entries
1010
from homeassistant.components import notify
1111
from homeassistant.components.mqtt import DOMAIN
1212
from homeassistant.const import CONF_NAME, SERVICE_RELOAD
@@ -688,7 +688,6 @@ async def test_publishing_with_custom_encoding(hass, mqtt_mock, caplog):
688688
await hass.async_block_till_done()
689689

690690
assert not hass.services.has_service(notify.DOMAIN, "test3")
691-
assert "<Event service_removed[L]: domain=notify, service=test3>" in caplog.text
692691

693692

694693
async def test_reloadable(hass, mqtt_mock, caplog, tmp_path):
@@ -772,3 +771,29 @@ async def test_reloadable(hass, mqtt_mock, caplog, tmp_path):
772771
await hass.async_block_till_done()
773772

774773
assert not hass.services.has_service(notify.DOMAIN, "test_old_3")
774+
775+
776+
async def test_unload_entry(hass, mqtt_mock):
777+
"""Test unloading an entry."""
778+
# discover a notify service
779+
data = '{"name": "Test me", "command_topic": "test_topic_discovery" }'
780+
async_fire_mqtt_message(hass, f"homeassistant/{notify.DOMAIN}/bla/config", data)
781+
await hass.async_block_till_done()
782+
783+
assert hass.services.has_service(notify.DOMAIN, "test_me")
784+
assert await config_entries.support_entry_unload(hass, DOMAIN)
785+
786+
# get the config_entry and unload it
787+
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
788+
789+
await config_entry.async_unload(hass)
790+
await hass.async_block_till_done()
791+
792+
# test if auto discovered item did not survive the platform unload
793+
with pytest.raises(ServiceNotFound):
794+
await hass.services.async_call(
795+
notify.DOMAIN,
796+
"test_me",
797+
{notify.ATTR_TITLE: "Title", notify.ATTR_MESSAGE: "Message"},
798+
blocking=True,
799+
)

0 commit comments

Comments
 (0)