Skip to content

Commit

Permalink
Merge branch 'master' into add_occupancy_and_async
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl authored Jun 4, 2020
2 parents 1aac669 + 1a9bfc0 commit 5f2b138
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
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 = 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:
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}

0 comments on commit 5f2b138

Please sign in to comment.