2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Vera scenes."""
|
2017-12-07 06:47:19 +00:00
|
|
|
import logging
|
2020-04-03 07:49:50 +00:00
|
|
|
from typing import Callable, List
|
2017-12-07 06:47:19 +00:00
|
|
|
|
|
|
|
from homeassistant.components.scene import Scene
|
2020-04-03 07:49:50 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
from .const import DOMAIN, VERA_ID_FORMAT
|
2017-12-07 06:47:19 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor config entry."""
|
|
|
|
controller_data = hass.data[DOMAIN]
|
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
2020-04-03 07:49:50 +00:00
|
|
|
VeraScene(device, controller_data.controller)
|
|
|
|
for device in controller_data.scenes
|
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-12-07 06:47:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VeraScene(Scene):
|
|
|
|
"""Representation of a Vera scene entity."""
|
|
|
|
|
|
|
|
def __init__(self, vera_scene, controller):
|
|
|
|
"""Initialize the scene."""
|
|
|
|
self.vera_scene = vera_scene
|
|
|
|
self.controller = controller
|
|
|
|
|
|
|
|
self._name = self.vera_scene.name
|
|
|
|
# Append device id to prevent name clashes in HA.
|
|
|
|
self.vera_id = VERA_ID_FORMAT.format(
|
2019-07-31 19:25:30 +00:00
|
|
|
slugify(vera_scene.name), vera_scene.scene_id
|
|
|
|
)
|
2017-12-07 06:47:19 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the scene status."""
|
|
|
|
self.vera_scene.refresh()
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def activate(self):
|
2017-12-07 06:47:19 +00:00
|
|
|
"""Activate the scene."""
|
|
|
|
self.vera_scene.activate()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the scene."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the scene."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"vera_scene_id": self.vera_scene.vera_scene_id}
|