Skip to content

Commit

Permalink
Add homeassistant.create_label service (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Apr 3, 2024
1 parent 50e9b79 commit 4069d98
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""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, label_registry as lr

from ....services import AbstractSpookAdminService

if TYPE_CHECKING:
from homeassistant.core import ServiceCall

SUPPORTED_LABEL_THEME_COLORS = {
"primary",
"accent",
"disabled",
"amber",
"black",
"blue-grey",
"blue",
"brown",
"cyan",
"dark-grey",
"deep-orange",
"deep-purple",
"green",
"grey",
"indigo",
"light-blue",
"light-green",
"light-grey",
"lime",
"orange",
"pink",
"purple",
"red",
"teal",
"white",
"yellow",
}


class SpookService(AbstractSpookAdminService):
"""Home Assistant service to create labels on the fly."""

domain = DOMAIN
service = "create_label"
schema = {
vol.Required("name"): cv.string,
vol.Optional("color"): vol.Any(
cv.color_hex, vol.In(SUPPORTED_LABEL_THEME_COLORS)
),
vol.Optional("description"): cv.string,
vol.Optional("icon"): cv.icon,
}

async def async_handle_service(self, call: ServiceCall) -> None:
"""Handle the service call."""
label_registry = lr.async_get(self.hass)
label_registry.async_create(
name=call.data["name"],
color=call.data.get("color"),
description=call.data.get("description"),
icon=call.data.get("icon"),
)
79 changes: 79 additions & 0 deletions custom_components/spook/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,85 @@ homeassistant_delete_floor:
selector:
floor:

homeassistant_create_label:
name: Create a label 👻
description: >-
Creates a new label on the fly.
fields:
name:
name: Name
description: The name of the label to create.
required: true
selector:
text:
description:
name: Description
description: Description for the label.
required: true
selector:
text:
icon:
name: Icon
description: Icon to use for the label.
required: false
selector:
icon:
placeholder: mdi:tag
color:
name: Color
description: >-
Color to use for the label. Can be a color name from the list, or a
hex color code (like #FF0000).
selector:
select:
options:
- label: Primary theme color
value: primary
- label: Accent theme color
value: accent
- label: Disabled theme color
value: disabled
- label: Red
value: red
- label: Pink
value: pink
- label: Purple
value: purple
- label: Deep purple
value: deep_purple
- label: Indigo
value: indigo
- label: Blue
value: blue
- label: Light blue
value: light_blue
- label: Cyan
value: cyan
- label: Teal
value: teal
- label: Green
value: green
- label: Light green
value: light_green
- label: Lime
value: lime
- label: Yellow
value: yellow
- label: Orange
value: orange
- label: Deep orange
value: deep_orange
- label: Brown
value: brown
- label: Grey
value: grey
- label: Blue grey
value: blue_grey
- label: Black
value: black
- label: White
value: white

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

0 comments on commit 4069d98

Please sign in to comment.