Skip to content

Commit

Permalink
Add icons to scripts (home-assistant#31899)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Feb 17, 2020
1 parent c788946 commit 1b7dd6c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
21 changes: 16 additions & 5 deletions homeassistant/components/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ATTR_ENTITY_ID,
ATTR_NAME,
CONF_ALIAS,
CONF_ICON,
EVENT_SCRIPT_STARTED,
SERVICE_RELOAD,
SERVICE_TOGGLE,
Expand Down Expand Up @@ -42,7 +43,8 @@

SCRIPT_ENTRY_SCHEMA = vol.Schema(
{
CONF_ALIAS: cv.string,
vol.Optional(CONF_ALIAS): cv.string,
vol.Optional(CONF_ICON): cv.icon,
vol.Required(CONF_SEQUENCE): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_DESCRIPTION, default=""): cv.string,
vol.Optional(CONF_FIELDS, default={}): {
Expand Down Expand Up @@ -207,9 +209,15 @@ async def service_handler(service):
scripts = []

for object_id, cfg in config.get(DOMAIN, {}).items():
alias = cfg.get(CONF_ALIAS, object_id)
script = ScriptEntity(hass, object_id, alias, cfg[CONF_SEQUENCE])
scripts.append(script)
scripts.append(
ScriptEntity(
hass,
object_id,
cfg.get(CONF_ALIAS, object_id),
cfg.get(CONF_ICON),
cfg[CONF_SEQUENCE],
)
)
hass.services.async_register(
DOMAIN, object_id, service_handler, schema=SCRIPT_SERVICE_SCHEMA
)
Expand All @@ -227,9 +235,12 @@ async def service_handler(service):
class ScriptEntity(ToggleEntity):
"""Representation of a script entity."""

def __init__(self, hass, object_id, name, sequence):
icon = None

def __init__(self, hass, object_id, name, icon, sequence):
"""Initialize the script."""
self.object_id = object_id
self.icon = icon
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
self.script = Script(hass, sequence, name, self.async_update_ha_state)

Expand Down
21 changes: 21 additions & 0 deletions tests/components/script/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,24 @@ async def test_extraction_functions(hass):
"device-in-both",
"device-in-last",
}


async def test_config(hass):
"""Test passing info in config."""
assert await async_setup_component(
hass,
"script",
{
"script": {
"test_script": {
"alias": "Script Name",
"icon": "mdi:party",
"sequence": [],
}
}
},
)

test_script = hass.states.get("script.test_script")
assert test_script.name == "Script Name"
assert test_script.attributes["icon"] == "mdi:party"

0 comments on commit 1b7dd6c

Please sign in to comment.