Skip to content

Commit

Permalink
Add homeassistant.remove_label_from_entity service
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed Apr 4, 2024
1 parent 846d098 commit 6723508
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Spook - Your homie."""

from __future__ import annotations

from typing import TYPE_CHECKING

import voluptuous as vol

from homeassistant.components.homeassistant import DOMAIN
from homeassistant.helpers import config_validation as cv, entity_registry as er

from ....services import AbstractSpookAdminService

if TYPE_CHECKING:
from homeassistant.core import ServiceCall


class SpookService(AbstractSpookAdminService):
"""Home Assistant service to remove a label from an entity."""

domain = DOMAIN
service = "remove_label_from_entity"
schema = {
vol.Required("label_id"): vol.All(cv.ensure_list, [cv.string]),
vol.Required("entity_id"): vol.All(cv.ensure_list, [cv.string]),
}

async def async_handle_service(self, call: ServiceCall) -> None:
"""Handle the service call."""
entity_registry = er.async_get(self.hass)
for entity_id in call.data["entity_id"]:
if entity_entry := entity_registry.async_get(entity_id):
labels = entity_entry.labels.copy()
labels.difference_update(call.data["label_id"])
entity_registry.async_update_entity(entity_id, labels=labels)
23 changes: 22 additions & 1 deletion custom_components/spook/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ homeassistant_remove_label_from_area:
homeassistant_remove_label_from_device:
name: Remove a label from a device 👻
description: >-
Removes a label to a device. If multiple labels or multiple devices are
Removes a label from a device. If multiple labels or multiple devices are
provided, all combinations will be removed.
fields:
label_id:
Expand All @@ -620,6 +620,27 @@ homeassistant_remove_label_from_device:
device:
multiple: true

homeassistant_remove_label_from_entity:
name: Remove a label from an entity 👻
description: >-
Removes a label from an entity. If multiple labels or multiple entities are
provided, all combinations will be removed.
fields:
label_id:
name: Label ID
description: The ID(s) of the label(s) to remove from the entity/entities.
required: true
selector:
label:
multiple: true
entity_id:
name: Entity ID
description: The ID(s) of the entity/entities to remove the label(s) from.
required: true
selector:
entity:
multiple: true

homeassistant_delete_label:
name: Delete a label 👻
description: >-
Expand Down

0 comments on commit 6723508

Please sign in to comment.