2020-10-16 06:16:07 +00:00
|
|
|
"""Provides device automations for Tasmota."""
|
|
|
|
|
|
|
|
from hatasmota.const import AUTOMATION_TYPE_TRIGGER
|
|
|
|
|
|
|
|
from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
|
|
|
from . import device_trigger
|
2020-10-30 08:56:41 +00:00
|
|
|
from .const import DATA_REMOVE_DISCOVER_COMPONENT, DATA_UNSUB
|
2020-10-16 06:16:07 +00:00
|
|
|
from .discovery import TASMOTA_DISCOVERY_ENTITY_NEW
|
|
|
|
|
|
|
|
|
2020-10-30 08:56:41 +00:00
|
|
|
async def async_remove_automations(hass, device_id):
|
|
|
|
"""Remove automations for a Tasmota device."""
|
|
|
|
await device_trigger.async_remove_triggers(hass, device_id)
|
|
|
|
|
|
|
|
|
2020-10-16 06:16:07 +00:00
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up Tasmota device automation dynamically through discovery."""
|
|
|
|
|
|
|
|
async def async_device_removed(event):
|
|
|
|
"""Handle the removal of a device."""
|
|
|
|
if event.data["action"] != "remove":
|
|
|
|
return
|
2020-10-30 08:56:41 +00:00
|
|
|
await async_remove_automations(hass, event.data["device_id"])
|
2020-10-16 06:16:07 +00:00
|
|
|
|
|
|
|
async def async_discover(tasmota_automation, discovery_hash):
|
|
|
|
"""Discover and add a Tasmota device automation."""
|
|
|
|
if tasmota_automation.automation_type == AUTOMATION_TYPE_TRIGGER:
|
|
|
|
await device_trigger.async_setup_trigger(
|
|
|
|
hass, tasmota_automation, config_entry, discovery_hash
|
|
|
|
)
|
|
|
|
|
2020-10-22 23:22:51 +00:00
|
|
|
hass.data[
|
|
|
|
DATA_REMOVE_DISCOVER_COMPONENT.format("device_automation")
|
|
|
|
] = async_dispatcher_connect(
|
2020-10-16 06:16:07 +00:00
|
|
|
hass,
|
2020-11-26 21:34:26 +00:00
|
|
|
TASMOTA_DISCOVERY_ENTITY_NEW.format("device_automation"),
|
2020-10-16 06:16:07 +00:00
|
|
|
async_discover,
|
|
|
|
)
|
2020-10-30 08:56:41 +00:00
|
|
|
hass.data[DATA_UNSUB].append(
|
|
|
|
hass.bus.async_listen(EVENT_DEVICE_REGISTRY_UPDATED, async_device_removed)
|
|
|
|
)
|