ELY3M
(ELY M.)
August 10, 2025, 4:20pm
1
I have been trying to remove test label from the email messages in this script.
I tried the imap.move function and it is not working right.
is there any way to remove the “test” label?
alias: ufo email alert
description: ""
triggers:
- trigger: event
event_type: imap_content
conditions:
- alias: Check for specific phrase in subject
condition: template
value_template: "{{ trigger.event.data['subject'] is search('work103') }}"
actions:
- action: imap.move
metadata: {}
data:
entry: 01XXXXXXXXXXXXXXXXXXXXXXG
uid: "{{ trigger.event.data.uid }}"
seen: true
target_folder: inbox
- action: light.turn_on
target:
device_id: 1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx87f
data:
brightness_pct: 100
rgb_color:
- 179
- 255
- 0
mode: single
How are you applying the “test” label?
ELY3M
(ELY M.)
August 10, 2025, 7:53pm
3
via gmail filtering system… I already resolved this…
ELY3M
(ELY M.)
August 10, 2025, 7:55pm
4
I had to make custom compound…
gmail, imap, gmail-imap
async def async_remove(call: ServiceCall) -> None:
"""Process remove email service call."""
entry_id: str = call.data[CONF_ENTRY]
uid: str = call.data[CONF_UID]
seen = bool(call.data.get(CONF_SEEN))
target_label: str = call.data[CONF_TARGET_LABEL]
_LOGGER.debug(
"Remove message %s label %s. Mark as seen: %s. Entry: %s",
uid,
target_label,
seen,
entry_id,
)
client = await async_get_imap_client(hass, entry_id)
try:
if seen:
response = await client.store(uid, "+FLAGS (\\Seen)")
raise_on_error(response, "seen_failed")
##response = await client.copy(uid, target_folder)
##raise_on_error(response, "copy_failed")
##https://stackoverflow.com/questions/17263500/how-to-remove-a-label-from-an-email-message-from-gmail-by-using-the-imap-protoco
##imap.store(item, '-X-GM-LABELS', label) # <-- Will now remove the label!
response = await client.store(uid, "-X-GM-LABELS", target_label)
raise_on_error(response, "remove_failed")
response = await asyncio.wait_for(
client.protocol.expunge(uid, by_uid=True), client.timeout
)
raise_on_error(response, "expunge_failed")
except (TimeoutError, AioImapException) as exc:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="imap_server_fail",
translation_placeholders={"error": str(exc)},
) from exc
await client.close()
hass.services.async_register(DOMAIN, "remove", async_remove, SERVICE_REMOVE_SCHEMA)