Skip to content

Commit 64d0699

Browse files
committed
Fixed a weird bug with manual colour simulation and fixed persistence
1 parent 8dd76c0 commit 64d0699

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

custom_components/juwel_helialux/light.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,13 @@ async def async_turn_on(self, **kwargs):
144144
await self.coordinator.set_manual_override(True, 5)
145145

146146
try:
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)
147+
# Get duration from number entity (same approach as in switch.py)
148+
duration_entity = f"number.{self._attr_unique_id.split('_light')[0]}_manual_color_simulation_duration"
149+
duration_state = self.coordinator.hass.states.get(duration_entity)
150+
duration_minutes = int(float(duration_state.state) * 60) if duration_state else 720 # Default to 12 hours if not found
151+
152+
_LOGGER.debug("Using manual color simulation duration: %s minutes", duration_minutes)
153+
153154
# Set the light state with the configured duration
154155
await self._controller.start_manual_color_simulation(duration_minutes)
155156
await self._controller.set_manual_color(white, blue, green, red)
@@ -168,12 +169,20 @@ async def async_turn_on(self, **kwargs):
168169
async def async_turn_off(self, **kwargs):
169170
"""Turn the light off."""
170171
_LOGGER.debug("Turning off Juwel Helialux light")
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)
174-
await self._controller.set_manual_color(0, 0, 0, 0)
175-
self._attr_is_on = False
176-
self._attr_brightness = 0
177-
self._attr_rgbw_color = (0, 0, 0, 0)
178-
179-
self.async_write_ha_state()
172+
try:
173+
# Get duration from number entity (same approach as in switch.py)
174+
duration_entity = f"number.{self._attr_unique_id.split('_light')[0]}_manual_color_simulation_duration"
175+
duration_state = self.coordinator.hass.states.get(duration_entity)
176+
duration_minutes = int(float(duration_state.state) * 60) if duration_state else 720 # Default to 12 hours if not found
177+
178+
_LOGGER.debug("Using manual color simulation duration: %s minutes", duration_minutes)
179+
180+
await self._controller.start_manual_color_simulation(duration_minutes)
181+
await self._controller.set_manual_color(0, 0, 0, 0)
182+
self._attr_is_on = False
183+
self._attr_brightness = 0
184+
self._attr_rgbw_color = (0, 0, 0, 0)
185+
self.async_write_ha_state()
186+
except Exception as e:
187+
_LOGGER.error("Error turning off light: %s", e)
188+
raise

custom_components/juwel_helialux/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"issue_tracker": "https://github.com/MrSleeps/Juwel-HeliaLux-Home-Assistant-Custom-Component/issues",
1111
"platforms": ["sensor", "light", "select", "binary_sensor", "number", "switch"],
1212
"requirements": ["aiohttp"],
13-
"version": "2.0.4"
13+
"version": "2.0.5"
1414
}

custom_components/juwel_helialux/number.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from homeassistant.components.number import NumberEntity
33
from homeassistant.const import UnitOfTime
4+
from homeassistant.helpers.storage import Store # <-- Added for persistence
45
from .const import DOMAIN
56

67
_LOGGER = logging.getLogger(__name__)
@@ -12,8 +13,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
1213
tank_id = tank_name.lower().replace(" ", "_")
1314

1415
numbers = [
15-
HelialuxColorSimulationDuration(coordinator, entry, tank_name, tank_id),
16-
HelialuxDaytimeSimulationDuration(coordinator, entry, tank_name, tank_id)
16+
HelialuxColorSimulationDuration(hass, coordinator, entry, tank_name, tank_id),
17+
HelialuxDaytimeSimulationDuration(hass, coordinator, entry, tank_name, tank_id)
1718
]
1819

1920
async_add_entities(numbers, update_before_add=True)
@@ -22,7 +23,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
2223
class HelialuxNumberEntity(NumberEntity):
2324
"""Base class for Helialux number entities."""
2425

25-
def __init__(self, coordinator, entry, tank_name, tank_id, attribute, min_value, max_value, default_value):
26+
def __init__(self, hass, coordinator, entry, tank_name, tank_id, attribute, min_value, max_value, default_value):
2627
self.coordinator = coordinator
2728
self.entry = entry
2829
self.tank_name = tank_name
@@ -38,23 +39,22 @@ def __init__(self, coordinator, entry, tank_name, tank_id, attribute, min_value,
3839
self._attr_device_info = coordinator.device_info
3940
self._attr_translation_key = f"{attribute}_duration"
4041

42+
# Initialize persistent storage
43+
self._store = Store(hass, 1, f"{DOMAIN}_{self._attr_unique_id}.json")
44+
4145
@property
4246
def native_value(self):
4347
"""Return the current duration setting."""
4448
return self._state
4549

4650
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-
5251
"""Set the duration value."""
5352
self._state = float(value)
5453
# Store the value in the coordinator
5554
self.coordinator.data[f"{self._attr_translation_key}_duration"] = self._state
5655
self.async_write_ha_state()
57-
_LOGGER.debug(f"Set {self.entity_id} to {value} hours")
56+
await self._store.async_save({"value": self._state}) # Save persistently
57+
_LOGGER.debug(f"Set {self.entity_id} to {value} hours")
5858

5959
async def async_update(self):
6060
"""Update the state from HA data."""
@@ -65,20 +65,28 @@ def _update_state(self):
6565
"""Update internal state from coordinator data."""
6666
self._state = self.native_value
6767

68+
async def async_added_to_hass(self):
69+
"""Load previously stored state when added to Home Assistant."""
70+
await super().async_added_to_hass()
71+
data = await self._store.async_load()
72+
if data and "value" in data:
73+
self._state = float(data["value"])
74+
_LOGGER.debug(f"Restored {self.entity_id} to {self._state} hours")
75+
6876

6977
class HelialuxColorSimulationDuration(HelialuxNumberEntity):
7078
"""Number entity for setting the manual color simulation duration."""
7179

72-
def __init__(self, coordinator, entry, tank_name, tank_id):
80+
def __init__(self, hass, coordinator, entry, tank_name, tank_id):
7381
min_value = entry.data.get("color_simulation_min", 1)
7482
max_value = entry.data.get("color_simulation_max", 24)
75-
super().__init__(coordinator, entry, tank_name, tank_id, "manual_color_simulation", min_value, max_value, 12)
83+
super().__init__(hass, coordinator, entry, tank_name, tank_id, "manual_color_simulation", min_value, max_value, 12)
7684

7785

7886
class HelialuxDaytimeSimulationDuration(HelialuxNumberEntity):
7987
"""Number entity for setting the manual daytime simulation duration."""
8088

81-
def __init__(self, coordinator, entry, tank_name, tank_id):
89+
def __init__(self, hass, coordinator, entry, tank_name, tank_id):
8290
min_value = entry.data.get("daytime_simulation_min", 1)
8391
max_value = entry.data.get("daytime_simulation_max", 24)
84-
super().__init__(coordinator, entry, tank_name, tank_id, "manual_daytime_simulation", min_value, max_value, 12)
92+
super().__init__(hass, coordinator, entry, tank_name, tank_id, "manual_daytime_simulation", min_value, max_value, 12)

0 commit comments

Comments
 (0)