Skip to content

Commit

Permalink
⬆️ Update to API V2
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitAnastay committed Sep 16, 2024
1 parent 3c16d17 commit 9163079
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
17 changes: 17 additions & 0 deletions custom_components/adaptive_lighting/hue_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Help fixing a serialization issue"""

from dataclasses import dataclass

@dataclass
class ResourceIdentifier:
"""
Represent a ResourceIdentifier object as used by the Hue api.
clip-api.schema.json#/definitions/ResourceIdentifierGet
clip-api.schema.json#/definitions/ResourceIdentifierPost
clip-api.schema.json#/definitions/ResourceIdentifierPut
clip-api.schema.json#/definitions/ResourceIdentifierDelete
"""

rid: str # UUID
rtype: str
68 changes: 28 additions & 40 deletions custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from datetime import timedelta
from typing import TYPE_CHECKING, Any, Literal

import aiohue_BenoitAnastay
from aiohue import HueBridgeV2
from aiohue.v2.models.scene import ScenePut
from .hue_utils import ResourceIdentifier

import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
import ulid_transform
Expand Down Expand Up @@ -818,18 +821,6 @@ def __init__(
self.lights: list[str] = data[CONF_LIGHTS]
self.hue_keyword = data[CONF_HUE_KEYWORD]

# Set bridge
try:
config_entries = hass.config_entries.async_entries(domain="hue")
config_entry = config_entries[0]
self.hue_bridge = aiohue_BenoitAnastay.HueBridgeV1(
config_entry.data["host"],
config_entry.data["api_key"],
)
except Exception:
_LOGGER.exception(
"Cannot set hue bridge",
)
# backup data for use in change_switch_settings "configuration" CONF_USE_DEFAULTS
self._config_backup = deepcopy(data)
self._set_changeable_settings(data=data, defaults=None)
Expand Down Expand Up @@ -1592,35 +1583,32 @@ async def update_hue_run(self, service_data: ServiceData):
self._name,
self.hue_keyword,
)
await self.hue_bridge.initialize()
for scene_id in self.hue_bridge.scenes:
scene = self.hue_bridge.scenes[scene_id]
if self.hue_keyword in scene.name:
color_temp = color_temperature_kelvin_to_mired(
config_entries = self.hass.config_entries.async_entries(domain="hue")
config_entry = config_entries[0]

color_temp = color_temperature_kelvin_to_mired(
service_data[ATTR_COLOR_TEMP_KELVIN],
)
brightness = round(254 * self._settings["brightness_pct"] / 100)
_LOGGER.debug(
"%s: Updating %s with values bri:%s, color_temp:%s",
self._name,
scene.name,
brightness,
color_temp,
)
lightstates = await scene.lightstates
for light_id in scene.lights:
try:
await scene.set_lightstate(
id=light_id,
on=lightstates[light_id]["on"],
bri=brightness,
ct=color_temp,
)
except Exception:
_LOGGER.exception(
"Cannot update scene %s",
id,
)
brightness = self._settings["brightness_pct"]

async with HueBridgeV2(config_entry.data["host"], config_entry.data["api_key"]) as bridge:
for scene in bridge.scenes:
if self.hue_keyword in scene.metadata.name:
_LOGGER.debug(
"%s: Updating %s with values bri:%s, color_temp:%s",
self._name,
scene.metadata.name,
brightness,
color_temp,
)
actions = list()
for action in scene.actions:
action.target=ResourceIdentifier(rid=action.target.rid,rtype='light')
action.action.color_temperature.mirek=color_temp
action.action.dimming.brightness=brightness
actions.append(action)
update_obj = ScenePut(actions=actions)
await bridge.scenes.scene.update(scene.id, update_obj)


class SimpleSwitch(SwitchEntity, RestoreEntity):
Expand Down

0 comments on commit 9163079

Please sign in to comment.