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 an option to update hue scenes based on a keywork #1019

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f9bc6b9
initial reimplementation of hue scene updating
BenoitAnastay Jul 1, 2024
28e9170
Fixs
BenoitAnastay Jul 1, 2024
6c2308b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 1, 2024
1714b62
➕ pip install aiohue-BenoitAnastay
BenoitAnastay Jul 1, 2024
e8bd933
Merge branch 'main' of https://github.com/BenoitAnastay/adaptive-ligh…
BenoitAnastay Jul 1, 2024
9d19f11
🚨 a bit of lint
BenoitAnastay Jul 1, 2024
9e50753
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 1, 2024
426696c
➕ add pip install aiohue
BenoitAnastay Jul 1, 2024
25f8406
avoid oob values, convert kelvin to mired
BenoitAnastay Jul 2, 2024
5d680b4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2024
ae3a727
Cleanup dependencies
BenoitAnastay Sep 16, 2024
3c16d17
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
9163079
⬆️ Update to API V2
BenoitAnastay Sep 16, 2024
6968f13
Merge branch 'basnijholt:main' into main
BenoitAnastay Sep 16, 2024
e71bbef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
4516a89
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
30fd0d8
🚨 lint
BenoitAnastay Sep 16, 2024
6e4a419
🚨 lint
BenoitAnastay Sep 16, 2024
f21c01a
Merge branch 'main' of https://github.com/BenoitAnastay/adaptive-ligh…
BenoitAnastay Sep 16, 2024
5e40647
🎨 Set `color_temp` for lights individually
BenoitAnastay Sep 16, 2024
d8a4f55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
47bb470
🐛 Updates scenes when lights are off
BenoitAnastay Sep 16, 2024
40357ac
🐛 Fix `internal servor error` with some values
BenoitAnastay Sep 21, 2024
908081c
⚡️ Add a 5 minutes delay between updates of each keyword
BenoitAnastay Sep 22, 2024
d81c77a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
56cefce
🚨 lint
BenoitAnastay Sep 23, 2024
2e42467
Merge branch 'main' into main
BenoitAnastay Jan 8, 2025
51ac388
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 8, 2025
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
6 changes: 6 additions & 0 deletions custom_components/adaptive_lighting/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@
'documented defaults), or "configuration" (reverts to switch config defaults). ⚙️'
)

CONF_HUE_KEYWORD, DEFAULT_HUE_KEYWORD = "hue_keyword", "None"
DOCS[CONF_HUE_KEYWORD] = (
"Scenes witch contain this keyword will be updated on the HUE bridge"
)
TURNING_OFF_DELAY = 5

DOCS_MANUAL_CONTROL = {
Expand Down Expand Up @@ -367,6 +371,7 @@ def int_between(min_int, max_int):
(CONF_INTERCEPT, DEFAULT_INTERCEPT, bool),
(CONF_MULTI_LIGHT_INTERCEPT, DEFAULT_MULTI_LIGHT_INTERCEPT, bool),
(CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, DEFAULT_INCLUDE_CONFIG_IN_ATTRIBUTES, bool),
(CONF_HUE_KEYWORD, NONE_STR, str),
]


Expand Down Expand Up @@ -435,6 +440,7 @@ def apply_service_schema(initial_transition: int = 1):
vol.Optional(ATTR_ADAPT_COLOR, default=True): cv.boolean,
vol.Optional(CONF_PREFER_RGB_COLOR, default=False): cv.boolean,
vol.Optional(CONF_TURN_ON_LIGHTS, default=False): cv.boolean,
vol.Optional(CONF_HUE_KEYWORD): cv.string,
},
)

Expand Down
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
59 changes: 59 additions & 0 deletions custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import homeassistant.util.dt as dt_util
import ulid_transform
import voluptuous as vol
from aiohue import HueBridgeV2
from aiohue.v2.models.scene import ScenePut
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
Expand Down Expand Up @@ -74,6 +76,8 @@
from homeassistant.helpers import entity_platform, entity_registry
from homeassistant.helpers.entity_component import async_update_entity

from .hue_utils import ResourceIdentifier

