Add a filter to the tasmota device registry listener (#93640)

We can avoid creating a task when the event is not
a remove which will be most cases
This commit is contained in:
J. Nick Koston 2023-05-29 19:59:51 -05:00 committed by GitHub
parent 493d78f070
commit cc107bd0b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@ from hatasmota.models import DiscoveryHashType
from hatasmota.trigger import TasmotaTrigger
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Event, HomeAssistant
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -24,10 +24,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> N
async def async_device_removed(event: Event) -> None:
"""Handle the removal of a device."""
if event.data["action"] != "remove":
return
await async_remove_automations(hass, event.data["device_id"])
@callback
def _async_device_removed_filter(event: Event) -> bool:
"""Filter device registry events."""
return event.data["action"] == "remove"
async def async_discover(
tasmota_automation: TasmotaTrigger, discovery_hash: DiscoveryHashType
) -> None:
@ -45,5 +48,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> N
async_discover,
)
hass.data[DATA_UNSUB].append(
hass.bus.async_listen(EVENT_DEVICE_REGISTRY_UPDATED, async_device_removed)
hass.bus.async_listen(
EVENT_DEVICE_REGISTRY_UPDATED,
async_device_removed,
event_filter=_async_device_removed_filter,
)
)