-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add homeassistant.create_label service (#713)
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
custom_components/spook/ectoplasms/homeassistant/services/create_label.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters