From 430ecc8b0829a8cf5efa2382a43d9ddd8f3776f7 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Thu, 4 Jun 2020 19:41:51 +0200 Subject: [PATCH 1/3] Add basic scene support --- custom_components/tahoma/__init__.py | 7 +++-- custom_components/tahoma/scene.py | 45 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 custom_components/tahoma/scene.py diff --git a/custom_components/tahoma/__init__.py b/custom_components/tahoma/__init__.py index 5a06b51cd..13aa89f1d 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 = TahomaApi(username, password) controller.get_setup() devices = controller.get_devices() - # scenes = api.get_action_groups() + scenes = api.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..1ca8eca13 --- /dev/null +++ b/custom_components/tahoma/scene.py @@ -0,0 +1,45 @@ +"""Support for Tahoma scenes.""" +import logging +from typing import Any + +from homeassistant.components.scene import Scene + +from . import DOMAIN as TAHOMA_DOMAIN + +_LOGGER = logging.getLogger(__name__) + +async def async_setup_entry(hass, entry, async_add_entities): + """Set up the Tahoma locks 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 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} From c2214166bdbb18002e90d572f1765d63be8f5432 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Thu, 4 Jun 2020 19:43:13 +0200 Subject: [PATCH 2/3] Bugfixes --- custom_components/tahoma/__init__.py | 2 +- custom_components/tahoma/scene.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/custom_components/tahoma/__init__.py b/custom_components/tahoma/__init__.py index 13aa89f1d..9cb854b93 100644 --- a/custom_components/tahoma/__init__.py +++ b/custom_components/tahoma/__init__.py @@ -64,7 +64,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): controller = TahomaApi(username, password) controller.get_setup() devices = controller.get_devices() - scenes = api.get_action_groups() + scenes = controller.get_action_groups() # TODO Add better exception handling except RequestException: diff --git a/custom_components/tahoma/scene.py b/custom_components/tahoma/scene.py index 1ca8eca13..56f03cb9d 100644 --- a/custom_components/tahoma/scene.py +++ b/custom_components/tahoma/scene.py @@ -4,10 +4,11 @@ from homeassistant.components.scene import Scene -from . import DOMAIN as TAHOMA_DOMAIN +from .const import DOMAIN _LOGGER = logging.getLogger(__name__) + async def async_setup_entry(hass, entry, async_add_entities): """Set up the Tahoma locks from a config entry.""" @@ -21,6 +22,7 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(entities) + class TahomaScene(Scene): """Representation of a Tahoma scene entity.""" @@ -34,6 +36,11 @@ 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.""" From 9ef7ddde4ef6546f7192d76b7ad3babe149deb54 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Thu, 4 Jun 2020 21:52:35 +0200 Subject: [PATCH 3/3] Fix string --- custom_components/tahoma/scene.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/tahoma/scene.py b/custom_components/tahoma/scene.py index 56f03cb9d..ef95e70b6 100644 --- a/custom_components/tahoma/scene.py +++ b/custom_components/tahoma/scene.py @@ -10,7 +10,7 @@ async def async_setup_entry(hass, entry, async_add_entities): - """Set up the Tahoma locks from a config entry.""" + """Set up the Tahoma scenes from a config entry.""" data = hass.data[DOMAIN][entry.entry_id]