Skip to content

Commit 430e296

Browse files
authored
Add homeassistant.remove_label_from_entity service (#720)
1 parent 846d098 commit 430e296

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Spook - Your homie."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
import voluptuous as vol
8+
9+
from homeassistant.components.homeassistant import DOMAIN
10+
from homeassistant.helpers import config_validation as cv, entity_registry as er
11+
12+
from ....services import AbstractSpookAdminService
13+
14+
if TYPE_CHECKING:
15+
from homeassistant.core import ServiceCall
16+
17+
18+
class SpookService(AbstractSpookAdminService):
19+
"""Home Assistant service to remove a label from an entity."""
20+
21+
domain = DOMAIN
22+
service = "remove_label_from_entity"
23+
schema = {
24+
vol.Required("label_id"): vol.All(cv.ensure_list, [cv.string]),
25+
vol.Required("entity_id"): vol.All(cv.ensure_list, [cv.string]),
26+
}
27+
28+
async def async_handle_service(self, call: ServiceCall) -> None:
29+
"""Handle the service call."""
30+
entity_registry = er.async_get(self.hass)
31+
for entity_id in call.data["entity_id"]:
32+
if entity_entry := entity_registry.async_get(entity_id):
33+
labels = entity_entry.labels.copy()
34+
labels.difference_update(call.data["label_id"])
35+
entity_registry.async_update_entity(entity_id, labels=labels)

custom_components/spook/services.yaml

+22-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ homeassistant_remove_label_from_area:
602602
homeassistant_remove_label_from_device:
603603
name: Remove a label from a device 👻
604604
description: >-
605-
Removes a label to a device. If multiple labels or multiple devices are
605+
Removes a label from a device. If multiple labels or multiple devices are
606606
provided, all combinations will be removed.
607607
fields:
608608
label_id:
@@ -620,6 +620,27 @@ homeassistant_remove_label_from_device:
620620
device:
621621
multiple: true
622622

623+
homeassistant_remove_label_from_entity:
624+
name: Remove a label from an entity 👻
625+
description: >-
626+
Removes a label from an entity. If multiple labels or multiple entities are
627+
provided, all combinations will be removed.
628+
fields:
629+
label_id:
630+
name: Label ID
631+
description: The ID(s) of the label(s) to remove from the entity/entities.
632+
required: true
633+
selector:
634+
label:
635+
multiple: true
636+
entity_id:
637+
name: Entity ID
638+
description: The ID(s) of the entity/entities to remove the label(s) from.
639+
required: true
640+
selector:
641+
entity:
642+
multiple: true
643+
623644
homeassistant_delete_label:
624645
name: Delete a label 👻
625646
description: >-

0 commit comments

Comments
 (0)