11import logging
22from homeassistant .components .number import NumberEntity
33from homeassistant .const import UnitOfTime
4+ from homeassistant .helpers .storage import Store # <-- Added for persistence
45from .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):
2223class 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
6977class 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
7886class 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