Skip to content

Commit 6baf1f3

Browse files
committed
Manual colour simulation now uses set number correctly
1 parent b6c575a commit 6baf1f3

File tree

7 files changed

+62
-29
lines changed

7 files changed

+62
-29
lines changed

custom_components/juwel_helialux/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3636
async def async_unload_entry(hass, entry):
3737
"""Handle removal of an entry."""
3838
unload_ok = await hass.config_entries.async_unload_platforms(entry, ["sensor", "light", "select", "binary_sensor","number","switch"])
39-
return unload_ok
39+
return unload_ok

custom_components/juwel_helialux/coordinator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ async def _async_update_data(self):
102102

103103
except Exception as e:
104104
_LOGGER.error("Error fetching data: %s", e)
105-
return self.data or {}
105+
return self.data or {}

custom_components/juwel_helialux/light.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,34 @@ async def async_turn_on(self, **kwargs):
124124
brightness = kwargs.get("brightness", 255)
125125
rgbw_color = kwargs.get("rgbw_color", (255, 255, 255, 255))
126126

127-
# Convert to device scale (0-100) - only do this once!
128-
white = min(100, max(0, round(rgbw_color[3] / 2.55)))
129-
blue = min(100, max(0, round(rgbw_color[2] / 2.55)))
130-
green = min(100, max(0, round(rgbw_color[1] / 2.55)))
131-
red = min(100, max(0, round(rgbw_color[0] / 2.55)))
127+
# Convert to device scale (0-100)
128+
white = min(100, max(0, rgbw_color[3] / 2.55))
129+
blue = min(100, max(0, rgbw_color[2] / 2.55))
130+
green = min(100, max(0, rgbw_color[1] / 2.55))
131+
red = min(100, max(0, rgbw_color[0] / 2.55))
132132

133133
# Apply brightness scaling if needed
134134
if brightness < 255:
135135
scale = brightness / 255.0
136-
white = min(100, round(white * scale))
137-
blue = min(100, round(blue * scale))
138-
green = min(100, round(green * scale))
139-
red = min(100, round(red * scale))
136+
white = min(100, white * scale)
137+
blue = min(100, blue * scale)
138+
green = min(100, green * scale)
139+
red = min(100, red * scale)
140140

141141
_LOGGER.debug("Setting light to W:%d B:%d G:%d R:%d", white, blue, green, red)
142142

143+
# Enable manual override for 5 seconds
144+
await self.coordinator.set_manual_override(True, 5)
145+
143146
try:
144-
# Set the light state - values are already in 0-100 range
145-
await self._controller.start_manual_color_simulation(1439)
147+
# Set the light state
148+
#await self._controller.start_manual_color_simulation(1439)
149+
# duration_hours = self.hass.states.get(f"number.{self._attr_unique_id}_manual_color_simulation_duration").state
150+
duration_hours = self.coordinator.data.get("manual_color_simulation_duration", 12)
151+
duration_minutes = int(float(duration_hours) * 60)
152+
_LOGGER.debug("Manual Hours: %s Manual Minutes: %s", duration_hours, duration_minutes)
153+
# Set the light state with the configured duration
154+
await self._controller.start_manual_color_simulation(duration_minutes)
146155
await self._controller.set_manual_color(white, blue, green, red)
147156

148157
# Update local state immediately
@@ -153,15 +162,18 @@ async def async_turn_on(self, **kwargs):
153162

154163
except Exception as e:
155164
_LOGGER.error("Error setting light state: %s", e)
165+
await self.coordinator.set_manual_override(False)
156166
raise
157167

158168
async def async_turn_off(self, **kwargs):
159169
"""Turn the light off."""
160170
_LOGGER.debug("Turning off Juwel Helialux light")
161-
await self._controller.start_manual_color_simulation(1439)
171+
duration_hours = self.coordinator.data.get("manual_color_simulation_duration", 12) # default 12 hours
172+
duration_minutes = int(duration_hours * 60)
173+
await self._controller.start_manual_color_simulation(duration_minutes)
162174
await self._controller.set_manual_color(0, 0, 0, 0)
163175
self._attr_is_on = False
164176
self._attr_brightness = 0
165177
self._attr_rgbw_color = (0, 0, 0, 0)
166178

