Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add homeassistant.remove_label_from_entity service #720

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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