Skip to content

Commit

Permalink
Added controller_id to empty scene
Browse files Browse the repository at this point in the history
  • Loading branch information
c503ghosh committed Sep 13, 2024
1 parent e0451e2 commit a4bbf66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
19 changes: 12 additions & 7 deletions custom_components/dirigera_platform/dirigera_lib_patch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

from typing import Any, Dict, List, Optional
from typing import Any, Optional, Dict

from dirigera import Hub

from typing import Any, Optional, Dict
from dirigera.devices.device import Attributes, Device
from dirigera.hub.abstract_smart_home_hub import AbstractSmartHomeHub

Expand All @@ -23,15 +22,23 @@ def get_controllers(self) -> List[ControllerX]:
controllers = list(filter(lambda x: x["type"] == "controller", devices))
return [dict_to_controller(controller, self) for controller in controllers]

def create_empty_scene(self, name:str):
def create_empty_scene_for_controller(self, name:str, controller_id: str):
data = {
"info": { "name" : name , "icon" : "scenes_trophy"},
"type": "customScene",
"triggers": [],
"triggers": [ {
"type" : "controller",
"disabled": False,
"trigger": {
"controllerType" : "shortcutController",
"buttonIndex" : 0,
"device_id" : controller_id
}
}
],
"actions": []
}

#data = camelize_dict(data) # type: ignore
response_dict = self.post(
"/scenes",
data=data,
Expand All @@ -42,7 +49,6 @@ class ControllerAttributesX(Attributes):
battery_percentage: Optional[int] = None
switch_label: Optional[str] = None


class ControllerX(Device):
dirigera_client: AbstractSmartHomeHub
attributes: ControllerAttributesX
Expand All @@ -61,7 +67,6 @@ def set_name(self, name: str) -> None:
self.dirigera_client.patch(route=f"/devices/{self.id}", data=data)
self.attributes.custom_name = name


def dict_to_controller(
data: Dict[str, Any], dirigera_client: AbstractSmartHomeHub
) -> ControllerX:
Expand Down
29 changes: 15 additions & 14 deletions custom_components/dirigera_platform/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,26 @@ async def async_setup_entry(

hub_controllers = await hass.async_add_executor_job(hub.get_controllers)
logger.error(f"Got {len(hub_controllers)} controllers...")
controller_devices = [
ikea_controller(hass, hub, controller_device)
for controller_device in hub_controllers
# Only create a battery sensor entity if the device reports battery percentage
# This is not the case of the second device for SOMRIG controllers
if controller_device.attributes.battery_percentage
]

for controller in controller_devices:
# Controllers with more one button are returned as spearate controllers
# their uniqueid has _1, _2 suffixes. Only the primary controller has
# battery % attribute which we shall use to identify
controller_devices = []

for controller_device in hub_controllers:
controller : ikea_controller = ikea_controller(hass, hub, controller_device)

# Hack to create empty scene so that we can associate it the controller
# so that click of buttons on the controller can generate events on the hub
#hub.create(name=f"dirigera_platform_empty_scene_{controller.unique_id}",icon="scenes_heart")

scene_name=f"dirigera_platform_empty_scene_{controller.unique_id}"
#scene_info.name = f"dirigera_platform_empty_scene_{controller.unique_id}"
#scene_info.icon = "scenes_heart"
logger.error(f"Creating empty scene {scene_name} for controller...")
await hass.async_add_executor_job(hub.create_empty_scene,scene_name)


logger.error(f"Creating empty scene {scene_name} for controller {controller.unique_id}...")
await hass.async_add_executor_job(hub.create_empty_scene,scene_name, controller.unique_id)

if controller_device.attributes.battery_percentage :
controller_devices.append(controller)
env_sensors = []
for env_device in env_devices:
# For each device setup up multiple entities
Expand Down

0 comments on commit a4bbf66

Please sign in to comment.