diff --git a/custom_components/tahoma/__init__.py b/custom_components/tahoma/__init__.py index 8a25bbf78..87412fc4c 100644 --- a/custom_components/tahoma/__init__.py +++ b/custom_components/tahoma/__init__.py @@ -64,14 +64,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): controller = await hass.async_add_executor_job(TahomaApi, username, password) await hass.async_add_executor_job(controller.get_setup) devices = await hass.async_add_executor_job(controller.get_devices) - # scenes = api.get_action_groups() + scenes = await hass.async_add_executor_job(controller.get_action_groups) # TODO Add better exception handling except RequestException: _LOGGER.exception("Error when getting devices from the Tahoma API") return False - hass.data[DOMAIN][entry.entry_id] = {"controller": controller, "devices": []} + hass.data[DOMAIN][entry.entry_id] = {"controller": controller, "devices": [], "scenes": []} # List devices for device in devices: @@ -94,6 +94,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): _device.widget, ) + for scene in scenes: + hass.data[DOMAIN][entry.entry_id]["scenes"].append(scene) + return True diff --git a/custom_components/tahoma/scene.py b/custom_components/tahoma/scene.py new file mode 100644 index 000000000..ef95e70b6 --- /dev/null +++ b/custom_components/tahoma/scene.py @@ -0,0 +1,52 @@ +"""Support for Tahoma scenes.""" +import logging +from typing import Any + +from homeassistant.components.scene import Scene + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass, entry, async_add_entities): + """Set up the Tahoma scenes from a config entry.""" + + data = hass.data[DOMAIN][entry.entry_id] + + entities = [] + controller = data.get("controller") + + for scene in data.get("scenes"): + entities.append(TahomaScene(scene, controller)) + + async_add_entities(entities) + + +class TahomaScene(Scene): + """Representation of a Tahoma scene entity.""" + + def __init__(self, tahoma_scene, controller): + """Initialize the scene.""" + self.tahoma_scene = tahoma_scene + self.controller = controller + self._name = self.tahoma_scene.name + + def activate(self, **kwargs: Any) -> None: + """Activate the scene.""" + self.controller.launch_action_group(self.tahoma_scene.oid) + + @property + def unique_id(self) -> str: + """Return a unique ID.""" + return self.tahoma_scene.oid + + @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.""" + return {"tahoma_scene_oid": self.tahoma_scene.oid}