Allow specifying an icon for a scene (#31898)

This commit is contained in:
Paulus Schoutsen 2020-02-17 08:41:33 -08:00 committed by GitHub
parent 3da136b034
commit c788946430
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 13 deletions

View File

@ -11,6 +11,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
ATTR_STATE, ATTR_STATE,
CONF_ENTITIES, CONF_ENTITIES,
CONF_ICON,
CONF_ID, CONF_ID,
CONF_NAME, CONF_NAME,
CONF_PLATFORM, CONF_PLATFORM,
@ -75,16 +76,21 @@ CONF_SNAPSHOT = "snapshot_entities"
DATA_PLATFORM = f"homeassistant_scene" DATA_PLATFORM = f"homeassistant_scene"
STATES_SCHEMA = vol.All(dict, _convert_states) STATES_SCHEMA = vol.All(dict, _convert_states)
PLATFORM_SCHEMA = vol.Schema( PLATFORM_SCHEMA = vol.Schema(
{ {
vol.Required(CONF_PLATFORM): HA_DOMAIN, vol.Required(CONF_PLATFORM): HA_DOMAIN,
vol.Required(STATES): vol.All( vol.Required(STATES): vol.All(
cv.ensure_list, cv.ensure_list,
[ [
{ vol.Schema(
vol.Required(CONF_NAME): cv.string, {
vol.Required(CONF_ENTITIES): STATES_SCHEMA, vol.Optional(CONF_ID): cv.string,
} vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_ICON): cv.icon,
vol.Required(CONF_ENTITIES): STATES_SCHEMA,
}
)
], ],
), ),
}, },
@ -105,7 +111,7 @@ CREATE_SCENE_SCHEMA = vol.All(
SERVICE_APPLY = "apply" SERVICE_APPLY = "apply"
SERVICE_CREATE = "create" SERVICE_CREATE = "create"
SCENECONFIG = namedtuple("SceneConfig", [CONF_NAME, STATES]) SCENECONFIG = namedtuple("SceneConfig", [CONF_ID, CONF_NAME, CONF_ICON, STATES])
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -213,7 +219,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
_LOGGER.warning("Empty scenes are not allowed") _LOGGER.warning("Empty scenes are not allowed")
return return
scene_config = SCENECONFIG(call.data[CONF_SCENE_ID], entities) scene_config = SCENECONFIG(None, call.data[CONF_SCENE_ID], None, entities)
entity_id = f"{SCENE_DOMAIN}.{scene_config.name}" entity_id = f"{SCENE_DOMAIN}.{scene_config.name}"
old = platform.entities.get(entity_id) old = platform.entities.get(entity_id)
if old is not None: if old is not None:
@ -239,8 +245,12 @@ def _process_scenes_config(hass, async_add_entities, config):
async_add_entities( async_add_entities(
HomeAssistantScene( HomeAssistantScene(
hass, hass,
SCENECONFIG(scene[CONF_NAME], scene[CONF_ENTITIES]), SCENECONFIG(
scene.get(CONF_ID), scene.get(CONF_ID),
scene[CONF_NAME],
scene.get(CONF_ICON),
scene[CONF_ENTITIES],
),
) )
for scene in scene_config for scene in scene_config
) )
@ -249,9 +259,8 @@ def _process_scenes_config(hass, async_add_entities, config):
class HomeAssistantScene(Scene): class HomeAssistantScene(Scene):
"""A scene is a group of entities and the states we want them to be.""" """A scene is a group of entities and the states we want them to be."""
def __init__(self, hass, scene_config, scene_id=None, from_service=False): def __init__(self, hass, scene_config, from_service=False):
"""Initialize the scene.""" """Initialize the scene."""
self._id = scene_id
self.hass = hass self.hass = hass
self.scene_config = scene_config self.scene_config = scene_config
self.from_service = from_service self.from_service = from_service
@ -261,17 +270,23 @@ class HomeAssistantScene(Scene):
"""Return the name of the scene.""" """Return the name of the scene."""
return self.scene_config.name return self.scene_config.name
@property
def icon(self):
"""Return the icon of the scene."""
return self.scene_config.icon
@property @property
def unique_id(self): def unique_id(self):
"""Return unique ID.""" """Return unique ID."""
return self._id return self.scene_config.id
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the scene state attributes.""" """Return the scene state attributes."""
attributes = {ATTR_ENTITY_ID: list(self.scene_config.states)} attributes = {ATTR_ENTITY_ID: list(self.scene_config.states)}
if self._id is not None: unique_id = self.unique_id
attributes[CONF_ID] = self._id if unique_id is not None:
attributes[CONF_ID] = unique_id
return attributes return attributes
async def async_activate(self): async def async_activate(self):

View File

@ -258,3 +258,33 @@ async def test_entities_in_scene(hass):
("scene.scene_3", ["light.kitchen", "light.living_room"]), ("scene.scene_3", ["light.kitchen", "light.living_room"]),
): ):
assert ha_scene.entities_in_scene(hass, scene_id) == entities assert ha_scene.entities_in_scene(hass, scene_id) == entities
async def test_config(hass):
"""Test passing config in YAML."""
assert await async_setup_component(
hass,
"scene",
{
"scene": [
{
"id": "scene_id",
"name": "Scene Icon",
"icon": "mdi:party",
"entities": {"light.kitchen": "on"},
},
{
"name": "Scene No Icon",
"entities": {"light.kitchen": {"state": "on"}},
},
]
},
)
icon = hass.states.get("scene.scene_icon")
assert icon is not None
assert icon.attributes["icon"] == "mdi:party"
no_icon = hass.states.get("scene.scene_no_icon")
assert no_icon is not None
assert "icon" not in no_icon.attributes