167-
self.async_write_ha_state()
179+
self.async_write_ha_state()

custom_components/juwel_helialux/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"integration_type": "device",
99
"iot_class": "local_polling",
1010
"issue_tracker": "https://github.com/MrSleeps/Juwel-HeliaLux-Home-Assistant-Custom-Component/issues",
11-
"platforms": ["sensor", "light", "select", "binary_sensor"],
11+
"platforms": ["sensor", "light", "select", "binary_sensor", "number", "switch"],
1212
"requirements": ["aiohttp"],
13-
"version": "2.0.3"
13+
"version": "2.0.4"
1414
}

custom_components/juwel_helialux/number.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ def native_value(self):
4444
return self._state
4545

4646
async def async_set_native_value(self, value):
47+
#"""Set the duration value."""
48+
#self._state = int(value)
49+
#self.async_write_ha_state()
50+
#_LOGGER.debug(f"Set {self.entity_id} to {value} minutes")
51+
4752
"""Set the duration value."""
48-
self._state = int(value)
53+
self._state = float(value)
54+
# Store the value in the coordinator
55+
self.coordinator.data[f"{self._attr_translation_key}_duration"] = self._state
4956
self.async_write_ha_state()
50-
_LOGGER.debug(f"Set {self.entity_id} to {value} minutes")
57+
_LOGGER.debug(f"Set {self.entity_id} to {value} hours")
5158

5259
async def async_update(self):
5360
"""Update the state from HA data."""

custom_components/juwel_helialux/pyhelialux/pyHelialux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,4 @@ async def stop_manual_daytime_simulation(self):
350350
if response.status != 200:
351351
_LOGGER.error(f"Failed to stop manual daytime simulation: {response.status}")
352352
except Exception as e:
353-
_LOGGER.error(f"Error stopping manual daytime simulation: {e}")
353+
_LOGGER.error(f"Error stopping manual daytime simulation: {e}")

custom_components/juwel_helialux/switch.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,29 @@ def __init__(self, coordinator, tank_name, tank_id):
6565

6666
async def async_turn_on(self, **kwargs):
6767
"""Turn on manual color simulation using the duration set in number helper."""
68-
duration_entity = f"number.{self.tank_id}_manual_color_simulation_duration"
69-
duration_state = self.coordinator.hass.states.get(duration_entity)
70-
duration = int(duration_state.state) * 60 if duration_state else 60 # Convert hours to minutes
71-
72-
_LOGGER.debug(f"Starting manual color simulation for {duration} minutes")
73-
74-
await self.coordinator.helialux.start_manual_color_simulation(duration)
75-
await self.coordinator.async_refresh()
76-
self._update_state()
68+
try:
69+
duration_entity = f"number.{self.tank_id}_manual_color_simulation_duration"
70+
duration_state = self.coordinator.hass.states.get(duration_entity)
71+
72+
if duration_state is None:
73+
_LOGGER.warning("Duration number entity not found, using default 1 hour")
74+
duration = 60 # Default to 1 hour in minutes
75+
else:
76+
try:
77+
duration = float(duration_state.state) * 60 # Convert hours to minutes
78+
duration = max(1, min(1440, duration)) # Clamp between 1 min and 24 hours
79+
except (ValueError, TypeError) as e:
80+
_LOGGER.warning(f"Invalid duration value: {duration_state.state}, using default 1 hour. Error: {e}")
81+
duration = 60
82+
83+
_LOGGER.debug(f"Starting manual color simulation for {duration} minutes")
84+
await self.coordinator.helialux.start_manual_color_simulation(int(duration))
85+
await self.coordinator.async_refresh()
86+
self._update_state()
87+
88+
except Exception as e:
89+
_LOGGER.error(f"Error starting manual color simulation: {e}")
90+
raise
7791

7892
async def async_turn_off(self, **kwargs):
7993
"""Turn off manual color simulation."""

0 commit comments

Comments
 (0)