Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scenes #14

Merged
merged 3 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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:
Expand All @@ -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


Expand Down
52 changes: 52 additions & 0 deletions custom_components/tahoma/scene.py
Original file line number Diff line number Diff line change
@@ -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}