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 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 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
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
2 changes: 1 addition & 1 deletion custom_components/adaptive_lighting/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"documentation": "https://github.com/basnijholt/adaptive-lighting#readme",
"iot_class": "calculated",
"issue_tracker": "https://github.com/basnijholt/adaptive-lighting/issues",
"requirements": ["ulid-transform"],
"requirements": ["ulid-transform","aiohue-BenoitAnastay"],
BenoitAnastay marked this conversation as resolved.
Show resolved Hide resolved
"version": "1.22.0"
}
58 changes: 58 additions & 0 deletions custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from datetime import timedelta
from typing import TYPE_CHECKING, Any, Literal

import aiohue_BenoitAnastay
import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
import ulid_transform
Expand Down Expand Up @@ -88,6 +89,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 @@ -114,6 +116,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 @@ -813,7 +816,20 @@ 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]

# 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 @@ -1241,6 +1257,8 @@ async def prepare_adaptation_data(

context = context or self.create_context("adapt_lights")

await self.update_hue_run(service_data)
BenoitAnastay marked this conversation as resolved.
Show resolved Hide resolved

return prepare_adaptation_data(
self.hass,
light,
Expand Down Expand Up @@ -1564,6 +1582,46 @@ async def _sleep_mode_switch_state_event_action(self, event: Event) -> None:
force=True,
)

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

_LOGGER.debug(
"%s: Will updates scenes containing %s",
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(
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,
)


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
2 changes: 2 additions & 0 deletions scripts/setup-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ 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 aiohue-BenoitAnastay # same as above
pip install $(python test_dependencies.py)
Loading