if [MAJOR_VERSION, MINOR_VERSION] < [2023, 9]:
from homeassistant.helpers.entity import DeviceInfo
else:
Expand All @@ -89,6 +93,7 @@
from homeassistant.loader import bind_hass
from homeassistant.util import slugify
from homeassistant.util.color import (
color_temperature_kelvin_to_mired,
color_temperature_to_rgb,
color_xy_to_RGB,
)
Expand All @@ -115,6 +120,7 @@
CONF_BRIGHTNESS_MODE_TIME_DARK,
CONF_BRIGHTNESS_MODE_TIME_LIGHT,
CONF_DETECT_NON_HA_CHANGES,
CONF_HUE_KEYWORD,
CONF_INCLUDE_CONFIG_IN_ATTRIBUTES,
CONF_INITIAL_TRANSITION,
CONF_INTERCEPT,
Expand Down Expand Up @@ -817,6 +823,7 @@ def __init__(
self._name = data[CONF_NAME]
self._interval: timedelta = data[CONF_INTERVAL]
self.lights: list[str] = data[CONF_LIGHTS]
self.hue_keyword = data[CONF_HUE_KEYWORD]

# backup data for use in change_switch_settings "configuration" CONF_USE_DEFAULTS
self._config_backup = deepcopy(data)
Expand Down Expand Up @@ -1392,6 +1399,8 @@ async def _update_attrs_and_maybe_adapt_lights( # noqa: PLR0912
)
self.async_write_ha_state()

await self.update_hue_run()

if not force and self._only_once:
return

Expand Down Expand Up @@ -1568,6 +1577,56 @@ async def _sleep_mode_switch_state_event_action(self, event: Event) -> None:
force=True,
)

async def update_hue_run(self):
"""Function updating HUE scene."""
if self.hue_keyword is None:
return

_LOGGER.debug(
"%s: Will updates scenes containing %s",
self._name,
self.hue_keyword,
)
config_entries = self.hass.config_entries.async_entries(domain="hue")
config_entry = config_entries[0]

color_temp = color_temperature_kelvin_to_mired(
self._settings["color_temp_kelvin"],
)

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 = []
for action in scene.actions:
action.target = ResourceIdentifier(
rid=action.target.rid,
rtype="light",
)
light = bridge.lights.get(action.target.rid)
color_temp = clamp(
color_temp,
light.color_temperature.mirek_schema.mirek_minimum,
light.color_temperature.mirek_schema.mirek_maximum,
)
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):
"""Representation of a Adaptive Lighting switch."""
Expand Down
10 changes: 8 additions & 2 deletions custom_components/adaptive_lighting/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"skip_redundant_commands": "skip_redundant_commands: Skip sending adaptation commands whose target state already equals the light's known state. Minimizes network traffic and improves the adaptation responsivity in some situations. 📉Disable if physical light states get out of sync with HA's recorded state.",
"intercept": "intercept: Intercept and adapt `light.turn_on` calls to enabling instantaneous color and brightness adaptation. 🏎️ Disable for lights that do not support `light.turn_on` with color and brightness.",
"multi_light_intercept": "multi_light_intercept: Intercept and adapt `light.turn_on` calls that target multiple lights. ➗⚠️ This might result in splitting up a single `light.turn_on` call into multiple calls, e.g., when lights are in different switches. Requires `intercept` to be enabled.",
"include_config_in_attributes": "include_config_in_attributes: Show all options as attributes on the switch in Home Assistant when set to `true`. 📝"
"include_config_in_attributes": "include_config_in_attributes: Show all options as attributes on the switch in Home Assistant when set to `true`. 📝",
"hue_keyword": "hue_keyword"
},
"data_description": {
"interval": "Frequency to adapt the lights, in seconds. 🔄",
Expand All @@ -81,7 +82,8 @@
"brightness_mode_time_light": "(Ignored if `brightness_mode='default'`) The duration in seconds to ramp up/down the brightness after/before sunrise/sunset. 📈📉.",
"autoreset_control_seconds": "Automatically reset the manual control after a number of seconds. Set to 0 to disable. ⏲️",
"send_split_delay": "Delay (ms) between `separate_turn_on_commands` for lights that don't support simultaneous brightness and color setting. ⏲️",
"adapt_delay": "Wait time (seconds) between light turn on and Adaptive Lighting applying changes. Might help to avoid flickering. ⏲️"
"adapt_delay": "Wait time (seconds) between light turn on and Adaptive Lighting applying changes. Might help to avoid flickering. ⏲️",
"hue_keyword": "Set the HUE scene keyword to update"
}
}
},
Expand Down Expand Up @@ -262,6 +264,10 @@
"autoreset_control_seconds": {
"description": "Automatically reset the manual control after a number of seconds. Set to 0 to disable. ⏲️",
"name": "autoreset_control_seconds"
},
"hue_keyword": {
"description": "Set the HUE scene keyword to update",
"name": "hue_keyword"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions scripts/setup-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ pip install -r core/requirements_test.txt

pip install -e core/
pip install ulid-transform # this is in Adaptive-lighting's manifest.json
pip install aiohue
BenoitAnastay marked this conversation as resolved.
Show resolved Hide resolved
pip install $(python test_dependencies.py)